Control structures the structures which control the flow of program from one part to another depending on condition Types of control structures: 1. Sequential Structure 2. Selection Structure / Branching Structure: if –else, switch- case 3. Loop Structure/ Iteration Construct:- for, while , do - while 1 Prepared by Dharmakumari Kalakheti
It is the most simplest programming structure where statements are executed sequentially from top to bottom without repetition, branching and without any condition 2 Prepared by Dharmakumari Kalakheti
In this structure execution of statements depends upon a condition. If condition is true, one action is followed, otherwise another action is followed. It is also known as branching structure or decision construct. 3 Prepared by Dharmakumari Kalakheti
Syntax: if(Conditional expression) { Statement(s) } 4 Prepared by Dharmakumari Kalakheti
Syntax if (conditional expression) { statement } Else { statement } 5 Prepared by Dharmakumari Kalakheti
Syntax if (conditional expression) { statement } else if (conditional expression) { statement } else { statement } 6 Prepared by Dharmakumari Kalakheti
Practical Questions 1. Write algorithm, flow chart and program to find out the given number is negative or positive. 2. Write algorithm, flow chart and program to find out the given number is odd or even. 3. Write algorithm, flow chart and program to read three integer numbers and print the maximum. 4. 4. Write a flowchart and a program to find out whether the given 4 digit number (year) is a leap year or not 7 Prepared by Dharmakumari Kalakheti
Nested if-else statement Syntax if (conditional expression) { if (conditional expression) { statement } else { statement } } else { statement } 8 Prepared by Dharmakumari Kalakheti
1. Write algorithm, flow chart and program to find out the given number is negative or positive. #include<stdio.h> #include<conio.h> void main() { clrscr(); int n; printf("enter the value of n= "); scanf("%d",&n); if(n>=0) printf("positive number "); else printf("negative number "); getch(); } 9 Prepared by Dharmakumari Kalakheti
2. Write algorithm, flow chart and program to find out the given number is odd or even. #include<stdio.h> #include<conio.h> void main() { clrscr(); int x; printf("Enter the value of x= "); scanf("%d",&x); if(x%2==0) printf("even number "); else printf("odd number "); getch(); } 10 Prepared by Dharmakumari Kalakheti
3.Write algorithm, flow chart and program to read three integer numbers and print the maximum. #include<stdio.h> #include<conio.h> //lude<math.h> void main() { clrscr(); int a,b,c; printf("enter the three number: a,b,c="); scanf("%d,%d,%d", &a,&b,&c); printf("a=%d,b=%d,c=%dn",a,b,c); if (a>=b && a>=c) printf("a is greater "); else if (b>=a && b>=c) printf("b is greater"); else printf("C is greater"); getch(); } 11 Prepared by Dharmakumari Kalakheti
. START Read numbers a ,b ,c Is a>b? Is a>c ? Is b>c ? PRINT a as greater PRINT C is greater PRINT b as greater STOP YES YES YES NO NO NO 12 Prepared by Dharmakumari Kalakheti
#include<stdio.h> #include<conio.h> void main() { clrscr(); int n,b; printf("enter the value of n= "); scanf("%d",&n); if(n%2) {b==0; printf("Even number"); } else printf("Odd number "); getch(); } 4.Write a program to display whether the input digit is odd or even 13 Prepared by Dharmakumari Kalakheti
7.Write a program to read average temperature of a day in Fahrenheit to print "Nice Day" if temp is >60 & <80 "Cold Day" if temp is <=60 "Hot Day" if temp is >=80 8.Write a flowchart a program to read length & breadth of a room and print area & print "Auditorium " if area >2500 " Hall" if area.>=500 and <=2500 "Big Room" if area>150 and <500 "Small Room " if area <=150 9.Write a flowchart and a program to find out whether the given 4 digit number (year) is a leap year 14 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> #include <math.h> void main() { int a; clrscr(); printf("three any number="); scanf("%d",&a); if(a/5==0&& a/11!=0) printf(" %d is the divisible by 5and not divisible by 11 "); else printf("the condition is not satisfied "); getch(); } #WAP to input number and display it if it is exactly divisible by 5 but not by 11 (first semester 2072-12-24) 15 Prepared by Dharmakumari Kalakheti
17.Write a program to read average temperature of a day in Fahrenheit to print "Nice Day" if temp is >60 & <80 "Cold Day" if temp is <=60 "Hot Day" if temp is >=80 #include<stdio.h> #include<conio.h> void main() { int temp; clrscr(); printf("Enter temp: "); scanf("%d",&temp); if(temp > 60 && temp<80) printf("nice day: "); else if(temp<=60) printf("cold day: "); else if(temp>=80) printf("hot day :"); getch(); } 16 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int n,a,r,s=0; printf("enter the number"); scanf("%d",&n); a=n; { r=n%10; s=s*10+r; n=n/10; } if(a==s) printf(“It is palindrome"); else printf(“It is not palindrome"); getch(); } WAP to check if input number is palindrome or not 17 Prepared by Dharmakumari Kalakheti
16.Write a flowchart a program to read length & breadth of a room and print area & print "Auditorium " if area >2500 " Hall" if area.>=500 and <=2500 "Big Room" if area>150 and <500 "Small Room " if area <=150 18 Prepared by Dharmakumari Kalakheti
#include<stdio.h> #include<conio.h> void main() { int l,b,a; printf("enter the lenth="); scanf("%d",&l); printf("enter the breadth="); scanf("%d",&b); a=l*b; printf("narea=%d",a); if(a>2500) printf("nauditorium"); else if(a>=500&&a<=2500) printf("nHall"); else if(a>150&&a<500) printf("nBigroom"); else if(a<=500) printf("nsmall room"); else printf("nNO space"); getch(); } 19 Prepared by Dharmakumari Kalakheti
Syntax switch (expression) { case value 1: statement block; break; case value 2: statement block; break case value 3: statement block; break; .. default statement block: } 20 Prepared by Dharmakumari Kalakheti
#include <conio.h> #include <stdio.h> void main() { int s,a=4,b=4,sum,product ,division; printf("enter any case :"); scanf("%d",&s); switch(s) { case 1: sum=a+b; printf("Sum=%d",sum); break; case 2: product=a*b; printf("product=%d",product); break; case 3: division=a%b; printf("division=%d",division); } getch(); 21 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { icte: clrscr(); int s,a,b,sum,product,division; //printf("enter any two numbers a and b:n "); //scanf("%d%d",&a,&b); printf("case 1: sumn"); printf("case 2:produtn"); printf("case 3: divisionn"); scanf("%d",&s); switch(s) { case 1: printf("enter any two numbers a and b:n "); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); break; case 2: printf("enter any two numbers a and b:n "); scanf("%d%d",&a,&b); product=a*b; printf("product=%d",product); break; case 3: printf("enter any two numbers a and b:n "); scanf("%d%d",&a,&b); division=a%b; printf("division=%d",division); } getch(); goto icte; } 22 Prepared by Dharmakumari Kalakheti
WAP to read two numbers and display the following menu: using switch case MENU i.Summation ii.Sum of squares iii.Sum of cubes iv.product v.Exit 23 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> void main() { int n,s,p,x=5,y=4,cub,sqr; //clrscr(); icte: printf("nt case 1: Summation"); printf("nt case 2: Sum of cubes"); printf("nt case 3: Sum of squares"); printf("nt case 4: product"); printf("nt case 5: exit"); printf("ntenter the case="); scanf("%d",&n); clrscr(); switch(n) { case 1: s=x+y; printf("sum=%d",s); break; case 2: cub=pow(x,3)+pow(y,3); printf("sum of cube=%d",cub); break; case 3: sqr= pow(x,2)+pow(y,2); printf(" sum of sqr=%d",sqr); break; case 4: p=x*y; printf("prod=%d",p); break; case 5: exit(0); } getch(); goto icte; } 24 Prepared by Dharmakumari Kalakheti
The operator pair “? :”is known as conditional operator. Unlike all other operators ? : in C operator is ternary operator i.e. it takes three operands.The conditional operator has the following construct : expr1 ? expr2: expr3 Here , expr1 is evaluated first. If expr1 is true, the value of value of expr2 is the value of conditional expression. If expr1 is false , the value of expr3 is the value of conditional expression. 25 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int a,b,larger; clrscr(); printf("Enter the two number:"); scanf("%d %d",&a,&b); larger=a>b? a:b; printf("the larger number is=%d", larger); getch(); } 26 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> int main() { int x=15,y; y=(x> 5)? 3:5; printf("%d",y); getch(); } 27 Prepared by Dharmakumari Kalakheti
a) Without using loop Void main() { Int I; Clrscr(); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Getch(); } 28 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int i; clrscr(); for(i=0;i<10;i++) printf("nWellcome to my college"); getch(); } 29 Prepared by Dharmakumari Kalakheti
The “for loop” loops from one number to another number and increases by a specified value each time.The “for loop” uses the following structure: for (Start value; continue or end condition; increase/decrease value) statement; 30 Prepared by Dharmakumari Kalakheti
for(i = 2; i <= 6; i = i + 2) { printf("%dt", i + 1); } for(i = 2; i <= 6; i = i + 2) { printf("%dt", i + 1); } for(i = 2; i <= 6; i = i + 2) { printf("%dt", i + 1); } for(i = 2; i <= 6; i = i + 2) { printf("%dt", i + 1); } For(i=1;i<=6 ; i++) 31 Prepared by Dharmakumari Kalakheti
32 Prepared by Dharmakumari Kalakheti
33 Prepared by Dharmakumari Kalakheti
#include< conio.h> #include<stdio.h> void main() { int i; for (i = 0; i < 10; i++) { printf ("Hellon"); printf ("Worldn"); } Getch(); } 34 Prepared by Dharmakumari Kalakheti
/* program to print the natural numbers from 1 to 10 */ #include<stdio.h> #include<conio.h> void main() { int i; for (i=1; i<=10; i++) { printf("%dn", i); } getch(); } 35 Prepared by Dharmakumari Kalakheti
In do ….while loop, the body of the loop is executed first without testing condition. At the end of the loop, test condition in the while statement is evaluated. If the condition is true , the program continues to evaluate the body of the loop once again .This process continues as long as the condition is true.When the condition becomes false the loop is terminated, and the control goes to the statement that appears immediately after the while statement . 36 Prepared by Dharmakumari Kalakheti
Since the test condition is evaluated at the bottom of the loop the do…. While loop construct provides an exit- controlled or bottom –tested loop and therefore the body of the loop is always executed at least once. 37 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { clrscr(); int n; n=10; do { printf("%dn",n); n--; } while(n>=1); getch(); } 38 Prepared by Dharmakumari Kalakheti
In the beginning of while loop, test expression is checked. If it is true, codes inside the body of while loop, i.e., code/s inside parenthesis are executed and again the test expression is checked and process continues until the test expression becomes false Syntax of while loop while (test expression)/condition { statements to be executed. } 39 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { clrscr(); int n; n=0; while(n<=10) { printf("%dn",n); n++; } getch(); } 40 Prepared by Dharmakumari Kalakheti
.Write a program to read any integer and to print its multiplication table. #include <stdio.h> #include <conio.h> void main() { int a,b,c; clrscr(); a=1; printf("enter any number="); scanf("%d",&b); for(a=1;a<=10;a++) { c = b*a; printf("n%d * %d = %d",b,a,c); } getch(); } 41 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int a,b,c; clrscr(); for(a=1;a<=10;a++) { for(b=1;b<=10; b++) { c = a*b; printf("t %d",c); } } getch(); } Write a program to print multiplication table from 1 to 10. 42 Prepared by Dharmakumari Kalakheti
43 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int i,j,n=6; for (i=1;i<=n;i++) { for (j=1;j<=i;j++) printf("t*"); printf("t"); printf("n"); } getch(); } 44 Prepared by Dharmakumari Kalakheti
#include<stdio.h> #include<conio.h> void main() { int n,fact=1; printf("enter the number="); scanf("%d",&n); while (n>1) { fact=fact*n; n-- ; } printf("fact=%d",fact); getch(); } 45 Prepared by Dharmakumari Kalakheti
46 Prepared by Dharmakumari Kalakheti
#include<stdio.h> #include<conio.h> void main() { clrscr(); int n; n=46; while(n<=85) { printf("ascii number=%d,charecter=%cn",n,n); n++; } getch(); } Write a program to print ofASCII character for 45 to 96.(using while loop) 47 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int n,m,s=0; clrscr(); printf("Enter student marks:n"); for(n=1;n<=3;n++) { scanf("%d",&m); s = s+m; } printf( "nsum=%d",s); getch(); } 48 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int n,sum=0,d; clrscr(); printf("Enter the integer="); scanf(" %d",&n); while(n!=0) { d=n%10; sum=sum+d; n=n/10; } printf("value of arbitery lengh=%d",sum); getch(); } Q.Write a program to find out sum of digit of a given integer of arbitrary length.( Using while loop) 49 Prepared by Dharmakumari Kalakheti
. Write a program to read 3 digit no and to test whether it is a Armstrong number or not (a3+b3+c3=abc ) #include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n,n1,d1,d2,d3,arm; printf("Enter number = "); scanf("%d",&n); n1=n; d1=n%10; n=n/10; d2=n%10; n=n/10; d3=n; arm= pow(d1,3)+pow(d2,3)+pow(d3,3); if(n1==arm) printf("armstrong......."); else printf("Not armstrong");234 getch(); 50 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> int main() { int n, sum=0,temp, rem; printf("Enter an integer: "); scanf("%d", &n); temp=n; while(n!=0) { rem=n%10; sum=sum+rem*rem*rem; n/=10; } if(sum==temp) printf("Armstrong") ; else printf("Not Armstrong"); getch(); } 51 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> #include <math.h> void main() { int n1,n,d1,d2,d3,arm; clrscr(); printf("three arm.number="); for(n1=100;n1<=999;n1++) { n=n1; d1=n%10; n=n/10; d2=n%10; n=n/10 ; d3=n; arm=pow(d1,3)+pow(d2,3)+pow(d3,3); if (arm==n1) printf("n %d",n1); } 52 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> #include <math.h> void main() { int n,c,a,sum=0; clrscr(); printf("Enter the arm="); scanf("%d",&n); c=n; while(c!=0) { a=c%10; c=c/10; sum=(sum+ pow(a,3)); } if(sum==n) printf("Arm"); else printf("NOT ARM"); getch(); } 53 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { clrscr(); int i,n,a; printf("Enter the value of Nth term "); scanf("%d",&n); printf("the reqired series is n"); a=1; for(i=1;i<=n;i++) { printf("%dt",a); a= a+4; } getch(); } 54 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { clrscr(); int x,y,z, count; x=1; y=1; printf("%d,%d",x,y); for (count=1;count<=25;count++) { z=x+y; printf("t%d",z); x=y; y=z; } getch(); } 55 Prepared by Dharmakumari Kalakheti
1 2 2 3 3 3 4 4 4 4 5 5 5 5 #include <stdio.h> #include <conio.h> void main() { clrscr(); int a,b; for(a=1;a<=5;a++) { printf("n"); for(b=1; b<=a; b++) printf("%d",a); } getch(); } 56 Prepared by Dharmakumari Kalakheti
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 #include <stdio.h> #include <conio.h> void main() { clrscr(); int a,b; for(a=1;a<=5;a++) { printf("n"); for(b=1;b<=a;b++) printf("%d",b); } getch(); 57 Prepared by Dharmakumari Kalakheti
#include <conio.h> #include <math.h> void main() { clrscr(); int i,n; printf("Enter the number :"); scanf("%d",&n); for(i=2;i<=n;i++) if(n%i==0) break; if(n==i) printf("prime"); else printf("Not prime "); getch(); } Q.WAP to determine whether a number is prime or not. 58 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> //WAP to print 50 prime number void main() { int ,i,j; clrscr(); for(i=2;i<=200;i++) { for(j=2;j<=i-1;j++) if(i%j==0) break;//number is divisiable by some other number if(i==j) printf("%dt",i); } getch(); } 59 Prepared by Dharmakumari Kalakheti
#include<stdio.h> #include<conio.h> void main() { int a=1,b=4,y,z,n,sum=0; clrscr(); a=1; b=4; printf("enter the y="); scanf("%d",&y); for (n=1;n<=y;n++) { z=a*b; sum=sum+z; printf("nseries:%dx%dt",a,b); b+=3; } printf("ntotal value of series:%d",sum); getch(); } 60 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int n=1,x=4,y,z; clrscr(); printf("enter the y="); scanf("%d",&y); for (n=1;n<=y;n++) { z=n*x; x+=3; printf("nseries:%dx%dt",n,x); } getch(); } 61 Prepared by Dharmakumari Kalakheti
#include <stdio.h> #include <conio.h> void main() { int n, reverse=0, rem,temp; printf("Enter an integer: "); scanf("%d", &n); temp=n; while(temp!=0) { rem=temp%10; reverse=reverse*10+rem; temp/=10; } /* Checking if umber entered by user and it's reverse number is equal. */ if(reverse==n) printf("%d is a palindrome.",n); else printf("%d is not a palindrome.",n); getch(); } 62 Prepared by Dharmakumari Kalakheti
3. Write a flowchart and a program to read 15 persons age and find out how many person fall under the following categories. Baby – age 0 to 5 Attending School- age 6 to 17 Adult – age 18 and over (Using for loop) 63 Prepared by Dharmakumari Kalakheti
void main { int baby=0,adult=0,school=0,count,age; clrscr(); for(count=1;count<=15;count++) { printf("enter the age of our baby age:%d=",count); scanf("%d",&age); if(age>=0 && age<=5) baby=baby+1; else if(age>=6 && age <=17) school=school+1; else adult=adult+1; } printf("number of baby:%d",baby); printf("nnumber of school age:%d",school); printf ("nnumber of adul age :%d",adult); getch(); } 64 Prepared by Dharmakumari Kalakheti
4. Write a flowchart and a program to read 15 persons age maximum, minimum #include <stdio.h> #include <conio.h> #include <math.h> void main() { int max=0,min=100,count,age; clrscr(); for(count=1;count<=15;count++) { printf(“Enter the age of ict students="); scanf("%d",&age); if(age>=max) max=age; if (age<=min) min=age; } printf("nmax age=%d",max); printf("nmin age=%d",min); getch(); } 65 Prepared by Dharmakumari Kalakheti

