0% found this document useful (0 votes)
19 views

Chapter 3

Uploaded by

Priyansh Jain
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 views

Chapter 3

Uploaded by

Priyansh Jain
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/ 53

PPS (3110003)

CHAPTER 3
Control structure in C

Q.1 Define break and continue statement with example. (PPS_WIN2019)


(PPS_WIN_18)(CPU_SUM_2016,19)
The break statement is used to come out of the loop instantly. When a break statement is
encountered inside a loop, the control directly comes out of loop and the loop gets
terminated. It is used with if statement, whenever used inside loop.

Its syntax is:

break;

Example of break statement

#include <stdio.h>

int main()

int num =0;

while(num<=100)

printf("value of variable num is: %d\n", num);

CKPCET, SURAT 1
PPS (3110003)

if (num==2)

break;

num++;

printf("Out of while-loop");

return 0;

Output:

value of variable num is: 0

value of variable num is: 1

value of variable num is: 2

Out of while-loop

The continue statement skips the current iteration of the loop and continues with the next
iteration.

Its syntax is:

continue;

CKPCET, SURAT 2
PPS (3110003)

Example of Continue Statement

#include <stdio.h>

int main ()

/* local variable definition */

int a = 10;

/* do loop execution */

do {

if( a == 15) {

a = a + 1;

continue;

printf("value of a: %d\n", a);

a++;

} while( a < 20 );

return 0;

CKPCET, SURAT 3
PPS (3110003)

Output

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 16

value of a: 17

value of a: 18

value of a: 19

Q.2 Construct ‘C’ program to print the following pattern using loop
statement.(PPS_WIN_2019)

22

333

4444

55555

#include <stdio.h>

void main()

int i,j,rows=5;

for(i=1;i<=rows;i++)

CKPCET, SURAT 4
PPS (3110003)

for(j=1;j<=i;j++)

printf("%d",i);

printf("\n");

}
Q.3 Demonstrate a C program to input an integer number and check last digit of
number is even or odd.(PPS_WIN_2019)

#include <stdio.h>

int main()

int Number, LastDigit;

printf("\n Please Enter any Number : ");

scanf("%d", & Number);

LastDigit = Number % 10;

printf(" \n The Last Digit of a Given Number %d = %d", Number, LastDigit);

if(LastDigit%2==0)

printf("%d is an EVEN number.",LastDigit);

else

printf("%d is an ODD number.",LastDigit);

return 0;

Q.4 Explain for loop with example.

A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.

CKPCET, SURAT 5
PPS (3110003)

Syntax

The syntax of a for loop in C programming language is −

