0% found this document useful (0 votes)
19 views12 pages

pps-1 Unit-3 Ques Bank Answers

The document provides an overview of control structures in C programming, including sequential, selection, and iterative statements. It includes explanations and examples of if-else statements, nested if-else, else-if ladders, and various programming tasks such as finding the greatest of numbers, checking eligibility to vote, and calculating grades. Additionally, it discusses unconditional statements, differences between break and continue, and includes several programming examples for practical understanding.

Uploaded by

sonunikhil6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

pps-1 Unit-3 Ques Bank Answers

The document provides an overview of control structures in C programming, including sequential, selection, and iterative statements. It includes explanations and examples of if-else statements, nested if-else, else-if ladders, and various programming tasks such as finding the greatest of numbers, checking eligibility to vote, and calculating grades. Additionally, it discusses unconditional statements, differences between break and continue, and includes several programming examples for practical understanding.

Uploaded by

sonunikhil6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT-III

SHORT ANSWER QUESTIONS:

1. Define control structure and classify them.


Flow of control through any given function is implemented with three basic types of control
structures:
Sequential statements: default mode. Sequential execution of code statements (one line after
another) -- like following a recipe
Selection statements: used for decisions, branching -- choosing between 2 or more
alternative paths. In C++, these are the types of selection statements:
• if
• if/else
• switch
Iterative statements: used for looping, i.e. repeating a piece of code multiple times in a row.
In C++, there are three types of loops:
• while
• do/while
• for

2. Explain if-else with syntax and flowchart.


If condition returns true then the statements inside the body of “if” are executed and the
statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in
“else” are executed.
Syntax:
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}

Flow diagram of if else statement

3. Write a program to find greatest of two numbers using if else statement.


#include<stdio.h>
void main() Output: enter a,b values
{
int a,b; 10 20
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b); B is Big
if(a>b)
printf(“A is Big”);
else
printf(“B is Big”);
}

4. Explain nested-if syntax with flow chart.


When an if else statement is present inside the body of another “if” or “else” then this is called nested
if else.

Syntax of Nested if else statement:

if(condition){
//Nested if else inside the body of "if"
if(condition2){
//Statements inside the body of nested "if"
}
else{
//Statements inside the body of nested "else"
}
}
else{
//Statements inside the body of "else"
}

Flow chart

5. Explain ladder-if syntax with flow chart.


When we have multiple options available or we need to take multiple decisions based on available
condition, we can use another form of if statement called else…if ladder.
In else…if ladder each else is associated with another if statement.
Evaluation of condition starts from top to down.
If condition becomes true then the associated block with if statement is executed and rest of
conditions are skipped.
If the condition becomes false then it will check for next condition in a sequential manner.
It repeats until all conditions are cheeked or a true condition is found.
If all condition available in else…if ladder evaluated to false then default else block will be executed.
It has the following syntax:
if (condition 1)
{
// block 1
}
else if (codition 2)
{
// block 2
}
else if(codition 3)
{
// block 3
}
else
{
// default block
}

Flowchart:

7 Write a program to check whether the person is eligible to vote or not.


void main()
{ Output: enter the age:
int age;
printf(“enter the age:”); 10
scanf(“%d”,&age);
if(age>=18) EVEN
printf(“Elligible for vote”);
else
printf(“Not elligible for vote”);
}

8 Write a program to print grade of a student using else if ladder.


Marks obtained by the student must given through keyboard. Print the result as per the following
rules. 1. Percentage is above or equal to 75- distinction. 2. Percentage is less than 75 and equal to 60-
first class. 3. Percentage is less than 60 and equal to 50- second class. 4. Percentage is less than 50 and
equal to 40- third class. 5. Percentage is below 40 – failed.

#include<stdio.h> Output:

Enter marks

70 70 70 70

First class
main()
{
float m1,m2,m3,m4;
float perc;
printf(“enter marks\n”);
scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4);
perc=(m1+m2+m3+m4)/4;
if(perc>=75)
printf(“\ndistinction”);
else if(per<75 && per>=60)
printf(“\n first class”);
else if(per<60 && per>=50)
printf(“\n second class”);
else if(per<50 && per>=40)
printf(“\n third class”);
else printf(“\nfail”);
}

9 Write a program to find greatest of three numbers using Nested if.

#include <stdio.h>
void main()
{
int a,b,c;
printf("Enter 3 numbers: \n");
scanf("%d %d %d",&a,&b,&c);
if(a>=b) Output:
{
if(a>=c) Enter 3 numbers
printf("%d is largest",a);
else 342
printf("%d is largest",c);
} 4 is largest
else if(b>=c)
printf("%d is largest",b);
else
printf("%d is largest",c);
}
10 Write the difference between two-way selection and multi-way selection statements.

The two-way selection statement:

• The decision is based on resolving a binary expression, and then executing a set of commands
depending on whether the response was true or false.
• C Language implements two-way selection with the if…else statement.
• An if…else statement is a paired statement used to selectively execute code based on two
alternatives.

The Multi-way selection statement:

• You can choose statement to run from many sets of choices.


• There are 2 multy-way selection statements in C

else if statement and switch case.


• This statement selects choice based on expression value or variable value.

11 Write a program to check whether the entered character is Vowel or Consonant.

#include<stdio.h>
void main()
{
char ch;
printf(“Enter a character:”);
scanf(“%c”,&ch);
if(ch==’a’|| ch==’i’|| ch==’o’|| ch==’e’|| ch==’u’)
printf(“%d is a vowel”,ch);
else
printf(“%d is nor a vowel”,ch);
}

12 Write the difference between while and do while statements.

