We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43
UNIT 3:
DECISION MAKING AND LOOP
STATEMENTS WRITEA C PROGRAM TO FIND WHETHER THE GIVEN NUMBER IS EVEN OR ODD USING IF……ELSE STATEMENT #include<stdio.h> #include<conio.h > void main( ) { int num; clrscr( ); printf(“enter the num”); scanf(%d,&num0; if(num%2==0); printf(“number is even \n”); else printf(“number is oddn”); getch( ); } INPUT : Enter the num 13 OUTPUT : Number is odd WRITEA C PROGRAM TO FIND THE BIGGEST VALUE OF THREE NUMBERS USING NESTED….IF ….ELSE . #include<stdio.h> #include<coino.h> void main ( ) { int a , b ,c; clrscr(); printf(“enter the three numbers :”); scanf(“%d %d %d”, &a &b &c); if (a > b) { if (a > c) printf (“\ n % d is the biggest number “ , a ); else printf(“\ n % d is the biggest number “ , c ); } else { if ( b > c ) printf(“\ n % d is the biggest number “, b ); else printf(“\ n % d is the biggest number “ , c ); } getch (); } INPUT : enter three numbers : 3 4 5 OUTUT : 5 is the biggest number WRITE A C PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION USING ELSE…..IF LADDER . #include<stdio.h> #include<conio.h> #include<math.h> void main () { float a , b , c, r1 , r2 , d; clrscr(); printf(“enter a, b , c value “); scanf(“%f %f %f ”, & a , &b, &c); d=b*b-4*a*c; if(d==0) { printf(“roots are real & equal”); r1= -b/(2*a); r2=-b/(2*a); printf(“root 1= %f and root =%f”,r1 r2); } else if (d>0) { printf(“roots are real & different”); r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); printf(“root1=%f”,r1); printf(“root2=%f”,r2); } else { Printf(“roots are imaginary”); } getch(); } INPUT : enter a b c values 1 5 6 OUTPUT : roots are real & different root1 = 3.000000 root 2 = 2.000000 WRITE A C PROGRAM TO PERFORM ARTHIMETIC OPERATIONS USING SWITCH …..CASE STATEMENT. #include<stdio.h> #include<conio.h> void main () { int n1, n2, ch ; clrscr(); printf(“enter the first number”); scanf(“%d”, &n1); printf(“enter the second number”); scanf(“%d”,&n2); printf(“1. addition”); printf(“2.substraction”); printf(“3.multiplication”); printf(“4.division”); printf(“enter your choice from 1 to 4); scanf(“%d”, & ch); switch(ch) { case 1: printf (“\n %d + %d= %d”,n1, n2 ,n1+ n2); case 2: printf(“\n %d -%d= %d”,n1 , n2 n1 –n2); Case 3: printf (“\n %d * %d=%d “,n1 ,n2 , n1*n2); break; case4 : printf(“\n %d /%d= %d”,n1 ,n2,n1/ n2); break; } getch(); } INPUT: enter the first number 144 enter the second number 12 1.ADDITION 2.SUBSTRACTION 3MULTIPLICATION 4.DIVISION ENTER YOUR CHOICE FROM 1 TO 4. 4 OUTPUT : 144/12 =12 WRITE A C PROGRAM TO PRINT SUM OF ‘n’NATURAL NUMBERS USING WHILE LOOP . #include<stdio.h> #include<conio.h> void main() { int i=1, sum =0,n; clrscr(); printf(“enter the value of n”); scanf(“%d”,&n); while(i <=n) for (i=1;I,=num ; i++) { printf(“%d \n”, i); sum = sum + I; } printf(“\n sum of natural numbers from 1 to %d is :%d \n “,num , sum); getch(); } INPUT : Enter any positive number 10 Natural numbers from 1 to 10 : 1 2 3 4 5 6 7 8 9 10 OUTPUT : sum of natural numbers from 1 to 10 is :55 WRITE A C PROGRAM TO CHECK THE GIVEN NUMBER IS PALINDROME OR NOT USING WHILE LOOP. #include < stdio .h > #include<conio.h > void main() { int n, c , s = 0 ; clrscr (); printf (“Enter any number ; ‘ ) ; scanf (“% d “ , & n) ; c=n ; while ( n > 0 ) { r = n % 10 ; } if(c==s) printf(“palindrome number”); else printf(“not”); getch(); } INPUT : enter any number 345 OUTPUT : not a palindrome number WRITE A C PROGRAM TO PRINT THE NUMBER FROM 1 TO 10 USING WHILE AND FORLOOP. #include<stdio.h> #include<conio.h> void main () { int i=1; clrscr(); while (i<10) { printf(“%d \n”, I ); i=i+1; } printf(“\n”); getch(); } OUTPUT : 1 2 3 4 5 6 7 8 9 10 FOR LOOP : #include<stdio.h> #include<conio.h> void main () { int i ; clrscr(); for (i=1;i<10;i++) { printf(“%d \n” , i); printf(“\n”); } getch(); } OUTPUT : 1 2 3 4 5 6 7 8 9 10 WRITEA C PROGRAM TO PRINT THE REVERSE OF GIVEN NUMBER USING WHILE LOOP . #include<stdio.h> #include<conio.h> void main () { int rev =0,rem,n,r; clrscr(); printf(“enter a number :”); scanf(“%d”, &n); x=n while(n>0) { rem +n% 10; rev =rev *10+rem; n=n/10; } printf(“\n reverse of a number %d is %d”, x , rev ); getch(); } INPUT : enter a number 1324 OUTPUT : revrse of a number 1324 is 4231. WRITEA C PROGRAM TO PRINT FACTORIAL VALUE OF A GIVEN NUMBER USING WHILE LOOP FOR LOOP #include<stdio.h> #include<conio.h. void main() { int n, I, fact =1; clrscr(); printf(“enter any no:”); scanf(“%d”, &n); for(i=n;i>=1;i--) { fact=fact*I; } Printf(“factorial =%d”, fact); getch(); } WHILE LOOP : #include<stdio.h> #include<conio.h> void main() { int I, n , a=0, b1 , temp ; printf(“enter the value of n”); scanf(“%d”, &n); for(i1;i<=n; i++) { printf(“%d”, a); temp =a +b; a=b; b=temp; } INPUT : enter the value of the n 10 OUTPUT : 0112358132134 WRITEA C PROGRAM TO PRINT THE FOLLOWING PATTERN USING NESTED LOOPS . * ** *** #include<stdio.h> #include<conio.h> void main () { int I, j; clrscr(); for(i=1;i<=3;i++); { for(j=1;j<=I;j++) { printf(“*”); } printf(“\n”); } getch(); } OUTPUT : * ** *** WRITE A C PROGRAM TO PRINT THE FOLLOWING PATTERN USING NESTED LOOPS. 1 1 2 1 2 3 1 2 3 4 #include<stdio.h> #include<conio.h> void main() { int I , j : clrscr(); for(i=1 ; i<=4 ; i++) { for(j=1; j<=4; j++) { printf(“%d”, j); } printf(“\n”); } getch(); } OUTPUT : 1 1 2 1 2 3 1 2 3 4 WRITE A C PROGRAM TO CONVERT A DECIMAL NUMBER TO A BINARY NUMBER USING FOR LOOP #include<stdio.h> #include<conioh> void main () { long b[20],n , r , c=0,I; clrscr(); printf(“enter a decimal number :”); scanf(“%d”,&n); while(n>0) { r=n%2; b[c]=r; n=n/2; c++; } printf(“\n the binary equivalent is :”); for(i=c-1;i>=0;i--) printf(“%ld” , b[i]); getch(); } OUTPUT : Enter a decimal number :123 The binary equivalent is : 1111011 WRITE A C PROGRAM TO UNDERSTAND THE USE OF CONTINUE #include<stdio.h> void main () { int n ; for (n=1;n<=5;n++) { if(n==3) { printf(“ I understand the use of continue \n”); continue; } printf(“number =%d \n “,n); } printf(“out of for loop \n”); } OUTPUT : Number =1 Number = 2 I understand the use of continue Number = 3 Number =4 Out of for loop