for ( init; condition; increment ) {

statement(s);

Here is the flow of control in a 'for' loop −

• The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
• Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and the flow of control jumps to the next
statement just after the 'for' loop.
• After the body of the 'for' loop executes, the flow of control jumps back up to the
increment statement. This statement allows you to update any loop control variables.
This statement can be left blank, as long as a semicolon appears after the condition.
• The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the 'for' loop terminates.

Example

#include <stdio.h>

int main () {

int a;

/* for loop execution */

for( a = 10; a < 20; a = a + 1 ){

printf("value of a: %d\n", a);

return 0;

Output

CKPCET, SURAT 6
PPS (3110003)

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

value of a: 16

value of a: 17

value of a: 18

value of a: 19

Q.5 Build a function to check number is prime or not. If number is prime then function
return value 1 otherwise return 0. .(PPS_WIN2019) (PPS_WIN_18)
#include<stdio.h>

int check_prime(int);

main()

{
int n, result;

printf("Enter an integer to check whether it is prime or not.\n");


scanf("%d",&n);

result = check_prime(n);

if ( result == 1 )
printf("%d is prime.\n", n);
else
printf("%d is not prime.\n", n);

return 0;
}

int check_prime(int a)
{

CKPCET, SURAT 7
PPS (3110003)

int c;

for ( c = 2 ; c <= a - 1 ; c++ )


{
if ( a%c == 0 )
return 0;
}
return 1;
}

Q.6 Construct ‘C’ program to print the following pattern using loop statement.
.(PPS_WIN2019)
1
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
printf("\n Enter the value of n:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();

CKPCET, SURAT 8
PPS (3110003)

}
Q.7 State the difference between entry control loop and exit control loop.
.(CPU_WIN_2019)(CPU_SUM_2016)
BASIS OF
ENTRY CONTROLLED LOOP EXIT CONTROLLED LOOP
COMPARISON
Entry controlled loop is a loop in Exit controlled loop is a loop in which
which the test condition is checked the loop body is executed first and then
Description
first, and then loop body will be the given condition is checked
executed. afterwards.
False Test If the test condition is false, loop body If the test condition is false, loop body
Condition will not be executed. will be executed at least once.
Examples of exit controlled loop An example of exit controlled loop is
Example
include, For Loop and While Loop. Do While Loop.
Entry controlled loop are used when
Entry controlled loop is used when
checking of test condition is
Use checking of test condition is mandatory
mandatory before executing loop
after executing.
body.

Q.8 Compare for loop and while loop with illustrations .(CPU_WIN_2019) (PPS_WIN_18)

BASIS FOR
FOR WHILE
COMPARISON

Declaration for(initialization; condition; while ( condition)


iteration)
{
{ statements; //body of loop
//body of 'for' loop }
}

Format Initialization, condition checking, Only initialization and condition


iteration statement are written at the checking is done at the top of the
top of the loop. loop.

Use The 'for' loop used only when we The 'while' loop used only when
already knew the number of the number of iteration are not
iterations. exactly known.

CKPCET, SURAT 9
PPS (3110003)

BASIS FOR
FOR WHILE
COMPARISON

Condition If the condition is not put up in 'for' If the condition is not put up in
loop, then loop iterates infinite times. 'while' loop, it provides
compilation error.

Initialization In 'for' loop the initialization once In while loop if initialization is


done is never repeated. done during condition checking,
then initialization is done each time
the loop iterate.

Iteration statement In 'for' loop iteration statement is In 'while' loop, the iteration
written at top, hence, executes only statement can be written anywhere
after all statements in loop are in the loop.
executed.

Example of for loop


#include <stdio.h>
int main () {
int x;
for( x = 1; x < 10; x = x + 1 ){
printf("Value: %dn", x);
}
return 0;
}
OutPut:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6

CKPCET, SURAT 10
PPS (3110003)

Value: 7
Value: 8
Value: 9
Example of while loop
#include <stdio.h>
int main () {
int x = 1;
/* Execute While Loop */
while( x < 10 ) {
printf("Value: %dn", x);
x++;
}
return 0;
}
Output
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
Value: 9

CKPCET, SURAT 11
PPS (3110003)

Q.9 Write a program that reads a value n and generates the following pattern:
.(CPU_WIN_2019)

1
123
12345
1234567
123456789
#include<stdio.h>
int main()
{
int i, j, k;
for (i=1;i<=5;i++)
{
for (j=i;j<5;j++)
{
printf(" ");
}
for (k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
return 0;
}
Q.10 Define general form of 1) do while loop 2) Nested if 3) goto (PPS_WIN_18)
The do-while loop is also called exit control loop because, in do-while loop, compiler will 1st
execute the statements, then check the condition, whether it is true or false.
Syntax of Do-While Loop

CKPCET, SURAT 12
PPS (3110003)

do
{
//Statements
}while(condition test);
A nested if in C is an if statement that is the target of another if statement. Nested if statements
means an if statement inside another if statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
The goto statement is a jump statement which jumps from one point to another point within a
function.
Syntax of goto statement
goto label;
..
.
label: statement;
Q.11 Explain switch case statement with example to read number between 1 to 7 and print
relatively day Sunday to Saturday. (PPS_WIN_18)
Switch statement tests the value of a variable and compares it with multiple cases.
General Syntax
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:

CKPCET, SURAT 13
PPS (3110003)

Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x;
#include <stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");

CKPCET, SURAT 14
PPS (3110003)

break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}

return 0;
}
Output
Enter week number(1-7): 1
Monday
Q.12 Write a program to find out an area of a circle.(CPU_WIN_2017)
#include<stdio.h>
int main()
{
float radius, area;
printf("\nEnter the radius of Circle : ");
scanf("%d", &radius);
area = 3.14 * radius * radius;
printf("\nArea of Circle : %f", area);
return (0);
}

CKPCET, SURAT 15
PPS (3110003)

Output :
Enter the radius of Circle : 2.0
Area of Circle : 6.14
Q.13 Write a program to print multiplication table of any number. .(CPU_WIN_2017)
int main()
{
int number, i = 1;
printf(" Enter the Number:");
scanf("%d", &number);
printf("Multiplication table of %d:\n ", number);
printf("--------------------------\n");
while (i <= 10)
{
printf(" %d x %d = %d \n ", number, i, number * i);
i++;
}
return 0;
}
Output:
Enter the Number:6
Multiplication table of 6:
--------------------------
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48

CKPCET, SURAT 16
PPS (3110003)

6 x 9 = 54
6 x 10 = 60

Q.14 Explain Entry Controlled Loop and Exit Controlled Loop with
flowchart.(CPU_WIN_2017) (CPU_SUM_2016)

1)Entry controlled loop - The loop which has a condition check at the entrance of the loop, the
loop executes only and only if the condition is satisfied is called as entry control loop.
EX-
• while loop
• For loop
2) Exit controlled loop - The loop which keeps on executing until a particular condition is satisfied
and when the condition is satisfied according to the criteria the loop exits, this is known as exit
controlled loop.
EX-
• While loop (during polling)
Q.14 Discuss general form of following decision – making statements. (CPU_WIN_2016)
(I) IF (II) SWITCH (III) GOTO
(1) The general form of a if statement is,
if(expression)

