0% found this document useful (0 votes)
25 views69 pages

3 Chapter 3 CS - Vinit

This document discusses different types of control structures in C programming including selection statements like if, if-else and switch case statements as well as iterative statements like for, while and do-while loops. Examples are provided to demonstrate if/else, nested if-else, else-if ladder and switch case structures.

Uploaded by

Arya Vinod
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)
25 views69 pages

3 Chapter 3 CS - Vinit

This document discusses different types of control structures in C programming including selection statements like if, if-else and switch case statements as well as iterative statements like for, while and do-while loops. Examples are provided to demonstrate if/else, nested if-else, else-if ladder and switch case structures.

Uploaded by

Arya Vinod
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/ 69

C-Programming Control Structur

Chapter 3: Control Structure

• In programming we need to sometimes control the flow of operation other than just the sequential
statements. In this case we need the control statements.

• Control statements are of two types


1. Selection statements
– Selection statements are used to perform the operations based on a particular condition i.e.
if a condition is true performing one task else perform another task.

2. Iterative statements (Looping)


– Iterative statements (Looping) are used to perform certain operation repetitively for certain
number of times. In C we have three iterative statements viz. for loop, while loop and do-
while loop.

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 1
C-Programming Control Structur

Selection: It is decision making statement


• Sometimes, a program requires checking of certain conditions in program execution.
• C provides various key condition statements to check condition and execute statements according
conditional criteria.

Selection Statement is of following types.


1. If Statement
2. If – else Statement
3. Nested if- else Statement
4. Switch – Case

If Statement

Syntax

if (condition)
{
Statements1;
}

The condition given with the if statement is true. Then statements1 are executed in this case.

• If the condition specified in the if statements is false then the statements named as statements1
are not executed in this case.

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 2
C-Programming Control Structur

If Else Statement

Syntax

if (condition)
{
Statements-1;
}
else
{
Statements-2;
}

• The set of statements named as statements 1 in the above syntax are executed if the condition
given with the if statement is true.
• If the condition specified in the if statements is false then the statements named as statements2 in
the above syntax are executed.

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 3
C-Programming Control Structur

Example : Write a program to check entered number is even or odd.