Loop's definition and practical code in C programming

  • 1.
    Control structures thestructures which control the flow of program from one part to another depending on condition Types of control structures: 1. Sequential Structure 2. Selection Structure / Branching Structure: if –else, switch- case 3. Loop Structure/ Iteration Construct:- for, while , do - while 1 Prepared by Dharmakumari Kalakheti
  • 2.
    It is themost simplest programming structure where statements are executed sequentially from top to bottom without repetition, branching and without any condition 2 Prepared by Dharmakumari Kalakheti
  • 3.
    In this structureexecution of statements depends upon a condition. If condition is true, one action is followed, otherwise another action is followed. It is also known as branching structure or decision construct. 3 Prepared by Dharmakumari Kalakheti
  • 4.
  • 5.
  • 6.
    Syntax if (conditional expression) { statement } elseif (conditional expression) { statement } else { statement } 6 Prepared by Dharmakumari Kalakheti
  • 7.
    Practical Questions 1. Writealgorithm, flow chart and program to find out the given number is negative or positive. 2. Write algorithm, flow chart and program to find out the given number is odd or even. 3. Write algorithm, flow chart and program to read three integer numbers and print the maximum. 4. 4. Write a flowchart and a program to find out whether the given 4 digit number (year) is a leap year or not 7 Prepared by Dharmakumari Kalakheti
  • 8.
    Nested if-else statement Syntax if(conditional expression) { if (conditional expression) { statement } else { statement } } else { statement } 8 Prepared by Dharmakumari Kalakheti
  • 9.
    1. Write algorithm,flow chart and program to find out the given number is negative or positive. #include<stdio.h> #include<conio.h> void main() { clrscr(); int n; printf("enter the value of n= "); scanf("%d",&n); if(n>=0) printf("positive number "); else printf("negative number "); getch(); } 9 Prepared by Dharmakumari Kalakheti
  • 10.
    2. Write algorithm,flow chart and program to find out the given number is odd or even. #include<stdio.h> #include<conio.h> void main() { clrscr(); int x; printf("Enter the value of x= "); scanf("%d",&x); if(x%2==0) printf("even number "); else printf("odd number "); getch(); } 10 Prepared by Dharmakumari Kalakheti
  • 11.
    3.Write algorithm, flowchart and program to read three integer numbers and print the maximum. #include<stdio.h> #include<conio.h> //lude<math.h> void main() { clrscr(); int a,b,c; printf("enter the three number: a,b,c="); scanf("%d,%d,%d", &a,&b,&c); printf("a=%d,b=%d,c=%dn",a,b,c); if (a>=b && a>=c) printf("a is greater "); else if (b>=a && b>=c) printf("b is greater"); else printf("C is greater"); getch(); } 11 Prepared by Dharmakumari Kalakheti
  • 12.
    . START Read numbers a ,b,c Is a>b? Is a>c ? Is b>c ? PRINT a as greater PRINT C is greater PRINT b as greater STOP YES YES YES NO NO NO 12 Prepared by Dharmakumari Kalakheti
  • 13.
    #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,b; printf("enterthe value of n= "); scanf("%d",&n); if(n%2) {b==0; printf("Even number"); } else printf("Odd number "); getch(); } 4.Write a program to display whether the input digit is odd or even 13 Prepared by Dharmakumari Kalakheti
  • 14.
    7.Write a programto read average temperature of a day in Fahrenheit to print "Nice Day" if temp is >60 & <80 "Cold Day" if temp is <=60 "Hot Day" if temp is >=80 8.Write a flowchart a program to read length & breadth of a room and print area & print "Auditorium " if area >2500 " Hall" if area.>=500 and <=2500 "Big Room" if area>150 and <500 "Small Room " if area <=150 9.Write a flowchart and a program to find out whether the given 4 digit number (year) is a leap year 14 Prepared by Dharmakumari Kalakheti
  • 15.
    #include <stdio.h> #include <conio.h> #include<math.h> void main() { int a; clrscr(); printf("three any number="); scanf("%d",&a); if(a/5==0&& a/11!=0) printf(" %d is the divisible by 5and not divisible by 11 "); else printf("the condition is not satisfied "); getch(); } #WAP to input number and display it if it is exactly divisible by 5 but not by 11 (first semester 2072-12-24) 15 Prepared by Dharmakumari Kalakheti
  • 16.
    17.Write a programto read average temperature of a day in Fahrenheit to print "Nice Day" if temp is >60 & <80 "Cold Day" if temp is <=60 "Hot Day" if temp is >=80 #include<stdio.h> #include<conio.h> void main() { int temp; clrscr(); printf("Enter temp: "); scanf("%d",&temp); if(temp > 60 && temp<80) printf("nice day: "); else if(temp<=60) printf("cold day: "); else if(temp>=80) printf("hot day :"); getch(); } 16 Prepared by Dharmakumari Kalakheti
  • 17.
    #include <stdio.h> #include <conio.h> voidmain() { int n,a,r,s=0; printf("enter the number"); scanf("%d",&n); a=n; { r=n%10; s=s*10+r; n=n/10; } if(a==s) printf(“It is palindrome"); else printf(“It is not palindrome"); getch(); } WAP to check if input number is palindrome or not 17 Prepared by Dharmakumari Kalakheti
  • 18.
    16.Write a flowcharta program to read length & breadth of a room and print area & print "Auditorium " if area >2500 " Hall" if area.>=500 and <=2500 "Big Room" if area>150 and <500 "Small Room " if area <=150 18 Prepared by Dharmakumari Kalakheti
  • 19.
    #include<stdio.h> #include<conio.h> void main() { int l,b,a; printf("enterthe lenth="); scanf("%d",&l); printf("enter the breadth="); scanf("%d",&b); a=l*b; printf("narea=%d",a); if(a>2500) printf("nauditorium"); else if(a>=500&&a<=2500) printf("nHall"); else if(a>150&&a<500) printf("nBigroom"); else if(a<=500) printf("nsmall room"); else printf("nNO space"); getch(); } 19 Prepared by Dharmakumari Kalakheti
  • 20.
    Syntax switch (expression) { case value1: statement block; break; case value 2: statement block; break case value 3: statement block; break; .. default statement block: } 20 Prepared by Dharmakumari Kalakheti
  • 21.
    #include <conio.h> #include <stdio.h> voidmain() { int s,a=4,b=4,sum,product ,division; printf("enter any case :"); scanf("%d",&s); switch(s) { case 1: sum=a+b; printf("Sum=%d",sum); break; case 2: product=a*b; printf("product=%d",product); break; case 3: division=a%b; printf("division=%d",division); } getch(); 21 Prepared by Dharmakumari Kalakheti
  • 22.
    #include <stdio.h> #include <conio.h> voidmain() { icte: clrscr(); int s,a,b,sum,product,division; //printf("enter any two numbers a and b:n "); //scanf("%d%d",&a,&b); printf("case 1: sumn"); printf("case 2:produtn"); printf("case 3: divisionn"); scanf("%d",&s); switch(s) { case 1: printf("enter any two numbers a and b:n "); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); break; case 2: printf("enter any two numbers a and b:n "); scanf("%d%d",&a,&b); product=a*b; printf("product=%d",product); break; case 3: printf("enter any two numbers a and b:n "); scanf("%d%d",&a,&b); division=a%b; printf("division=%d",division); } getch(); goto icte; } 22 Prepared by Dharmakumari Kalakheti
  • 23.
    WAP to readtwo numbers and display the following menu: using switch case MENU i.Summation ii.Sum of squares iii.Sum of cubes iv.product v.Exit 23 Prepared by Dharmakumari Kalakheti
  • 24.
    #include <stdio.h> #include <conio.h> #include<math.h> #include <stdlib.h> void main() { int n,s,p,x=5,y=4,cub,sqr; //clrscr(); icte: printf("nt case 1: Summation"); printf("nt case 2: Sum of cubes"); printf("nt case 3: Sum of squares"); printf("nt case 4: product"); printf("nt case 5: exit"); printf("ntenter the case="); scanf("%d",&n); clrscr(); switch(n) { case 1: s=x+y; printf("sum=%d",s); break; case 2: cub=pow(x,3)+pow(y,3); printf("sum of cube=%d",cub); break; case 3: sqr= pow(x,2)+pow(y,2); printf(" sum of sqr=%d",sqr); break; case 4: p=x*y; printf("prod=%d",p); break; case 5: exit(0); } getch(); goto icte; } 24 Prepared by Dharmakumari Kalakheti
  • 25.
    The operator pair“? :”is known as conditional operator. Unlike all other operators ? : in C operator is ternary operator i.e. it takes three operands.The conditional operator has the following construct : expr1 ? expr2: expr3 Here , expr1 is evaluated first. If expr1 is true, the value of value of expr2 is the value of conditional expression. If expr1 is false , the value of expr3 is the value of conditional expression. 25 Prepared by Dharmakumari Kalakheti
  • 26.
    #include <stdio.h> #include <conio.h> voidmain() { int a,b,larger; clrscr(); printf("Enter the two number:"); scanf("%d %d",&a,&b); larger=a>b? a:b; printf("the larger number is=%d", larger); getch(); } 26 Prepared by Dharmakumari Kalakheti
  • 27.
    #include <stdio.h> #include <conio.h> intmain() { int x=15,y; y=(x> 5)? 3:5; printf("%d",y); getch(); } 27 Prepared by Dharmakumari Kalakheti
  • 28.
    a) Without usingloop Void main() { Int I; Clrscr(); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Printf(“nWelcome to my college”); Getch(); } 28 Prepared by Dharmakumari Kalakheti
  • 29.
    #include <stdio.h> #include <conio.h> voidmain() { int i; clrscr(); for(i=0;i<10;i++) printf("nWellcome to my college"); getch(); } 29 Prepared by Dharmakumari Kalakheti
  • 30.
    The “for loop”loops from one number to another number and increases by a specified value each time.The “for loop” uses the following structure: for (Start value; continue or end condition; increase/decrease value) statement; 30 Prepared by Dharmakumari Kalakheti
  • 31.
    for(i = 2;i <= 6; i = i + 2) { printf("%dt", i + 1); } for(i = 2; i <= 6; i = i + 2) { printf("%dt", i + 1); } for(i = 2; i <= 6; i = i + 2) { printf("%dt", i + 1); } for(i = 2; i <= 6; i = i + 2) { printf("%dt", i + 1); } For(i=1;i<=6 ; i++) 31 Prepared by Dharmakumari Kalakheti
  • 32.
  • 33.
  • 34.
    #include< conio.h> #include<stdio.h> void main() {int i; for (i = 0; i < 10; i++) { printf ("Hellon"); printf ("Worldn"); } Getch(); } 34 Prepared by Dharmakumari Kalakheti
  • 35.
    /* program toprint the natural numbers from 1 to 10 */ #include<stdio.h> #include<conio.h> void main() { int i; for (i=1; i<=10; i++) { printf("%dn", i); } getch(); } 35 Prepared by Dharmakumari Kalakheti
  • 36.
    In do ….whileloop, the body of the loop is executed first without testing condition. At the end of the loop, test condition in the while statement is evaluated. If the condition is true , the program continues to evaluate the body of the loop once again .This process continues as long as the condition is true.When the condition becomes false the loop is terminated, and the control goes to the statement that appears immediately after the while statement . 36 Prepared by Dharmakumari Kalakheti
  • 37.
    Since the testcondition is evaluated at the bottom of the loop the do…. While loop construct provides an exit- controlled or bottom –tested loop and therefore the body of the loop is always executed at least once. 37 Prepared by Dharmakumari Kalakheti
  • 38.
    #include <stdio.h> #include <conio.h> voidmain() { clrscr(); int n; n=10; do { printf("%dn",n); n--; } while(n>=1); getch(); } 38 Prepared by Dharmakumari Kalakheti
  • 39.
    In the beginningof while loop, test expression is checked. If it is true, codes inside the body of while loop, i.e., code/s inside parenthesis are executed and again the test expression is checked and process continues until the test expression becomes false Syntax of while loop while (test expression)/condition { statements to be executed. } 39 Prepared by Dharmakumari Kalakheti
  • 40.
    #include <stdio.h> #include <conio.h> voidmain() { clrscr(); int n; n=0; while(n<=10) { printf("%dn",n); n++; } getch(); } 40 Prepared by Dharmakumari Kalakheti
  • 41.
    .Write a programto read any integer and to print its multiplication table. #include <stdio.h> #include <conio.h> void main() { int a,b,c; clrscr(); a=1; printf("enter any number="); scanf("%d",&b); for(a=1;a<=10;a++) { c = b*a; printf("n%d * %d = %d",b,a,c); } getch(); } 41 Prepared by Dharmakumari Kalakheti
  • 42.
    #include <stdio.h> #include <conio.h> voidmain() { int a,b,c; clrscr(); for(a=1;a<=10;a++) { for(b=1;b<=10; b++) { c = a*b; printf("t %d",c); } } getch(); } Write a program to print multiplication table from 1 to 10. 42 Prepared by Dharmakumari Kalakheti
  • 43.
  • 44.
    #include <stdio.h> #include <conio.h> voidmain() { int i,j,n=6; for (i=1;i<=n;i++) { for (j=1;j<=i;j++) printf("t*"); printf("t"); printf("n"); } getch(); } 44 Prepared by Dharmakumari Kalakheti
  • 45.
    #include<stdio.h> #include<conio.h> void main() { int n,fact=1; printf("enterthe number="); scanf("%d",&n); while (n>1) { fact=fact*n; n-- ; } printf("fact=%d",fact); getch(); } 45 Prepared by Dharmakumari Kalakheti
  • 46.
  • 47.
    #include<stdio.h> #include<conio.h> void main() { clrscr(); int n; n=46; while(n<=85) { printf("asciinumber=%d,charecter=%cn",n,n); n++; } getch(); } Write a program to print ofASCII character for 45 to 96.(using while loop) 47 Prepared by Dharmakumari Kalakheti
  • 48.
    #include <stdio.h> #include <conio.h> voidmain() { int n,m,s=0; clrscr(); printf("Enter student marks:n"); for(n=1;n<=3;n++) { scanf("%d",&m); s = s+m; } printf( "nsum=%d",s); getch(); } 48 Prepared by Dharmakumari Kalakheti
  • 49.
    #include <stdio.h> #include <conio.h> voidmain() { int n,sum=0,d; clrscr(); printf("Enter the integer="); scanf(" %d",&n); while(n!=0) { d=n%10; sum=sum+d; n=n/10; } printf("value of arbitery lengh=%d",sum); getch(); } Q.Write a program to find out sum of digit of a given integer of arbitrary length.( Using while loop) 49 Prepared by Dharmakumari Kalakheti
  • 50.
    . Write aprogram to read 3 digit no and to test whether it is a Armstrong number or not (a3+b3+c3=abc ) #include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n,n1,d1,d2,d3,arm; printf("Enter number = "); scanf("%d",&n); n1=n; d1=n%10; n=n/10; d2=n%10; n=n/10; d3=n; arm= pow(d1,3)+pow(d2,3)+pow(d3,3); if(n1==arm) printf("armstrong......."); else printf("Not armstrong");234 getch(); 50 Prepared by Dharmakumari Kalakheti
  • 51.
    #include <stdio.h> #include <conio.h> intmain() { int n, sum=0,temp, rem; printf("Enter an integer: "); scanf("%d", &n); temp=n; while(n!=0) { rem=n%10; sum=sum+rem*rem*rem; n/=10; } if(sum==temp) printf("Armstrong") ; else printf("Not Armstrong"); getch(); } 51 Prepared by Dharmakumari Kalakheti
  • 52.
    #include <stdio.h> #include <conio.h> #include<math.h> void main() { int n1,n,d1,d2,d3,arm; clrscr(); printf("three arm.number="); for(n1=100;n1<=999;n1++) { n=n1; d1=n%10; n=n/10; d2=n%10; n=n/10 ; d3=n; arm=pow(d1,3)+pow(d2,3)+pow(d3,3); if (arm==n1) printf("n %d",n1); } 52 Prepared by Dharmakumari Kalakheti
  • 53.
    #include <stdio.h> #include <conio.h> #include<math.h> void main() { int n,c,a,sum=0; clrscr(); printf("Enter the arm="); scanf("%d",&n); c=n; while(c!=0) { a=c%10; c=c/10; sum=(sum+ pow(a,3)); } if(sum==n) printf("Arm"); else printf("NOT ARM"); getch(); } 53 Prepared by Dharmakumari Kalakheti
  • 54.
    #include <stdio.h> #include <conio.h> voidmain() { clrscr(); int i,n,a; printf("Enter the value of Nth term "); scanf("%d",&n); printf("the reqired series is n"); a=1; for(i=1;i<=n;i++) { printf("%dt",a); a= a+4; } getch(); } 54 Prepared by Dharmakumari Kalakheti
  • 55.
    #include <stdio.h> #include <conio.h> voidmain() { clrscr(); int x,y,z, count; x=1; y=1; printf("%d,%d",x,y); for (count=1;count<=25;count++) { z=x+y; printf("t%d",z); x=y; y=z; } getch(); } 55 Prepared by Dharmakumari Kalakheti
  • 56.
    1 2 2 3 33 4 4 4 4 5 5 5 5 #include <stdio.h> #include <conio.h> void main() { clrscr(); int a,b; for(a=1;a<=5;a++) { printf("n"); for(b=1; b<=a; b++) printf("%d",a); } getch(); } 56 Prepared by Dharmakumari Kalakheti
  • 57.
    1 1 2 1 23 1 2 3 4 1 2 3 4 5 #include <stdio.h> #include <conio.h> void main() { clrscr(); int a,b; for(a=1;a<=5;a++) { printf("n"); for(b=1;b<=a;b++) printf("%d",b); } getch(); 57 Prepared by Dharmakumari Kalakheti
  • 58.
    #include <conio.h> #include <math.h> voidmain() { clrscr(); int i,n; printf("Enter the number :"); scanf("%d",&n); for(i=2;i<=n;i++) if(n%i==0) break; if(n==i) printf("prime"); else printf("Not prime "); getch(); } Q.WAP to determine whether a number is prime or not. 58 Prepared by Dharmakumari Kalakheti
  • 59.
    #include <stdio.h> #include <conio.h>//WAP to print 50 prime number void main() { int ,i,j; clrscr(); for(i=2;i<=200;i++) { for(j=2;j<=i-1;j++) if(i%j==0) break;//number is divisiable by some other number if(i==j) printf("%dt",i); } getch(); } 59 Prepared by Dharmakumari Kalakheti
  • 60.
    #include<stdio.h> #include<conio.h> void main() { int a=1,b=4,y,z,n,sum=0; clrscr(); a=1; b=4; printf("enterthe y="); scanf("%d",&y); for (n=1;n<=y;n++) { z=a*b; sum=sum+z; printf("nseries:%dx%dt",a,b); b+=3; } printf("ntotal value of series:%d",sum); getch(); } 60 Prepared by Dharmakumari Kalakheti
  • 61.
    #include <stdio.h> #include <conio.h> voidmain() { int n=1,x=4,y,z; clrscr(); printf("enter the y="); scanf("%d",&y); for (n=1;n<=y;n++) { z=n*x; x+=3; printf("nseries:%dx%dt",n,x); } getch(); } 61 Prepared by Dharmakumari Kalakheti
  • 62.
    #include <stdio.h> #include <conio.h> voidmain() { int n, reverse=0, rem,temp; printf("Enter an integer: "); scanf("%d", &n); temp=n; while(temp!=0) { rem=temp%10; reverse=reverse*10+rem; temp/=10; } /* Checking if umber entered by user and it's reverse number is equal. */ if(reverse==n) printf("%d is a palindrome.",n); else printf("%d is not a palindrome.",n); getch(); } 62 Prepared by Dharmakumari Kalakheti
  • 63.
    3. Write aflowchart and a program to read 15 persons age and find out how many person fall under the following categories. Baby – age 0 to 5 Attending School- age 6 to 17 Adult – age 18 and over (Using for loop) 63 Prepared by Dharmakumari Kalakheti
  • 64.
    void main { int baby=0,adult=0,school=0,count,age; clrscr(); for(count=1;count<=15;count++) { printf("enterthe age of our baby age:%d=",count); scanf("%d",&age); if(age>=0 && age<=5) baby=baby+1; else if(age>=6 && age <=17) school=school+1; else adult=adult+1; } printf("number of baby:%d",baby); printf("nnumber of school age:%d",school); printf ("nnumber of adul age :%d",adult); getch(); } 64 Prepared by Dharmakumari Kalakheti
  • 65.
    4. Write aflowchart and a program to read 15 persons age maximum, minimum #include <stdio.h> #include <conio.h> #include <math.h> void main() { int max=0,min=100,count,age; clrscr(); for(count=1;count<=15;count++) { printf(“Enter the age of ict students="); scanf("%d",&age); if(age>=max) max=age; if (age<=min) min=age; } printf("nmax age=%d",max); printf("nmin age=%d",min); getch(); } 65 Prepared by Dharmakumari Kalakheti