CKPCET, SURAT 17
PPS (3110003)

{
statement inside;
}
statement outside;
(2) Switch statement is a control statement that allows us to choose only one choice among the
many given choices.
The general form of switch statement is,
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}
(3) The goto statement allows us to transfer control of the program to the specified label.
Syntax of goto Statement
goto label;
... .. ...
... .. ...

CKPCET, SURAT 18
PPS (3110003)

label:
statement;
Q.15 List down three constructs for performing loop operations in C language. Write
general form of same. (CPU_WIN_2016) (CPU_WIN_2014)
'C' programming language provides us with three types of loop constructs:

1. The while loop

2. The do-while loop

3. The for loop

The basic format of while loop is as follows:

while (condition) {
statements;
}

The basic format of do while loop is as follows:

do {
statements
} while (expression);

The general structure of for loop is as follows:

for (initial value; condition; incrementation or decrementation )


{
statements;
}

Q.16 Develop a simple program to Add, subtract and multiply two numbers using switch
statement. . (CPU_WIN_2016) (CPU_SUM_2016)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");

CKPCET, SURAT 19
PPS (3110003)

printf("Enter the values of a & b: ");


scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
getch();
}
OUTPUT:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 20 15

CKPCET, SURAT 20
PPS (3110003)

Enter your Choice : 1


