3 Chapter 3 CS - Vinit
3 Chapter 3 CS - Vinit
• 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.
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
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
#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
#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
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
• 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
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
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
#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
Syntax
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.
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:-
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
#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:-
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
#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
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
#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:-
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
#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
#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 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
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.
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
*
**
***
****
#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
#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
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
#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
#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
#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
#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
****
***
**
*
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
#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:
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
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
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
#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
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
#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
#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
#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
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
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
#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
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
#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
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
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
#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
#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
#include<stdio.h>
#include<conio.h>
void main()
{
float x,sum,term,denr;
int i,n;
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;
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
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
#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- + − …..
𝟑! 𝟓! 𝟕! 𝒏!
#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
#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
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
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
Break
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
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
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
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
#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