PST Unit 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CA-C2T: PROBLEM SOLVING TECHNIQUES

UNIT - II [12 Hours]


C Programming: Getting Started, Variables and Arithmetic expressions. Input and Output: Standard input and output,
formatted output- printf, variable length argument list, formatted input-scanf. Control Flow: Statements and Blocks,
If-else, else-if, switch, loops: while loop, for loop, do while, break and continue, goto and labels. Pointers and Arrays:
pointers and address, pointers and function arguments, multidimensional array, initialization of pointer arrays,
command line arguments.

Two Mark Questions


1. Define variable..
Variables are the name given to the memory location to hold a value within the program . A
variable change during the execution of a program.
Syntax : <data type>variable list;
Ex : int a,b;
float x,y;
a=10;
2. Define constants.
They are the fixed values of a variable. They do not change during the execution of a program .
Ex : Integer: int max=100;
Real: float pi= 3.142;
3. Define c-tokens with an example.
They are the smallest and the basic elements of a c programming. C tokens includes identifiers
variables, keywords, operators, special symbols, strings.
Ex: Keywords: int, float, char etc.,
Variables: int a,b;
4. Explain formatted input and output with an example.
Formatted input [scanf() ]: It is a statement used to accept different types of values.
Syntax: scanf(<format specifier>list of variables)
Ex: int a;
float b;
char c;
scanf(“%d %f %c”, &a,&b,&c);

Formatted output [printf() ]: It is a statement used to print the values using field-width specification
and escape sequence so the output looks neat and presentable.
Syntax: printf(<format specifier>list of variables)
int a;
float b;
char c;
scanf(“%d %f %c”, &a,&b,&c);
printf(“%d %f %c”, a,b,c);
5. Explain the use of break and continue.
The break keyword is used to terminate any of the looping constructs. This keyword is always used in
the connection with ‘if’ within a looping construct.
Syntax: while(condition)
{
……………..
if(condition)
break;
………………..
………………..
}
When the continue is encountered, it transfers the control back to the loop condition by skipping the
rest of the statements of the body of the loop. The continue keyword only works with loops.
Syntax: while(condition)
{
……………..
if(condition)
continue;
………………..
………………..
}
6. Explain goto statements with examples.
It is an unconditional branching statement where the control of the program transfers from one part of
the program to another directly.
Syntax: go to<label name>
Ex: {
if(a%2==0)
go to even;
………………
………………..
………………..
even: printf(“\n Even number”);
go to stop;
}
7. How to initialize an array of pointer.
Array of pointer is also known as table of strings can be initialized as :
char *state_name[3]= { “Karnataka”,
“Tamil Nadu”,
“Kerala” };
8. Define pointer with an example.
A pointer is a variable which holds the address of another variable.
Int a;
Int *ptr_a;
ptr_a = &a;
9. Define array with example.
Array is a bounded collection of elements of same datatypes.
Ex: int Regno[5]= {10,20,30,40,50};
Regno[0] Regno[1] Regno[2] Regno[3] Regno[4]
10 20 30 40 50
10. What are command line argument?
When a parameter is supplied to a program during the execution then such as parameter is called
command line argument.
Syntax: main(int argc, char*argv[])
Ex: copy a file from FILE_A to FILE_B
(name of the program COPYPRG.c)
argc -3
argv[0]-> COPYPRG.c
argv[1]-> FILE_A
argv[2]-> FILE_B

Long Answers

1. Explain the structure of c program.


2. Explain if and else if structures
IF STATEMENT: If statement is a conditional statement that executes sequence of statement if
condition is true.
Syntax: if<condition>
{
Statements
}
Ex: if (num>0)
{
printf(" Positive number");
getch();
}
ELSE IF STATEMENT: An else if statement is a conditional statement that executes sequence of
statements if condition is false.
Syntax: if<condition>
{
Statement (true block)
}
else
{
Statement (false block)
}
Ex: if(a>b)
{
BIG=a;
printf("%d", big);
}
else
{
BIG=b;
printf("%d", big);
}
3. Explain different types of loops in C
4. Explain switch case with syntax and example.
It is a multiway branching statement similar to else if ladder where one of the case statement block
will be executed depending on the expression.
Syntax: switch(expression)
{
case label-1; statement-1
………………………
break;
case label-2; statement-2
………………………
break;
case label-2; statement-2
………………………
break;
default: default statement;
}
Ex: switch(dayno)
{
case 1: printf(“Monday”);
break;
case 2: printf(“Tuesday”);
break;
case 3: printf(“Wednesday”);
break;
default: printf(“Invalid number”);
}
5. What are functions. Explain syntax and example.
Function is a subprogram which is used to perform a specific task. The advantage of using a
function, it imposes the modularity of the program and thereby helps the programmer to divide
complex programs to simpler parts.
Syntax: <return type> function name( arguments)
{
body of the function;
}
Ex: void ADD(int a, int b)
{
int c;
c=a+b;
return(c);
}
6. Explain different categories of user define functions.
7. Explain call by value and call by reference.

8. Explain different data types supported by C.


9. Write a C program to given number is PRIME or NOT
Refer Lab program
10. Write a C find the roots of quadratic equations.
Refer Lab program
11. Write a C find sum of two matrices.
Refer Lab program
12. Write a C program to find factorial of a Number
Refer Lab program
13. Write a C program to generate Fibonacci series.
Refer Lab program
14. Write a C program to find largest of 3 numbers
Refer Lab program
15. To find percentage of marks and display appropriate message.
Refer Lab program

You might also like