Sum of 20 and 15 is : 35
Q.17 Write a program to select and print the largest of the three nos. using nested-if-else
statement. (CPU_WIN_2015)
#include <stdio.h>
int main()
{
int A, B, C;
printf("Enter three numbers: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
else if (B >= A && B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
return 0;
}
Output:
Enter the numbers A, B and C: 2 8 1
8 is the largest number.
Q.18 Write a program to find the sum of first N odd numbers. (CPU_WIN_2015)
#include<stdio.h>
int main()
{
int i, number, Sum = 0;
printf("\n Please Enter the Maximum Limit Value : ");
scanf("%d", &number);
printf("\n Odd Numbers between 0 and %d are : ", number);
for(i = 1; i <= number; i++)

CKPCET, SURAT 21
PPS (3110003)

{
if ( i%2 != 0 )
{
printf("%d ", i);
Sum = Sum + i;
}
}
printf("\n \n The Sum of Odd Numbers from 1 to %d = %d", number, Sum);
return 0;
}
OUTPUT
Please Enter the Maximum Limit Value : 10
Odd Numbers between 0 and 10 are : 1 3 5 7 9
The Sum of Odd Numbers from 1 to 10 = 25
Q.19 Explain various types of loop available in C with example. (CPU_WIN_2015)
(CPU_WIN_2013)
Types of Loop
• while loop
• do...while loop
• for loop
While Loop
While loop is also called entry control loop because, in while loop, compiler will 1st check the
condition, whether it is true or false, if condition is true then execute the statements.
initialization;
while (condition)
{
----------
----------

inc/dec;
}

CKPCET, SURAT 22
PPS (3110003)

Example of While Loop


#include<stdio.h>
void main()
{
int a=1,num;
printf("Enter any number : ");
scanf("%d",&num);
while (a<=num)
{
printf("\nHello...!!");
a++;
}
}
Output :
Enter any number : 5
Hello...!!
Hello...!!
Hello...!!
Hello...!!
Hello...!!
Do-While Loop
The do-while loop is also called exit control loop because, in do-while loop, compiler will 1st
execute the statements, then check the condition, whether it is true or false.
Syntax of Do-While Loop
initialization;
do
{
----------
----------
inc/dec;

CKPCET, SURAT 23
PPS (3110003)

} while (condition);
Example of Do-While Loop
#include<stdio.h>
void main()
{
int a=1,num;
printf("Enter any number : ");
scanf("%d",&num);
do
{
printf("\nHello...!!");
a++;
} while (a<=num);
}
Output :
Enter any number : 5
Hello...!!
Hello...!!
Hello...!!
Hello...!!
Hello...!!
For Loop
In for loop we put initialization, contidion and increment/decrement all together. Initialization will
be done once at the beginning of loop. Then, the condition is checked by the compiler. If the
condition is false, for loop is terminated. But, if condition is true then, the statements are executed
until condition is false.
Syntax of For Loop
for (initialization;condition;inc/dec)
{
----------
----------

CKPCET, SURAT 24
PPS (3110003)

Example of For Loop


#include<stdio.h>
void main()
{
int a,num;

printf("Enter any number : ");


scanf("%d",&num);
for (a=1;a<=num;a++)
printf("\nHello...!!");
}
Output :
Enter any number : 5
Hello...!!
Hello...!!
Hello...!!
Hello...!!
Hello...!!
Q. 20 Write a program to print the following pattern. (CPU_WIN_2015)
*
**
***
****
#include<stdio.h>
int main() {
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);

CKPCET, SURAT 25
PPS (3110003)

for (i=1; i<=rows; ++i) {


for (j=1; j<=i; ++j)
{ printf("* "); }
printf("\n");
}
return 0;
}

Q.21 Write a C Program to check whether the given number is prime or not.
(CPU_WIN_2014)
#include <stdio.h>
void main()
{
int num,i,ctr=0;
printf("Input a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++)
{
if(num % i==0)
{
ctr++;
break;
}
}
if(ctr==0 && num!= 1)
printf("%d is a prime number.\n",num);
else
printf("%d is not a prime number",num);
}
Output:

CKPCET, SURAT 26
PPS (3110003)

Input a number: 13
13 is a prime number.

Q.22 Write a program to print the following pattern (CPU_WIN_2014)


1
2 2
3 3 3
4 4 4 4
#include<stdio.h>
#include<conio.h>
int main()
{
int r,c,sp,n=4;
for(r=1; r<=4; r++)
{
for(sp=1; sp<=n; sp++)
printf(" ");
for(c=1; c<=r; c++)
{
printf("%d",r);
printf(" ");
}
printf("\n");
n=n-1;
}
getch();
return 0;
}

CKPCET, SURAT 27
PPS (3110003)

Q.23 Write a program to print the following pattern (CPU_WIN_2013)


1
01
101
0101
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<i;j++)
{
if((i+j)%2==0)
{
printf("0");
}
else
{
printf("1");
}
}
printf("\n");
}
getch();

CKPCET, SURAT 28
PPS (3110003)

Q. 24 Write a program to select and print the largest of the three nos. using nested-if-else
statement.
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);

