UNIT-3 Two Marks Question and Answers

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

UNIT – 3 Two marks question and answers

1. Define Branching statements. Mention its types


The statements that help us to jump from one statement to another statement with or without condition is
called branching statements.

1. Conditional Branching statements


2. Unconditional Branching statements

2. What is Conditional Branching statement? Give examples


The statements that help us to jump from one statement to another statement based on condition.
Example:
a. Simple if statement,
b. if…else statement,
c. nested if…else statement,
d. else…if ladder (cascaded if statement)
e. switch statement

3. What is unconditional Branching statement? Give examples

The statements that help us to jump from one statement to another statement without any conditions.
Example:
a. goto statement,
b. break statement,
c. continue statement and
d. return statement

4. Define looping statements. Give examples

The statements that help us to execute set of statements repeatedly are called as looping statements.
Example:
a. while statement,
b. do…while statement and
c. for statement

5. Write a c program to illustrate or demonstrate simple if statements.


#include<stdio.h>
int main()
{
int n;
printf(“Enter any non-zero integer: \n”);
scanf(“%d”, &n);
if(n>0)
{
printf(“Number is positive number ”);
}
return 0;
}
Output:
Enter any non-zero integer:
7
Number is positive number

6. Write syntax and flowchart to simple statement.


The syntax is shown below:
if (expression)
{
statement1;
}
Next Statement;

7. Write syntax and flowchart to if - else statement.


The syntax is shown below:
if (expression)
{
statement1;
}
else
{
Statement2;
}
Next Statement;
8. Write a c program to illustrate or demonstrate if-else statements.
#include<stdio.h>
int main()
{
int n;
printf(“Enter any non-zero integer: \n”);
scanf(“%d”, &n);
if(n>0)
{
printf(“Number is positive number ”);
}
else
{
printf(“Number is negative number”);
}
return 0;
}
Output 1:
Enter any non-zero integer:
7
Number is positive number
Output 2:
Enter any non-zero integer:
-7
Number is negative number

5. Write syntax and flowchart to nested if - else statement.


The syntax is shown below:
if(expr1)
{
if(expr2)
{
Statement1;
}
else
{
Statement2;
}
}
else
{
if(expr2)
{
Statement1;
}
else
{
Statement2;
}
}
Next Statement;

6. Write a c program to illustrate or demonstrate nested if-else statements.

#include<stdio.h>
int main()
{
int a,b,c;
printf(“Enter Three Values: \n”); scanf(“%d %d %d ”, &a, &b, &c);
printf(“Largest Value is: ”) ;
if(a>b)
{
if(a>c)
printf(“ %d ”, a);
else
printf(“ %d ”, c);
}
else
{
if(b>c)
printf(“ %d ”, b);
else
printf(“ %d ”, c);
}
return 0;
}
Output:
Enter Three Values:
17 18 6
Largest Value is: 18

7. Write Syntax to else if LADDER STATEMENT

if(expression1)
{
statement1;
}
else if(expression2)
{
statement2;
}
else if(expression3)
{
statement3 ;
}
.
.
.
else if(expressionN)
{
StatementN ;
}
else
{
Default Statement ;
}
Next Statement;
8. Write flowchart to else if LADDER STATEMENT
9. Write a c program to illustrate or demonstrate else if LADDER STATEMENT.
#include<stdio.h>
int main()
{
int a,b,c;
printf(“Enter Three Values: \n”);
scanf(“%d %d %d ”, &a, &b, &c); printf(“Largest Value is: ”) ;
if ( a>b && a>c )
printf(“ %d ”, a);
else if ( b>a && b>c )
printf(“ %d ”, b);
else
printf(“ %d”, c);
retorn 0;
}
Output:
Enter Three Values:
17 18 6
Largest Value is: 18

10. Write syntax for switch.


