https://shikshamentor.
com/programming-in-c-
for-msbte-k-scheme/
312303 - Programming In ‘C’ (Sem II)
As per MSBTE’s K Scheme
CO / CM / IF / AI / AN / DS
Unit II Control Structures Mark - 16
Exam
S. N. MSBTE Board Asked Questions Marks
Year
W-23,
1 State any four decision making statements. S-22 2M
S-18,
Decision making statements:
1. if statement
2. if-else statement
Ans. 3. if-else-if ladder
4. Nested if-else statement
5. switch statement
6. conditional operator statement (? : operator)
Write a program using switch statement to check whether entered W-23,
2 4M
character is VOWEL or CONSONANT W-18
# include<stdio.h>
#include<conio.h>
void main()
Ans.
{
char ch;
printf(“Enter any character”);
scanf(“%c”, &ch);
switch(ch)
{
case ‘a’:
printf("%c is a vowel",ch);
break;
case 'e':
printf("%c is a vowel",ch);
break;
case 'i':
printf("%c is a vowel",ch);
break;
case 'o':
printf("%c is a vowel",ch);
break;
case 'u':
printf("%c is a vowel",ch);
break;
default:
printf("%c is a consonant",ch);
break;
}
getch();
}
3 Give the syntax of for loop S-23 2M
The syntax of for loop in c language is given below:
for(Expression 1; Expression 2; Expression 3)
{
//code to be executed.
Ans
}
4 Explain GO-TO statement with Example S-23 4M
The C goto statement is a jump statement which is sometimes also referred to as
an unconditional jump statement. The goto statement can be used to jump from
anywhere to anywhere within a function.
Syntax:
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
In the above syntax, the first line tells the compiler to go to or jump to the statement
marked as a label. Here, the label is a user-defined identifier that indicates the target
Ans. statement. The statement immediately followed after ‘label:’ is the destination
statement. The ‘label:’ can also appear before the ‘goto label;’ statement in the above
syntax.
Write a C program to print following pattern using loop
12345
1234
5 S-23 6M
123
12
1
// C program to print the inverted right half pyramid of
// stars
#include <stdio.h>
int main()
{
int rows = 5;
// first loop to print all rows
Ans
for (int i = 0; i < rows; i++) {
// first inner loop to print the * in each row
for (int j = 0; j < rows - i; j++) {
printf("* ");
}
printf("\n");
}
}
Write a program to convert temperature in Fahrenheit degrees to
6 W-22 4M
Centigrade degrees.
Ans #include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Fahrenheit =%f Temperature in Centigrade
=%f", fahrenheit, celsius);
getch();
}
Write a C program to print following pattern using loop
1
22
7 W-22 4M
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
Ans
{
int i,j,n;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
Write a C program with comments to reverse the digit of integer
8 number. For example the number 12345 should be displayed as W-22 6M
54321.
#include<stdio.h>
Ans #include<conio.h>
void main()
{
int num=0;
clrscr();
printf("Enter the number"); // Accept the number to be reversed
scanf("%d", &num);
int rev_num = 0;
while (num > 0) // se a while loop to iterate until the value of num becomes 0
{
//last digit of number is extracted by using num %10 (modulo operator)
//which is then multiplied by 10
// num is updated by dividing by 10 which removes last digit in each iteration
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
printf("Reverse number is %d", ans); // print the reversed number
getch();
}
S-22
9 Explain do while loop with example. 4M
S-23
The do…while in C is a loop statement used to repeat some part of the code
till the given condition is fulfilled. It is a form of an exit-controlled or post-
tested loop where the test condition is checked after executing the body of
the loop. Due to this, the statements in the do…while loop will always be
executed at least once no matter what the condition is.
Syntax of do…while Loop in C
Ans. do {
// body of do-while loop
} while (condition);
// C Program to demonstrate the use of do...while loop
#include <stdio.h>
int main()
{
// loop variable declaration and initialization
int i = 0;
// do while loop
do {
printf("Hello\n");
i++;
} while (i < 3);
return 0;
}
Out Put
Hello
Hello
Hello
10 Explain nested if-else with example. W-19 4M
When a series of decision is required, nested if-else is used. Nesting
means using one if-else construct within another one. If the condition
in the outer if, is true, then only the inner if-else will get executed.
Ans. Further the statements in the inner if will get execute only if the
condition of inner if, evaluates to true. If it is false, the statements in
inner else will get executed.
If the outer if evaluates to false, then the statements in outer else get
executed.
General syntax:
if(condition) {
if(condition) {
statements
} else {
statements
}
} else
{
statements
}
statements
Example:
#include<stdio.h>
#include<conio.h>
void main() {
int val;
clrscr();
11 Illustrate the use of break and continue statement with example. W-19 4M
Break: It breaks the execution of the loop which allows exiting from
any loop or switch, such that break statement skips the remaining part
of current iterations of the loop.
Syntax: break;
Continue: It is used when it is required to skip the remaining portion
of the loop without breaking loop it will transfer control directly to
next iteration
Syntax: continue;
Ans
In given program sequence if “break” executes then execution control
will jump out of loop & next statement after loop will be executed. In
given program sequence if “continue” executes then execution
control will skip remaining statements of loop & will start next
iteration of loop
12 Write a program to add , subtract, multiply and divide two W-19 4M
numbers, accepted from user using switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ch,add,sub,mul,div;
clrscr();
printf("\n1 for addition \n2 for substraction");
printf("\n3 for multiplication \n4 for division");
printf("\nEnter two numbers:");
scanf("%d%d",&a,&b);
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
add=a+b;
Ans. printf("Addition of a & b=%d",add);
break;
case 2:
sub=a-b;
printf("Substraction of a & b=%d",sub);
break;
case 3:
mul=a*b;
printf("Multiplication of two numbers=%d",mul);
break;
case 4:
div=a/b;
printf("Division of two numbers=%d",div);
break;
default:
printf("Invalid choice....");
}
getch();
}
13 Write the syntax of switch case statement. S-19 2M
switch(variable)
{
case value1:
statements
break;
case value2:
statements;
Ans break;
.
.
.
default:
statements;
break;
}
S-19
14 State any two differences between while and do-while statement. 2M
W-22
While do-while
Condition is checked first then Statement(s) is executed atleast once,
statement(s) is executed. thereafter condition is checked.
Ans.
It might occur statement(s) is executed At least once the statement(s) is
zero times, If condition is false. executed.
No semicolon at the end of while. Semicolon at the end of while.
while(condition) while(condition);
If there is a single statement, brackets
Brackets are always required.
are not required.
Variable in condition is initialized variable may be initialized before or
before the execution of loop. within the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) do { statement(s); }
{ statement(s); } while(condition);
Draw flow chart for checking weather given number is prime or
15 S-19 4M
not.
Ans
Write a program to take input as a number and reverse it by
16 S-19 4M
While loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int no;
int sum=0,rem;
printf("\n Enter number:");
scanf("%d",&no);
Ans. while(no>0)
{
rem=no%10;
no=no/10;
sum=sum*10+ rem;
}
printf("\nsum=%d",sum);
getch();
}
17 Give syntax of if-else ladder. W-18 2M
if(condition_expression_One)
{
statement1;
Ans. }
else if (condition_expression_Two)
{
statement2;
18 Explain do –while loop with example. S-18 4M
In some applications it is necessary to execute the body of the loop
before the condition is checked; such situation can be handled by
do statement.
Ans.
At least once the body of loop will be executed.
do statement, first executes the body of the loop.
At the end of the loop, the test condition in the while statement is
evaluated. If the condition is true, then it continues to execute body
of the loop once again.
This process continues as long as the condition is true.
When the condition becomes false, the loops will be terminated
and the control goes to next statement after while statement.
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1;
clrscr();
printf("\n Odd numbers from 1 to 20 are \n");
do
{
if(i%2 ! = 0)
printf("\n %d", i);
i++;
}while(i<=20); /* The loop iterates till the value of i is less than or
equal to 20 */
getch();
}
Write a program using switch statement to check whether entered
19 W-18 6M
Character is VOWEL or CONSONANT.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
Ans.
clrscr();
printf("Enter character:");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("\n Entered character is VOWEL");
break;
default:
printf("\n Entered character is CONSONANT");
}
getch();
}
20 State use of while loop with syntax. S-18 2M
While loop is used in programming to repeat a specific block of
statement until some end condition is met.
The syntax of a while loop is:
while (test Expression)
Ans
{
Statements…
statements….
}
Draw a flowchart of Do-while loop and write a programto add
21 S-18 6M
Numbers until user enters zero.
Program:-
#include <stdio.h>
#include <conio.h>
Ans
void main()
{
int no,sum=0;
clrscr();
do
{
printf("\n Enter a number:");
scanf("%d",&no); sum=sum+no;
}while(no!=0);
printf("\n Sum of entered numbers =%d",sum);
getch();
}
Thank You
https://shikshamentor.com/programming-in-c-for-msbte-k-
scheme/
Visit
https://shikshamentor.com/