if (n1 >= n2) {


if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
} else {
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}

return 0;
}

}
Q.25 Explain various types of loop available in C with example. Compare while & do-while
loop(CPU_SUM_2014,2015,)(PPS_SUM_2019)
A Loop executes the sequence of statements many times until the stated condition becomes false.
A loop consists of two parts, a body of a loop and a control statement.
There are 3 types of Loop in C language, namely:
1. while loop
2. for loop

CKPCET, SURAT 29
PPS (3110003)

3. do while loop
while loop
while loop can be addressed as an entry control loop. It is completed in 3 steps.
1. Variable initialization.(e.g int x = 0;)
2. condition(e.g while(x <= 10))
3. Variable increment or decrement ( x++ or x-- or x = x + 2 )
Syntax :
variable initialization;
while(condition)
{
statements;
variable increment or decrement;
}
Example: Program to print first 10 natural numbers
#include<stdio.h>
void main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t", x);
/* below statement means, do x = x+1, increment x by 1*/
x++;
}
}
Output: 1 2 3 4 5 6 7 8 9 10
For loop
for loop is used to execute a set of statements repeatedly until a particular condition is satisfied.
We can say it is an open-ended loop. General format is,

CKPCET, SURAT 30
PPS (3110003)

for(initialization; condition; increment/decrement)


{
statement-block;
}
The for loop is executed as follows:
1. It first evaluates the initialization code.
2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and again follows from step 2.
5. When the condition expression becomes false, it exits the loop.
Example: Program to print first 10 natural numbers
#include<stdio.h>
void main( )
{
int x;
for(x = 1; x <= 10; x++)
{
printf("%d\t", x);
}
}
Output: 1 2 3 4 5 6 7 8 9 10
do while loop ( Q. Explain bottom tested loop)(CPU_SUM_2017)
In some situations it is necessary to execute body of the loop before testing the condition. Such
situations can be handled with the help of do-while loop. do statement evaluates the body of the
loop first and at the end, the condition is checked using while statement. It means that the body of
the loop will be executed at least once, even though the starting condition inside while is initialized
to be false. General syntax is,
do
{
.....
.....
}
while(condition)

CKPCET, SURAT 31
PPS (3110003)

Example: Program to print first 10 multiples of 5.


#include<stdio.h>
void main()
{
int a, i;
a = 5;
i = 1;
do
{
printf("%d\t", a*i);
i++;
}
while(i <= 10);
}
Output: 5 10 15 20 25 30 35 40 45 50

BASIS FOR
WHILE DO-WHILE
COMPARISON

General Form while ( condition) { do{


statements; //body of loop .
} statements; // body of loop.
.
} while( Condition );

Controlling Condition In 'while' loop the controlling In 'do-while' loop the controlling
condition appears at the start of the condition appears at the end of the
loop. loop.

Iterations The iterations do not occur if, the The iteration occurs at least once
condition at the first iteration, even if the condition is false at the
appears false. first iteration.

Alternate name Entry-controlled loop Exit-controlled loop

CKPCET, SURAT 32
PPS (3110003)

BASIS FOR
WHILE DO-WHILE
COMPARISON

Semi-colon Not used Used at the end of the loop

Q. 26 Write a program & algorithm perform addition, multiplication, subtraction and


division with switch statement. (CPU_SUM_2014,16)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
//clrscr();
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;

CKPCET, SURAT 33
PPS (3110003)

case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
getch();
}

CKPCET, SURAT 34
PPS (3110003)

