UNIT-3 Two Marks Question and Answers
UNIT-3 Two Marks Question and Answers
UNIT-3 Two Marks Question and Answers
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
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
#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
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
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.
#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
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
Output
1
2
3
4
5
#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
#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
# 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
# include <stdio.h>
int main()
{
int a=5;
printf("Value of a = %d", a);
return 0;
}
Output:
Value of a = 5
#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
#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