switch ( expression )
{
case label1 :
break;
case label2 :
break;
default Statement
}
11. Write a c program to demonstrate or illustrate switch statement?
#include <stdio.h>
int main()
{
int num1, num2, result ; char op ;
printf ( " Enter number operator number\n" ) ;
scanf ("%f %c %f", &num1, &op, &num2 ) ;
switch ( op )
{
case '+' : result = num1 + num2 ;
break ;
case '-' : result = num1 - num2 ;
break ;
case „*‟: result = num1 * num2 ;
break ;
case „%‟: result=num1%num2;
break;
case „/‟ :
if ( num2 != 0.0 )
{
result = num1 / num2 ;
break ;
}
// else we allow to fall through for error message
default : printf ("ERROR -- Invalid operation or division by 0.0" ) ;
}
printf( "%f %c %f = %f\n", num1, op, num2, result) ;
return 0;
}
Output:
Enter number operator number 2 + 4
2+4=6

12. Write a syntax to unconditional statements


goto statement

break statement

continue statement
13. Write syntax to looping statements
while loop
while(condition)
{
//code to be executed
Statement;
}

do-while statement

do
{
//code to be executed
Statement;
} while(condition);

for loop

for (Initialization;Condition;Increment/Decrement)
{
//code to be executed
Statement;
}
14. Write a c Program to illustrate goto statement.

#include<stdio.h>
int main()
{
int x;
printf(“Enter a Number: \n”);
scanf(“%d”, &x);
if(x % 2 = = 0)
goto even;
else
goto odd;
even : printf(“%d is Even Number” , x);
return 0;
odd :printf(“ %d is Odd Number” , x);
return 0;
}
Output 1:
Enter a Number: 5
5 is Odd Number.
Output 2:
Enter a Number: 8

8 is Even Number.

15. Write a c program to illustrate break statement

#include<stdio.h>
#include<stdlib.h>
int main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("Outside of loop i = %d",i);
return 0;
}
OUTPUT
0 1 2 3 4 5 Outside of loop i = 5
16. Write a c program to illustrate continue statement

#include<stdio.h>
#include<stdlib.h>
int main(){
int i=1; //initializing a local variable
//starting a loop from 1 to 10
for(i=1;i<=10;i++){
if(i==5)
{ //if value of i is equal to 5, it will continue the loop
continue;
}
printf("%d \n",i);
} //end of for loop
return 0;
}

OUTPUT
1
2
3
4
6
7
8
9
10
17. Write a flowchart to while statement
18. Write a flowchart to do-while statement

19. Write a flowchart to for loop


20. Write a c program illustrate to while loop

#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
Output
1
2
3
4
5

21. Write a c program illustrate to do-while loop

#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
}while(i<=5);
return 0;
}

Output
1
2
3
4
5
22. Write a c program illustrate to for loop
#include <stdio.h>
int main()
{
int i;
for (i=1;i <= 5;i++)
{
printf("%d\n", i);
}
return 0;
}
Output
1
2
3
4
5

23. Write a c program to illustrate arithmetic operators

#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
return 0;
}
OUTPUT
Addition of a, b is: 60
Subtraction of a, b is: 20

24. Write a c program to illustrate compound assignment operators

# include <stdio.h>
int main()
{
int Total=0,i; for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
return 0;
}
Output:
Total = 45

25. Write a c program to illustrate simple assignment operators

# include <stdio.h>
int main()
{
int a=5;
printf("Value of a = %d", a);
return 0;
}
Output:
Value of a = 5

26. Write a c program to illustrate relational operators


#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
return 0;
}
Output:
m and n are not equal

27. Write a c program to illustrate relational operators


#include <stdio.h>
int main()
{
int m=40,n=20;
if (!(m>n && m !=0) )
{
printf("&& Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true.”\”But, status is inverted as a false\n");
}
return 0;
}
Output:
&& Operator: Both conditions are true
! Operator: Both conditions are true. But, status is inverted as false

28. Write a c program to illustrate bitwise operators


#include <stdio.h>
int main()
{
int m=40,n=80,AND_opr,OR_opr;
AND_opr = (m&n);
OR_opr = (m|n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
return 0;
}
Output:
AND_opr value = 0
OR_opr value = 120

29. Write a C program to demonstrate conditional/ternary operators.

#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
return 0;
}
OUTPUT
x value is 1
y value is 2

30. Write a C program to demonstrate increment and decrement operators.

#include <stdio.h>
int main()
{
int i=1,j=20;
while(i<10)
{
printf("%d ",i);
i++;
}
while(j>10)
{
printf("%d ",j);
i--;
}
return 0;
}
Output:
123456789
20 19 18 17 16 15 14 13 12 11

You might also like