Q. 27 Write a program to print the following pattern. (CPU_SUM_2014)


*
**
***
****

#include <stdio.h>
#include <conio.h>
void main() {
int i,j,k,t=0;
clrscr();
for (i=1; i<=4; i++) {
for (k=t; k<4; k++) {
printf(" ");
}
for (j=0; j< i; j++) {
printf(" * ");
t = t + 1;
}
printf("\n");
}
getch();
}
Q. 28 Write a C program to convert Celsius to Fahrenheit and vice versa.
(CPU_SUM_2016,18)
int main()
{

float fh,cl;
int choice;

CKPCET, SURAT 35
PPS (3110003)

printf("\n1: Convert temperature from Fahrenheit to Celsius.");


printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);

if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}
Output:
First Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 1

Enter temperature in Fahrenheit: 98.6

CKPCET, SURAT 36
PPS (3110003)

Temperature in Celsius: 37.00

Second Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 2

Enter temperature in Celsius: 37.0


Temperature in Fahrenheit: 98.60

Q. 29 Write a C program to display prime number between 1 to 100 (CPU_SUM_2016)


#include<stdio.h>
int main(){
int numbr,k,remark;
printf(" The prime numbers between 1 and 100 : \n");
for(numbr=2;numbr<=100;++numbr)
{
remark=0;
for(k=2;k<=numbr/2;k++){
if((numbr % k) == 0){
remark++;
break;
}
}
if(remark==0)
printf("\n %d ",numbr);
}
return 0;
}
Output:

CKPCET, SURAT 37
PPS (3110003)

The prime numbers between 1 and 100 :


2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Q. 30 Write a program to accept start number and end number from the user and print all
the numbers in the range. (CPU_SUM_2017)
#include <stdio.h>
void main()
{
int num,m,n;
clrscr();
printf ("Enter range (m, n ) : ") ;
scanf ("%d %d", &m, &n);
for (num = m; num <= n; num++)
{
printf("%d ", num);
}
getch();
}
Output:
Enter range (m,n) : 5 9
56789

Q. 31 Explain switch case in ‘C’ with the help of an example. (CPU_SUM_2017)

The switch case statement is used when we have multiple options and we need to perform a
different task for each option.

C – Switch Case Statement

Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.

switch (variable or an integer expression)

case constant:

CKPCET, SURAT 38
PPS (3110003)

//C Statements;

case constant:

//C Statements;

default:

//C Statements;

}
Example:

#include <stdio.h>

int main()

int num=2;

switch(num+2)

case 1:

printf("Case1: Value is: %d", num);

case 2:

printf("Case1: Value is: %d", num);

case 3:

printf("Case1: Value is: %d", num);

default:

printf("Default: Value is: %d", num);

return 0;

CKPCET, SURAT 39
PPS (3110003)

Output:

Default: value is: 2

Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where num
value is 2 and after addition the expression resulted 4. Since there is no case defined with value 4
the default case is executed.

Q. 32 Write a program in ‘C’ to print the following pattern (CPU_SUM_2017)

23

456

78910

#include<stdio.h>

#include<conio.h>

void main()

int i,j, k=1;

clrscr();

for(i=1;i<=5;i++)

for(j=1;j<i;j++)

printf("%d",k);

k++

printf("\n");

CKPCET, SURAT 40
PPS (3110003)

getch();

Q. 33 Write a C program to read 10 numbers from user and store them in an array. Display
Sum, Minimum and Average of the numbers. (CPU_SUM_2018)
#include <stdio.h>
int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
total+=array[i];/* this means total=total+array[i]; */
}
average = total / num;
printf("\n Sum of all numbers = %.2f\n", total);

CKPCET, SURAT 41
PPS (3110003)