#include <stdio.h>
#include <conio.h>
void main()
{
int n;
printf(“Enter a number\n”);
scanf(“%d”, &n);
if(n%2==0)
{
printf(“%d is even number”,n);
}
else
{
printf(“%d is odd number”,n);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 4
C-Programming Control Structur

Nested if-else

• In some cases we have to check multiple cases of a particular condition. In such cases we have to
use an nested if-else.
Syntax:

if (condition-1)
{
if(condition-2)
statement1;
else
statement2;
}
else
{
if(condition-3)
statement13;
else
statement4;
}

• In this case the first condition is checked, if it is true then statements inside that if block are
executed. But if the condition is false it goes to the else statement. Again there is a condition with
if; the statements are executed if this second condition is true. Else it again goes to the else and
again checks the condition associated with this if statement.

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 5
C-Programming Control Structur

Example : Write a program to compare three numbers

#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,n3;
printf(“Enter three numbers\n”);
scanf(“%d, %d, %d”, &n1, &n2, &n3);
if(n1>n2)
{
if(n1>n3)
printf(“The largest number is %d”,n1);
else
printf(“The largest number is %d”,n3);
}
else
{
if(n2>n3)
printf(“The largest number is %d”,n2);
else
printf(“The largest number is %d”,n3);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 6
C-Programming Control Structur

Else if ladder

• If condtion1 is true then statement1 will be executed otherwise condition2 will be check if it is
true then statment2 will be executed otherwise statement 3 will be executed.

Syntax:

if(condition1)
{
Statment1;
}
else if(condition2)
{
Statment2;
}
else
{
Statment3;
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 7
C-Programming Control Structur

Example : Write a program to display the class according to marks scored.

Marks Class
70-100 Distinction
60-69 First Class
40-59 Second Class
0-39 Fail

#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
printf(“Enter the marks\n”);
scanf(“%d, &marks);
if(marks>=70)
{
printf(“Distinction”);
}
elseif(marks>=60)
{
printf(“First Class”);
}
elseif(marks>=40)
{
printf(“Second Class”);
}
else
{
printf(“Fail”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 8
C-Programming Control Structur

Switch-Case Selective Statement

• The statement consist of one or more case statements followed by colon(:).

• Value in the case statement must be integer or character and value must be unique.

• If case value did not matched then default block gets executed.

• Using switch-case the if-else ladder can be implemented in a much better way.

Syntax

switch (variable or condition)

case value1:

Statement1;

Break;

case value2:

Statement2;

Break;

case value3:

Statement3;

Break;

default:

Statement default;

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 9
C-Programming Control Structur

switch Ladder of if else

The keyword switch is used only once. The keyword if and else are used many time

The keyword case is used many times. That The logical OR(I I)operator used many times to
is because many cases are to be written. manage the condition.

The break statement is to used to take the When all the condition expressions goes false the
control out of switch. If all the comparisons final else part is used to take the final action.
are a failure then default part comes in
picture.

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 10
C-Programming Control Structur

Example : Write an program to implement calculator with following operation. 6


Marks [Dec-14].
i) Add two numbers iii) Division two numbers
ii) Subtract two numbers iv) Multiply two numbers

#include <stdio.h>
#include <conio.h>
void main()
{
int ch,a=10,b=20;
float ans;
printf("\nMenu");
printf("\n1.ADD\n2.Sub\n3.Mul\n4.Div");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
ans = a + b;
printf("\nRes is %f",ans);
break;
case 2:
ans = a - b;
printf("\nRes is %f", ans);
break;
case 3:
ans = a * b;
printf("\nRes is %f", ans);
break;
case 4:
ans = a / b;
printf("\nRes is %f", ans);
break;
default:
printf("\nWrong ch");
}//end of switch
getch();
return 0;
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 11
C-Programming Control Structur

Examlpe: Write a Program to read month number as input and display the month in
words.

#include <stdio.h>
#include <conio.h>
void main()
{
int m ;
clrscr() ;
printf("Enter a month number: ");
scanf("%d" , &m) ;
printf("Month in words is: ");
switch(m)
{
case 1: printf("January");
break ;
case 2: printf("February");
break ;
case 3: printf("March");
break ;
case 4: printf("April");
break ;
case 5: printf("May");
break ;
case 6: printf("June");
break ;
case 7: printf("July");
break ;
case 8: printf("August");
break ;
case 9: printf("September");
break ;
case 10: printf("October");
break ;

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 12
C-Programming Control Structur

FOR LOOP

– For is a iterative statement.


– It is used to repeat a set of statements number of times.

Syntax

for (initializations; condition; increment / decrement)

Statement;

• Initialization statement is executed first. They are used to initialize the value of the variables.
• The second step is checking the condition. If condition is true then statement will be excited If the
condition is false the execution of the loop will be terminated.
• The third step is to execute all the statements inside the curly braces. These statements will be
executed sequentially.
• The fourth step is the increment / decrement or updating operations.

Example: Program to display hello 10 times.

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


{
printf(“Hello\n”);
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 13
C-Programming Control Structur

Output

Hello
Hello
Hello
.
.

Hello

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 14
C-Programming Control Structur

Example: Write a program to find the sum of first n natural numbers where
n is entered by user. Note: 1,2,3... are called natural numbers.

#include <stdio.h>
#include <conio.h>
void main()
{
int n,i, sum=0;
printf("Enter the value of n.\n");
scanf("%d",&n);
for(i=1;i<=n;i++) //for loop terminates if count>n
{
sum=sum+i;
}
printf("Sum=%d",sum);
return 0;
}

Output:-

Enter the value of n.


5
Sum=15

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 15
C-Programming Control Structur

Example: WAP to find Factorial of a user entered number

#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fact = fact*i;
}
printf("Factorial of %d = %d\n", n, fact);
return 0;
}

Output:-

Enter a number to calculate it's factorial

Factorial of 4 = 24

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 16
C-Programming Control Structur

Example: Factors of a Positive Integer

#include <stdio.h>
#include <conio.h>
void main()
{
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0)
{
printf("%d ", i);
}
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 17
C-Programming Control Structur

While loop

• While and do-while loops are also for iterative operation.


• The while loop is used when you want to execute a block of code repeatedly with checked
condition before making iteration.

Syntax:

While (condition)
{

Statements;

• The operation of the while loop is such that, first the condition is checked. If the condition is
true, then the statements are executed. Once the statements are executed, the condition is again
checked and this keeps on repeating, until the condition is false. If the condition is false, the
statements insides the loop is not executed, instead the control directly comes out of the loop.

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 18
C-Programming Control Structur

Example: Write a Program to Reverse an Integer entered by user.

#include <stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n=n/10;
}
printf("Reversed Number = %d", reverse);
return 0;
}

Output:-

Enter an integer: 2345


Reversed Number = 5432

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 19
C-Programming Control Structur

Example: Write a program to check whether a number is palindrome or not.


[May-14] 10M

#include <stdio.h>
#include <conio.h>
int main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
temp=n/10;
}
if(reverse==temp)
{
printf("%d is a palindrome.",n);
}
else
{
printf("%d is not a palindrome.",n);
}
return 0;
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 20
C-Programming Control Structur

Example: WAP to check entered number is Armstrong number

(For example 371 is an armstrong number as 33 + 73 + 13 = 371.)

#include <stdio.h>
int main()
{
int num, sum = 0, temp, rem;
printf("Enter an integer\n");
scanf("%d",&num);
temp = num;
while(num != 0 )
{
rem = num%10;
sum = sum + rem *rem *rem;
num = num/10;
}
if (temp == sum)
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong number.\n");
return 0;
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 21
C-Programming Control Structur

DO-WHILE LOOPS

• First the code block is executed and then condition is evaluated.


Syntax
do
{
Statements;
} while (condition);

• First the statements are executed and then the condition is checked.
• If the condition is true the statements are executed again. If the condition is false, the statements
are not executed again.

Example:

i=10;
do
{
printf(“%d”);
i++;
} While (i! =0);

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 22
C-Programming Control Structur

Differences between while and do-while loop

while loop do-while loop


1 Syntax 1 Syntax
while (condition) do
{ {
- -
Statements; Statements;
- -
- -
} }while (condition);
2 This is called as a entry controlled loop. 2 This is called as exit controlled
as the entry inside the loop is possible loop,
only if the condition is true. as the entry inside this loop is sure
i.e. no condition is checked to enter
inside the loop. the exit is possible
only if the condition is false.
3 If the condition is false for the first time, 3 Even if the condition is false for the
the control will never enter into the loop. first time the control will enter into
the loop. Hence the statements
inside the loop will be executed at
least once.
4 There is no semicolon (;) after the 4 There is a semicolon (;) after the
condition in the syntax of the while loop. condition in the syntax of the do-
while loop.
5 Example 5 Example
i=1; i=1;
while(i<=5) do
{ {
Printf(“%d”,i); Printf(“%d”,i);
} }while(i<=5);

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 23
C-Programming Control Structur

Example: Explain the difference between for, while and do-while loop.

[May-13] 3-Marks.

No.1 For While do-while

Syntax Syntax Syntax


1.
for (exp1; exp2; exp3) while(condition) do
{ { {
Statement statement statement
} } }while(condition);

It is entry checking loop. It is entry checking It is exit checking


2.
loop. loop.

Initialisation (exp1) and update condition is part of condition is part of


3.
(exp2) are part of the syntax. statement statement

1. If the exp2 is false in the If the condition is Since the statement


4.
first iteration then statement false in the first part is executed
part will not executed. iteration then first and then the
statement part will condition is
not be executed. checked so
statement will be
always executed at
least once.

Example Example Example


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

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 24
C-Programming Control Structur

printf(“%d”,i); while(i<=5) do
will result into output 12345. printf(“%d”,i++); printf(“%d”,i++);
while(i<=5);
will result into
output 12345. will result into
output 12

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 25
C-Programming Control Structur

Pattern program

Example: Write a program to display following pattern

*
**
***
****

#include <stdio.h>
#include <conio.h>
void main()
{
int i , j , n ;
printf("Enter the number of lines: ") ;
scanf("%d" , &n) ;
printf("Required pattern is as follows: \n") ;
for(i=1 ; i<=n ; i++)
{
for(j=1; j<=i; j++)
{
printf("*”) ;
}
printf("\n") ;
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 26
C-Programming Control Structur

Example: Write a program to display following pattern


1
12
123
1234

#include <stdio.h>
#include <conio.h>
void main()
{
int i , j , n ;
printf("Enter the number of lines: ") ;
scanf("%d" , &n) ;
printf("Required pattern is as follows: \n") ;
for(i=1 ; i<=n ; i++)
{
for(j=1 ; j<=i ; j++)
{
printf("%d " , j) ;
}
printf("\n") ;
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 27
C-Programming Control Structur

Program: write a program to display the following pattern.

1
22
333
4444
55555

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j ;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,i);
}
printf(“\n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 28
C-Programming Control Structur

Write a program to display following pattern


A
AB
ABC
ABCD
ABCDE

#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , n ;
for(i=1 ; i<=5 ; i++)
{
for(j=1 ; j<=i ; j++)
{
printf("%c " , j+64) ;
}
printf("\n") ;
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 29
C-Programming Control Structur

Write a program to display following pattern


1
23
456
7 8 9 10

#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , k=1;
clrscr() ;
printf("Required pattern is as follows: \n") ;
for(i=1 ; i<=4 ; i++)
{
for(j=1 ; j<=i ; j++)
{
printf("%d " ,k) ;
k++;
}
printf("\n") ;
}
getch() ;
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 30
C-Programming Control Structur

Program: write a program to generate the following patterns.


5
44
333
2222
11111

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k=5;
for(i=1;i<=5;i++)
{
for(j=1; j<=i; j++)
{
printf( “%d”, k);
}
printf(“\n”);
k--;
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 31
C-Programming Control Structur

Program: Write a program display following pattern.


****
***
**
*

#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , n ;
clrscr() ;
printf("Enter the number of lines: ") ;
scanf("%d" , &n) ;
printf("Required pattern is as follows: \n") ;
for(i=n ; i>=1 ; i--)
{
for(j=1 ; j<=i ; j++)
{
printf("*") ;
}
printf("\n") ;
}
getch() ;
}
Output

Enter the number of lines: 4


Required pattern is as follows:

****
***
**
*

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 32
C-Programming Control Structur

Program: Write a program display following pattern.


1234
123
12
1

#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , n ;
clrscr() ;
printf("Enter the number of lines: ") ;
scanf("%d" , &n) ;
printf("Required pattern is as follows: \n") ;
for(i=n ; i>=1 ; i--)
{
for(j=1 ; j<=i ; j++)
{
printf("%d " , j) ;
}
printf("\n") ;
}
getch() ;
}

Output:

Enter the number of lines: 3


Required pattern is as follows:
1234
123
12
1

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 33
C-Programming Control Structur

Program: Write a program to display the following asking the user for the number
of lines in the upper half.

***

*****

*******

*********

********

*****

***

#include<stdio.h>
#include<conio.h>
void main
{
int i, j, n;
clrscr();
printf(“Enter the number of lines in the upper half:”);
scanf (“%d”,&n);
for (i=1;i<=n;i++)
{
for(j=1;j<=n-I;j++)
{
printf(“ ”);
}
for(j=1;j<=2*i-1;j++)
{
printf(“* ”);
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 34
C-Programming Control Structur

for(i=n-1;i>=1;i--)
{
printf(“ ”);
for(j=1;j<=2*i-1;j++)
{
printf(“* ”);
}
printf(“\n ”);
}
getch();
}

Output

Enter the number of lines in the upper half: 5


*
***
*****
*******
*********
********
*****
***
*

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 35
C-Programming Control Structur

Write a program to display the following asking the user for the number of lines.

1
121
12321
1234321
123454321
12345654321

#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , n;
clrscr();
printf(“enter the number of lines”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(“ ”);
}
for(j=1;j<=i; j++)
{
printf(“%d”,j);
}
for(j=i-1;j>=1;j--)
{
printf(“%d”,j);
}
printf(“\n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 36
C-Programming Control Structur

Output

enter the number of lines : 6


1
121
12321
1234321
123454321
12345654321

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 37
C-Programming Control Structur

Write a program to display the following asking the user for the number of lines.
1
232
34543
4567654
567858765

#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , n, k;
clrscr();
printf(“enter the number of lines”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(“ ”);
}
k=i;
for(j=1;j<=i; j++)
{
printf(“%d”,k);
k--;
}
k=2*i-2;
for(j= 1;j<=i ;j++)
{
printf(“%d”,k);
k--;
}
printf(“\n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 38
C-Programming Control Structur

Program: write a program to display the following pattern.


1
21A
321AB
4321ABC
54321ABCD

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf(“enter the number of lines”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(“ ”);
}
for(j=i;j>=1;j--)
{
printf(“%d”,j);
}
for(j=1;j<= i – 1; j++)
{
printf(“%c”,(64+j));
}
printf(“\n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 39
C-Programming Control Structur

Output

Enter the number of lines: 5


1
21A
321AB
4321ABC
54321ABCD

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 40
C-Programming Control Structur

Program: Write a program to display the following pattern


1
10
101
1010
10101

#include<stdio.h>
#include<conio.h>
void main
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
// For every odd column print 1
if(j % 2 == 1)
{
printf("1");
}
else
{
printf("0");
}
}

printf("\n");
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 41
C-Programming Control Structur

Program: Write a program to display following pattern


0
0 1
0 1 0
0 1 0 1
0 1 0 1 0

#include<stdio.h>
void main()
{
int i,j,k;
for(i=0 ; i<=4 ; i++)
{
for(j=4 ; j > i ; j--)
{
printf(" ");
}
for(j=0; j<=i ; j++)
{
if(j%2 == 0)
printf("0");
else
printf("1");
}
printf("\n");
}
return 0;
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 42
C-Programming Control Structur

Program: Write a program to display the following pattern


5432*
543*1
54*21
5*321
*4321

#include<stdio.h>
#include<conio.h>
void main
{
int i, j, n;
clrscr();
for (i=1;i<=5;i++)
{
for(j=5;j>=1;j--)
{
If(i==j)
Printf(“*”);
else
Printf(“%d”,j);
}
printf(“\n”);
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 43
C-Programming Control Structur

Program: Write a program to display the following patterns.

ABCD
ABC
AB
A

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for (i=1;i<=4;i++)
{
for(j=1;j<=i-1;j++)
{
printf(“ “);
}
for (j=1;j<=5-i;j++)
{
printf(“%c”,(char)(j+64));
}
printf(“\n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 44
C-Programming Control Structur

Program: Write a program to display the following patterns.

1
12A
123AB
1234ABC

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<4-i;j++)
printf(“ “);
}
for(j=1;j<=i;j++)
{
printf(“%d”,j);
}
for(j=1;j<=i-1;j++)
{
printf(“%c”,(char)(j+64);
}
printf(“%n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 45
C-Programming Control Structur

Program: Write a program to print following pattern for N lines


5
54
543
5432
54321

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)
{
printf(“ ”);
}
for(j=1,k=5;j<=i;j++)
{
printf(“%d”, k--)’
}
printf(“\n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 46
C-Programming Control Structur

Program: Write a program to print the following pattern.

A
BB
CCC
DDDD

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
printf(“Enter the number of lines:”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(“ ”);
}
for(j=1;j<=i;j++)
{
printf(“%c”, (j+64));
}
printf(“\n”);
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 47
C-Programming Control Structur

Program: Write a Program to convert decimal to binary form.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n, i;
clrscr();
printf(“Enter a number:”);
scanf(“%d”, &n);
printf(“Binary form is:”);
for(i=15;i>=0;i--)
{
printf(“%d”, n(int) pow(2,i));
n=n%(int)(pow(2,i));
}
getch();
}

Output 1:

Enter a number:12
Binary form is:0000000000001100

Output 2:

Enter a number:8
Binary form is:0000000000001000

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 48
C-Programming Control Structur

Program: Write a program to generate the following patterns.

1
21
123
4321
12345

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
for(i=1;i<=5;i++)
{
if(i%2!=0)
{
for(j=1;j<=i;j++)
printf(“%d”, j);
}
else
{
for(j=0;j>=1;j--)
printf(“%d”, j);
}
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 49
C-Programming Control Structur

Write a program to generate following patterns.

A
CB
FED
JIHG
ONMLK

#include<stdio.h>
#include<conio.h>
void main()
{
int n , i , j, m=65, k=64;
printf(“Enter n”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
k=k+i;
m=k;
for(j=0;j<=n-i;j++)
printf(“ ”);
for(j=1;j<=i;j++)
printf(“%c”, m--)
printf(“\n”;)
}
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 50
C-Programming Control Structur

Program: Write a C program to generate and print first N FIBONACCI numbers

#include <stdio.h>
void main()
{
int n1 =0, n2 =1, sum, n;
printf("Enter the value of n \n");
scanf("%d",&n);
printf("First %d FIBONACCI numbers are ...\n", n);
printf("%d\n", n1);
printf("%d\n", n2);
for(i=1;i<=num-2;i++)
{
sum = n1 + n2;
printf("%d\n", sum);
n1 = n2;
n2 = sum;
}
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 51
C-Programming Control Structur

Program: Write a program to calculate sum of series x = 1/2+3/4+5/6+…….n terms.

#include< stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
float sum = 0;
printf(“How many terms\n”);
scanf(“%d”, &n);
j=1;
for(i=1;i<=n; i++)
{
sum = sum+(float)(j)/(j+1);
j=j+2;
}
printf(“sum = %f”, sum);
}

Output

enter a number: 3
the value of this series= 2.083333

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 52
C-Programming Control Structur

Program: Write a program to calculate summation of series.


x=1-𝒙𝟐 /2!+𝒙𝟒 /4!-𝒙𝟔 /6!+𝒙𝟖 /8!..... upto n terms. [DEC-15, MAY-16]

#include<stdio.h>
#include<conio.h>
void main()
{

float x,sum,term,denr;

int i,n;

printf(“Enter the Value of x and Number of sums to be sum\t:”);

scanf(“%f%d”,&x,&n);

sum=1;term=1;

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

denr = (2*i)*(2*1-i);

term=-term*x*x/denr;

sum=sum+term;

printf(“\nthe sum =%f\nNumber of terms = %d\nvalue of x =%f\n


”,sum,n,x);

getch();

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 53
C-Programming Control Structur

Program: Write a program to calculate the value of the following series.

1 1 1 1
1+ + + +⋯+
2! 3! 4! 𝑛!

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
float sum=0.0;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&n);
for(i=1;i<n;i++)
{
fact=fact*i;
sum=sum+1.0/fact;
}
printf(“The value of the series is:%f”,sum);
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 54
C-Programming Control Structur

Program: Write a program to calculate summation of series


1/1! + 2/2! + 3/3! +….+ n/n! [10 M DEC-18]

#include <stdio.h>
#include <conio.h>
void main()
{
double n,f=1,i,sum=0;
printf("\n Enter the value: ");
scanf("%lf", &n);
for (i = 1; i <= n; i++)
{
f = f * i;
sum = sum +(i / f);
}
printf("\n Sum of the above series = %lf ", sum);
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 55
C-Programming Control Structur

Program

Write a program to calculate the sine of an angle using the following series for x as
the angle in radians.
𝒙𝟑 𝒙𝟓 𝒙𝟕 𝒙𝒏
sin x = x- + − …..
𝟑! 𝟓! 𝟕! 𝒏!

Accept the angle in degrees and value of n from user

#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int i, n, fact=1, sign= -1;
float x, numerator, sum, term;
clrscr();
printf(“Enter an angle in degrees and the value of n: ”)
scanf(“%f %d”, &x, &n);
x=x*3.14/180;
term=x;
sum=term;
for(i=3;i<=n;i=i+2)
{
fact=fact*i*(i-1) ;
numerator=pow(x,i);
term=numerator/fact;
sum=sum+sign*term;
sign=sign*term;
}
print(“The value of the series is;%f”,sum);
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 56
C-Programming Control Structur

Program

Write a program to calculate the sum of series


x-x/2!+x/3!-x/4!..........x/n! [6 M May-19]

#include <math.h>
#include <stdio.h>
void main()
{
int n,x,fact=1,sum,i,se;
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the value of x:");
scanf("%d",&x);
for(i=1;i<=n-1;i++)
{
fact = fact*i;
sum=sum+sign*x/fact;
sign=sign*-1;
}
printf("The result of series is: %d", sum);
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 57
C-Programming Control Structur

Program

An electric power distribution company charges its domestic consumer as


follows:

Consumption Units Rate of Charge


0-200 o.50 per unit
201-400 Rs. 100 plus Rs. 0.65 per
unit excess of 200
401-600 Rs. 230 plus Rs. 0.85 per
unit excess of 400
601-above Rs. 390 plus Rs. 1.00 per
unit excess of 600

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 58
C-Programming Control Structur

Program should read the units consumed for a customer and calculate the total bill.

#include<stdio.h>
#include<conio.h>
void main()
{
int units;
float charge;
clrscr();
printf(“Enter the units consumed:”);
scanf(“%d”, &units);
switch(units/200)
{
case 0:charge=units *0.5;
break;
case 1:charge=(units-200) *0.65+100;
break;
case 2:charge=(units-400) *0.85+230;
break;
default: charge=(units-600) +390;
break;
}
printf(“Charges=%f”, charge);
getch();
}

Output

Enter two numbers:25


10
LCM = 50
GCD = 5

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 59
C-Programming Control Structur

Branching Statements (Break, Continue and Go to)

Break

• Break statement is used to transfer the control of a program to end of loop.


• When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following the
loop
• It can be used to terminate a case in the switch statement
• If you are using nested loops, the break statement will stop the execution of the
innermost loop.

Syntax

break;

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 60
C-Programming Control Structur

Example

#include <stdio.h>
int main ()
{
int i = 1;
while( i <=10 )
{
printf("value of i: %d\n", i);
if( i= =5)
{
/* terminate the loop using break statement */
break;
}
i++;
}
Output
return 0;
}
value of i: 1
value of i: 2
value of i: 3
value of i: 4
value of i: 5

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 61
C-Programming Control Structur

Continue statement

• continue statement are used to transfer the control of a program to begning on loop and skip the
rest of current statement in loop.
Syntax

continue;

Example

for (int j=1; j<=10; j++)


{
if (j==5)
{
continue;
}
printf("%d ", j);
}

Output

1 2 3 4 6 7 8 9 10

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 62
C-Programming Control Structur

GOTO statement

• goto statement are used to transfer the control of a program from one part to
another part.
Syntax
goto label;
..
..
label: statement;

#include <stdio.h>
void main ()
{
int n = 1;
accept :
printf(“Enter a number\n”);
scanf(“%d”, & n);
if(n<0)
{
goto accept;
}
getch();
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 63
C-Programming Control Structur

Program

To check whether a given number is prime or not

/* Assume 2 is a prime number but 0 and 1 are not */


#include <stdio.h>
#include <conio.h>
void main()
{
int n ,i , flag ;
clrscr() ;
printf("Enter a number: ") ;
scanf("%d" , &n) ;
flag=1 ;
for(i=2 ; i<=n-1 ; n++)
{
if(n%i==0) /* True if number is not prime */
{
flag=0 ;
break ; /* Loop terminates if p is not prime */
}
}
if(flag==0)
printf("%d is not prime" , p) ;
else
printf("%d is prime" , p) ;
getch() ;
}

Output 1

Enter a number: 7
7 is prime

Output 2

Enter a number: 1
1 is not prime

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 64
C-Programming Control Structur

Program
Write a program to find GCD and LCM of 2 nos. 6M [Dec-14]

#include <stdio.h>
#include <conio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}

Output

enter a two numbers: 25


10
LCM = 50
GCD = 5

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 65
C-Programming Control Structur

Program

Write a program to display Armstrong nos between 1 to 1000. 6M[May-13]

#include <stdio.h>
#include <conio.h>
int main( )
{
int r,number = 0, c, sum = 0, temp;
printf("Enter an integer upto which you want to find armstrong
numbers\n");
scanf("%d",&number);
printf("Following armstrong numbers are found from 1 to
%d\n",number);
for( c = 1 ; c <= number ; c++ )
{
temp = c;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( c == sum )
printf("%d\n", c);
sum = 0;
}
return 0;
}

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 66
C-Programming Control Structur

Program
Write a program to generate prime nos between 1 to100.

#include<stdio.h>
#include<conio.h>
{
int main()
int n,j;
clrscr();
printf("Prime numbers between 1 to 100\n");
for(n=3;n<=100;n++)
{
for(j=2;j<n;j++)
{
if(n%j==0)
break;
}
if(j==n)
printf("%d",n);
}
getch();
return 0;
}

Output

2 13 31 53 73
3 17 37 59 79
5 19 41 61 83
7 23 43 67 89
11 29 47 71 97

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 67
C-Programming Control Structur

Prorame
Write a program to display the factors of user entered number.

#include<stdio.h>
#include<conio.h>
void main ()
{
int n, i;
clrscr();
printf(“enter a number”);
scanf(“%d”,&n);
printf(“factors are : \n”);
i= 2;
while(i<n)
{
if(n%i==0)
printf(“%d”\n”,i);
i++
}
getch();
}

Output

enter a number : 30
factors are:
2
3
5
6
10
15

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 68
C-Programming Control Structur

Questions
1. Explain difference between for, while and do while loop. (4 Mks)
2. Write a program to generate patters.
3. Write a program to generate prime nos between 1 to 100. (10M)
4. Write a program to generate following patterns. 10M
5. Explain difference between while and do-while. [5 M]
6. Explain for loop with syntax and example [4 M]
1. Explain switch control statement with example [4 M].
2. Discuss what is need of break statement in switch case [4 M]
3. Explain the following with example 1) goto, 2) continue 3) break [4 M]
4. Write a program print the sum of following series
1+22+33+…….+nn [5 Marks] [MAY-18]
5. Comapre break and continue statements [5 Marks] [MAY-18]
6. Compare if-else and switch statements [5 Marks] [MAY-18]
7. Write a program to calculate number of vowels in the entered string. [6 Marks]
[MAY-18]
8. Write a program to print following pattern
i) 5432*
543*1
54*21
5*321
*4321

ii) 5
54
543
5432
54321

This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg. 3 - 69

You might also like