13 Write a program to check whether the given number is Prime number or not.
#include<stdio.h>
int main()
{
int n, i, cnt=0;
printf("Enter the number to find whether it is prime or not:");
scanf("%d" , &n);
for(i=1;i<=n;i++)
{
if(n%i==0)
cnt++;
}
if(cnt==2)
printf("%d is a prime\n",n);
return 0;
}

14. Write a program to find sum of N natural numbers.

#include<stdio.h>
int main()
{
int n, i,sumt=0;
printf("Enter the number:");
scanf("%d" , &n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("sum of %d natural numbers = %d\n", n, sum);
return 0;
}

15. Write a program to find the factorial of a given number.

#include<stdio.h>
int main()
{
int n, i,fact=1;
printf("Enter the number:");
scanf("%d" , &n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial of %d = %d\n", n, fact);
return 0;
}

16. Write a program to print sum individual digits of the given number.

1. #include<stdio.h>
2. void main() Output:
3. {
4. int n, sum = 0, r; Enter an integer
5.
6. printf("Enter an integer: "); 121
7. scanf("%d", &n);
8. while( n!=0 ) 121 is a palindrome.
9. {
10. r = n%10;
11. sum = sum + r;
12. n=n/10;
13. }
14. printf("The sum of individual digits of a given %d number is = %d.", n, sum);
15. }

17. Write a program to display the following pattern.


*
* *
* * *
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf(“*”);
}
printf(“\n”);
}
}

18. Define unconditional statement in C and list out them .


unconditional statements are statements that execute without specific conditions. Such as.
continue,
break,
and goto.

19. Write the differences between break and continue statements.

Comparison Chart:
BASIS FOR
BREAK CONTINUE
COMPARISON

Task It terminates the execution of It terminates only the current iteration of the
remaining iteration of the loop. loop.

Control after 'break' resumes the control of the 'continue' resumes the control of the
break/continue program to the end of loop enclosing program to the next iteration of that loop
that 'break'. enclosing 'continue'.

Causes It causes early termination of loop. It causes early execution of the next
iteration.

Continuation 'break' stops the continuation of loop. 'continue' do not stops the continuation of
loop, it only stops the current iteration.
BASIS FOR
BREAK CONTINUE
COMPARISON

Other uses 'break' can be used with 'switch', 'continue' can not be executed with 'switch'
'label'. and 'labels'.

20. Write a short notes on goto statement.


A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled
statement in the same function.
NOTE − Use of goto statement is highly discouraged in any programming language because it makes
difficult to trace the control flow of a program, making the program hard to understand and hard to
modify. Any program that uses a goto can be rewritten to avoid them.
The syntax for a goto statement in C is as follows −
goto label;
..
.
label: statement;

FLOW GRAPH:

LONG ANSWER QUESTIONS

1. Explain Selection statements in C with syntax, flowchart and example.

Take a scan copy of class notes

2. Explain Iterative statements in C with syntax, flowchart and example.


Take a scan copy of class notes

3. Define switch statement explain with syntax and flowchart. Write a program to test all
arithmetic operators using switch statement.

Take a scan copy of class notes

4. Write a program to display minimum and maximum numbers among 3 numbers using
selection statements.

#include<stdio.h>
void main()
{
int a,b,c;
printf("eneter any 3 numbers"; Output:
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c) Enter any 3 values
printf("a is big");
else if(b>c) 10 50 40
printf("b is big");
B is big
else
printf("c is big"); A is small
if(a<b&&a<c)
printf("a is small");
else if(b<c)
printf("b is small");
else
printf("c is small");
}

5. Write a program to reverse a given number by using while loop 6 Write a program to check
whether the given number is Armstrong or not.
Ans: An Amstrong number is defined as the sum of cubes of individual digit of a given number.

1. #include<stdio.h>
2. void main()
3. {
4. int n, temp, r, sum = 0;
5.
6. printf("Enter any number: ");
Output:
7. scanf("%d", &number);
8.
Enter any number
9. temp= n;
10.
1634
11. while (n != 0)
12. {
13. r = n%10; 1634 is Armstrong number
14. sum += r*r*r;
15. n=n/10;
16. }
17.
18. if(sum == temp)
19. printf("%d is an Armstrong number.", temp);
20. else
21. printf("%d is not an Armstrong number.", temp);
22.
23. }

7 Write a program to check whether the given number is palindrome or not.

16. #include<stdio.h>
17. void main()
18. {
19. int n, rev = 0, r, temp;
20.
21. printf("Enter an integer: ");
22. scanf("%d", &n); Output:
23.
24. temp = n; Enter an integer
25.
26. // reversed integer is stored in variable 121
27. while( n!=0 )
28. {
121 is a palindrome.
29. r = n%10;
30. rev = rev*10 + r;
31. n=n/10;
32. }
33.
34. // palindrome if orignalInteger and reversedInteger are equal
35. if (temp == rev)
36. printf("%d is a palindrome.",temp );
37. else
38. printf("%d is not a palindrome.",temp);
39.
40. }

8. Write a program to display Fibonacci series by using for loop.

#include<stdio.h>
void main()
{
int a, b, c, n;
printf(“Enter the length of the Fibonacci series:”);
scanf(“%d”,&n);
a=0;b=0;
while((n-2)>=0)
{
c= a+b;
printf(“%d\t”,c);
a=b;
b=c;
}
}

9. Write a program to print Prime Numbers in a given Range.

Ans: #include<stdio.h>
int main()
{
int n, i, j, cnt=0;
printf("Enter the range to find the prime numbers between the range:");
scanf("%d" , &n);
for(i=2;i<=n;i++)
{
cnt=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
cnt++;
}
if(cnt==2)
printf("%d is a prime\n",i);

}
return 0;
}

10. Write a program to display the following pattern.

1
12
123
1234
Ans:
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf(“%4d”,j);
}
printf(“\n”);
}
}

You might also like