printf("\n Average of all input numbers = %.2f\n", average);


}
Q. 34 Write C program to find area and perimeter of rectangle. (CPU_SUM_2019)
#include<stdio.h>
int main() {
int length, breadth, area,perimeter;

printf("\nEnter the Length of Rectangle : ");


scanf("%d", &length);

printf("\nEnter the Breadth of Rectangle : ");


scanf("%d", &breadth);

area = length * breadth;

perimeter=2*(length+breadth);

printf("\nArea of Rectangle : %d", area);


printf("\nPerimeter of Rectangle : %d", perimeter);
return (0);
}
Output :
Enter the Length of Rectangle : 10
Enter the Breadth of Rectangle : 4
Area of Rectangle : 40
Perimeter of Rectangle : 28
Q. 35 Write a program to add first n numbers. Get value of n from user. (CPU_SUM_2019)
#include <stdio.h>
int main() {
int n,

CKPCET, SURAT 42
PPS (3110003)

printf(“Enter the no:”);


scanf(“%d”,n);
int sum=0, s=0;
for(int i = 1; i< n; i++){
for(int j= 1; j<i;j++ ){
s+= j;
}
sum += s;
}
printf("the sum of sum of natural number till %d is %d", n,sum);
return 0;
}
Q. 36 Write a C program to display given pattern (CPU_SUM_2019)
1
12
123
1234
12345
#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");

CKPCET, SURAT 43
PPS (3110003)

return 0;
}
Q. 37 Write a program to reverse a given number. (PPS_SUM_2019)
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

Output

Enter an integer: 2345


Reversed number = 5432

Q. 38 Write a program to find 1+1/2+1/3+1/4+....+1/n. (PPS_SUM_2019)


#include <stdio.h>
void main()
{
double number, sum = 0, i;

CKPCET, SURAT 44
PPS (3110003)

printf("\n enter the number ");


scanf("%lf", &number);
for (i = 1; i <= number; i++)
{
sum = sum + (1 / i);
if (i == 1)
printf("\n 1 +");
else if (i == number)
printf(" (1 / %lf)", i);
else
printf(" (1 / %lf) + ", i);
}
printf("\n The sum of the given series is %.2lf", sum);
}

Q. 39 Write a program to check whether entered character is vowel or not?


(PPS_SUM_2019)
#include <stdio.h>
int main() {
char c;
int lowercase, uppercase;
printf("Enter an alphabet: ");
scanf("%c", &c);
// evaluates to 1 if variable c is lowercase
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 if variable c is uppercase
uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 if c is either lowercase or uppercase
if (lowercase || uppercase)
printf("%c is a vowel.", c);

CKPCET, SURAT 45
PPS (3110003)

else
printf("%c is a consonant.", c);
return 0;
}

Q. 40 Write a program to print all Armstrong numbers in a given range. Armstrong


number is equal to sum of cubes of its individual digits. For example 153 = 1^3 +
5^3 + 3^3. So, 153 is Armstrong number. (PPS_SUM_2019)
#include <stdio.h>
int check_armstrong(int);
int power(int, int);
int main ()
{
int c, a, b;

printf("Input two integers\n");


scanf("%d%d", &a, &b);
for (c = a; c <= b; c++)
if (check_armstrong(c) == 1)
printf("%d\n", c);
return 0;
}
int check_armstrong(int n) {
long long sum = 0, t;
int remainder, digits = 0;
t = n;
while (t != 0) {
digits++;
t = t/10;
}

CKPCET, SURAT 46
PPS (3110003)

t = n;
while (t != 0) {
remainder = t%10;
sum = sum + power(remainder, digits);
t = t/10;
}
if (n == sum)
return 1;
else
return 0;
}
int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}

Q. 41 Write a program to display transpose of given 3*3 matrix. (PPS_SUM_2019)


#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// Assigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);

CKPCET, SURAT 47
PPS (3110003)

scanf("%d", &a[i][j]);
}

// Displaying the matrix a[][]


printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
// Finding the transpose of matrix a
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
MCQ
(CPU_WIN_2019)
Q. In which of the following the loop is executed at least once?
(a) while (b) for (c) do – while (d) if

CKPCET, SURAT 48
PPS (3110003)

(CPU_WIN_2017)
Q. Which one is the correct syntax of for loop?
a)for(initialization,condition,increment/decrement)
b)for(initialization;condition;increment/decrement)
c)for(condition,condition,increment/decrement)
d) for(condition;initialization;increment/decrement)

Q. Break statement is used for


a. Quit a program b. Quit the current iteration c. Both a and b
d. None of the above

(CPU_WIN_2016)
Q. Continue statement
(a) Breaks loop and goes to next statement after loop.
(b) does not break loop but starts new iteration. (c) exits the program.
(d) Starts from beginning of program.

Q.In which of the following the loop is executed at least once?


(a) while (b) do - while (c) for (d) if

Q.How many times following loop will be executed.


main()
{
int i = 32766;
while (i<= 32767)
{
printf(‘%d\n’,i);
i = i+ 1;
} }

CKPCET, SURAT 49
PPS (3110003)

(a) 2 times. (b) 1 times. (c) infinite times. (d) loop will not be executed.

(CPU_SUM_2014)
Q. Which of following loop is executed at least once?
a) do-while b) for c) if d) while

Q. What will be printed if we type the statement printf(“%d\n”,’d’);


a) 0 b) 100 c) error d) d

Q. Which among the following is a unconditional control structure.


a) goto b) for c) do-while d) if-else

(CPU_SUM_2015)
Q. The first expression in a for… loop is
a) Step value of loop b) Condition statement c) Value of the counter variable d) None of the
above

Q. Continue statement used for


a) To continue to the next line of code b) To stop the current iteration and begin the next
iteration from the beginning c) To handle run time error d) None of above

Q. What will be the output of following program


#include main()
{
int x,y = 10;
x = y * NULL;
printf(\"%d\",x);
}
a) error b) 10 c) 0 d) garbage value

CKPCET, SURAT 50
PPS (3110003)

(CPU_SUM_2016)
Q. Which are not looping structures?
(a) For loop (b) While loop (c) Do...while loop (d) if…else

Q. How many times the following code prints the string “hello” for(i=1;i<=50;i++);
printf(“Hello”);
(a) 1 (b) 50 (c) Zero (d) None of them

(CPU_SUM_2017)
Q. Continue statement
(a) Breaks loop and goes to next statement after loop. (b) Does not break loop but starts new
iteration. (c) Exits the program. (d) Starts from beginning of program.

Q. Which of the following loop is executed at least once


(a) for loop (b) while loop (c) do while loop (d) None of the above

Q. What is the output of the following code:


void main()
{
int i;
for(i=1;i<=10;i++);
printf(“%d\n”,i);
}
(a) 10 (b) 1 to 10 (c) 11 (d) None of the above

Q. What is the output of the following code:


void main()
{
int i;
for(i=65;i<70;i++)

CKPCET, SURAT 51
PPS (3110003)

printf(“%c,”,i);
}
(a)65,66,67,68,69,70 (b)a,b,c,d,e, (c)A,B,C,D,E, (d)A,B,C,D,E

Q. What is the output of following code:


void main()
{
int i=5;
switch(i)
{
case 3: printf(“three”);
case 4: printf(“four”);
case 5: printf(“five”);
case 6: printf(“six”);break;
case 7: printf(“seven”);
default: printf(“default”);
}
}
(a)five (b)fivesixsevendefault (c)fivesix (d) None of the above

(CPU_SUM_2018)
Q. For loop is _________.
(a)Function Controlled Loop (b)Entry Controlled Loop (c) Exit Controlled Loop (d) None
of these

(CPU_SUM_2019)
Q. How many times following loop will be rotated?
int a = 10, b = 1;
while(a>b)
{ a--; b++; }

CKPCET, SURAT 52
PPS (3110003)

(a) 5 (b) 4 (c) 10 (d) 1

Q. Switch case statement cannot be applicable on _________ data type.


(a) int (b) enumerated (c) char (d) float

CKPCET, SURAT 53

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy