02 Decision making 2.1 Decision making and branching if statement (if, if-, -if ladder, nested if-) Switch case statement, break statement. (14M) 2.2 Decision making and looping while, do, do-while statements for loop, continue statement (14M) Marks 28 CO204.3 Implement the C program using control structure 2.1 The if Statement The general form of if statement looks like this if ( test condition ) Statements; The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses (). If the condition is true, then the statements are executed. If the condition is not true then the statement is not executed. E.g. if ( x = = 10) y = x+ 5; Here, if the value of x is equal to integer constant 10 then statements within curly brackets i.e. y = x+5 will be executed otherwise skipped. e.g. main( ) int x = 10, y; if (x = = 10) y = x+ 5; printf( Value of y = %d \n, y); printf( Hello to the World of C ); Output: Value of y = 15 Hello to the World of C E.g. main( ) 1
int x = 5, y; if (x = = 10) y = x+ 5; printf( Value of y = %d \n, y); printf( Hello to the World of C ); Output: Hello to the World of C The if- Statement General form if (test condition) // if block starts Statements_1; Statements_2; // if block ends // block starts Statements_3; Statements_4; // block ends Here, based on condition is true or false if block or block will be executed. If condition is true then if block will be executed i.e. statements 1 and 2 will be executed. If condition is false then block will be executed i.e. statements 3 and 4 will be executed. Only one block, either if block or block, will be executed. E.g. main( ) int num1 = 10, num2 = 30; if ( num1 > num2) printf( Number 1 Is Greater ); printf( Number 2 Greater ); Here, condition checks which number is greater. Output: Number 2 Greater As the condition is false statement within block is executed. I.e. Number 2 is greater. 2
Nested if- We can write an entire if- construct within the body of the if statement and within the body of an statement. This is called nesting of ifs. General form if (test condition 1) if(test condition 2) Statement 1; Statement 2; Statement 3; Statement 4; Statement 5; Statement 6; Here, condition 1 is checked first, if it returns true then inner if condition 2 is checked. If condition 2 is true then statements 1 and 2 are executed. If condition 2 is false then statements 3 and 4 are executed. If condition 1 is false then block will be executed i.e. statements 5 and 6 will get executed. if ladder Used when multipath decision is involved General form if ( condition 1) Statement_1; if (condition 2) Statement_2; if (condition 3) Statement_3; 3
if (condition 4) Statement_4; Statement_5; Conditions are evaluated from top to bottom still true condition is found. As soon as true condition is met, block of statements associated with it is executed and rest of the if ladder is skipped. When all the conditions become false then the final block is executed. Programs: 1. Program to find biggest number among 3 numbers using if ladder /* Program to find biggest number among 3 numbers */ int no1, no2, no3; printf("enter 3 numbers : "); scanf("%d%d%d", &no1,&no2,&no3); if( no1 > no2 && no1 > no3) printf("biggest Number is %d", no1); if( no2 > no1 && no2 > no3) printf("biggest Number is %d", no2); if( no3 > no2 && no3 > no1) printf("biggest Number is %d", no3); printf(" All numbers are equal"); 2.Program to find biggest number among 3 numbers using if int no1, no2, no3; printf("enter 3 numbers : "); scanf("%d%d%d", &no1,&no2,&no3); if( no1 > no2 && no1 > no3) 4
printf("biggest Number is %d", no1); if( no2 > no1 && no2 > no3) printf("biggest Number is %d", no2); printf("biggest Number is %d", no3); 3.Program to accept marks of 5 subjects. Calculate total and average. Based on average determine the class obtained. int m1, m2, m3, m4, m5, total; float per; printf("enter marks of subject "); scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5); total = m1+m2+m3+m4+m5; per = (float) total/5; printf("\n Percentage is %f", per); if( per >= 75) printf("\nyou got Distinction "); if ( per >= 60) printf("\nyou got First Class "); if ( per >= 50) printf("\nyou got Second Class "); if ( per >= 40) printf("\nyou got Pass Class "); printf("\nsorry you failed"); 5
int m1, m2, m3, m4, m5, total; float avg; printf("enter marks of subject "); scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5); total = m1+m2+m3+m4+m5; avg = (float) total/5; printf("\n Avg is %f", avg); if( avg >= 75) printf("\nyou got Distinction "); if ( avg >= 60) printf("\nyou got First Class "); if ( avg >= 50) printf("\nyou got Second Class "); if ( avg >= 40) printf("\nyou got Pass Class "); printf("\nsorry you failed"); 5. Program to find biggest between two numbers. 6
int no1, no2; //To clear the screen printf("enter two numbers "); scanf("%d%d", &no1,&no2); if (no1 > no2) printf("\nbigger number is %d", no1); printf("\nbigger number is %d", no2); 6. Program to find number is even or odd int num; printf("enter a number "); scanf("%d", &num); if(num % 2 == 0) printf("\n Entered number %d is Even",num); printf("\n Entered number %d is Odd",num); 7. Program to find number is positive or negative int num; printf("enter the number"); scanf("%d", &num); if(num > 0) printf("\n %d is positive number", num); printf("\n %d is negative number", num); 7
8. Find entered character is vowel or not char ch; printf("enter a character "); scanf("%c", &ch); if( ch == 'a' ch == 'e' ch == 'i' ch == 'o' ch == 'u') printf("entered character %c is a Vowel", ch); printf("entered character %c is not a Vowel", ch); 9. Program to find smallest number among 3 numbers int no1, no2, no3; printf("enter three numbers "); scanf("%d%d%d", &no1,&no2,&no3); if ( no1 < no2 && no1 < no3) printf("\nsmallest number is %d ", no1); if ( no2 < no1 && no2 < no3) printf("\nsmallest number is %d ", no2); printf("\nsmallest number is %d ", no3); 10. Program to find whether entered year is Leap year */ 8
int year; printf("enter year"); scanf("%d", &year); if ((year % 4 == 0 && year % 100!= 0) year % 400 == 0) printf("enter year %d is Leap Year", year); printf("enter year %d is not Leap year", year); switch case statement The control statement that allows us to make a decision from the number of choices. General form switch (expression ) case value_1 : Block of statements; case value_2 : Block of statements; case value_3 : Block of statements; case value_4 : Block of statements; default : Block of statements ; First, the expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the values that follow the case statements. When a match is found, the program executes the statements following that case, and exits from switch case. If no match is found with any of the case statements, only the statements following the default are executed. E.g. main( ) int i = 2 ; switch ( i ) 9
case 1 : printf ( "I am in case 1 \n" ) ; break ; case 2 : printf ( "I am in case 2 \n" ) ; break ; case 3 : printf ( "I am in case 3 \n" ) ; break ; default : printf ( "I am in default \n" ) ; The output of this program would be: I am in case 2 Program to find entered character is vowel or not using switch case char ch; printf("enter a character "); scanf("%c", &ch); switch(ch) case 'a': case 'e': case 'i': case 'o': case 'u': printf("\nentered character %c is vowel ", ch); default : printf("\nentered character %c is not vowel ", ch); Menu driven program to perform 1.Addition 2.Substraction 3.Division 4.Multiplication 10
int x, y, z, opt; printf(" Enter two numbers "); scanf("%d%d", &x,&y); printf("\n 1. Addition"); printf("\n 2. Substraction"); printf("\n 3. Multiplication"); printf("\n 4. Division"); printf("\nenter Your Option"); scanf("%d",&opt); switch(opt) case 1 : z = x+y; printf("\naddition is %d", z); case 2 : z = x - y; printf("\nsubstraction is %d",z); case 3 : z = x * y; printf("\nmultiplication is %d",z); case 4 : z = x / y; printf("\ndivision is %d",z); default : printf("invalid choice"); Q. Write a menu driven program for following options 1. Find a number even or odd 2. Sum of two numbers int opt, num, x,y,z; printf("\n 1. Even Or Odd"); printf("\n 2. Sum of two Numbers"); printf("\nenter Your Option"); scanf("%d",&opt); switch(opt) 11
case 1 : printf("enter a number "); scanf("%d", &num); if(num % 2 == 0) printf("\n Entered number %d is Even",num); printf("\n Entered number %d is Odd",num); case 2 : printf("enter two numbers "); scanf("%d%d", &x, &y); z = x + y; printf("\nsum is %d",z); default : printf("invalid choice"); Loops (iteration) The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction. There are three methods by way of which we can repeat a part of a program. They are: (a) Using a for statement (b) Using a while statement (c) Using a do-while statement The while Loop The general form of while is as shown below: initialise loop counter ; while ( test condition ) Statements; // block of while loop increment / decrement of loop counter ; The statements within the while loop would keep on getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop. As a rule the while must test a condition that will eventually become false, otherwise the loop would be executed forever indefinitely. 12
Q. Program to find Factorial of a number using while loop int i = 1, fact = 1, num; printf("enter a number to find Factorial "); scanf("%d", &num); while( i <= num ) fact = fact * i; i++; printf ("\n Factorial of entered number %d is %d ", num, fact); Q. Fibonacci series using while loop int i=1, fab, s1 = 0,s2 = 1, num; printf("enter a number"); scanf("%d", &num); printf(" 0 \n 1 "); while( i <= num-2) fab = s1 + s2; printf("\n %d ", fab); s1 = s2; s2 = fab; i++; Q. Program find number is prime or not using while loop int i=2, num; printf("enter a number to find prime or not "); scanf("%d", &num); while( i < num ) if(num % i == 0) printf("enter number %d is not prime", num); 13
i++; if ( num == i ) printf("enter number %d is prime", num); Q. Program to print a number in reverse order int num,rem; printf("enter the number"); scanf("%d",&num); while( num!= 0 ) rem = num % 10; printf("%d",rem); num = num / 10; Q. Program to calculate sum of digits of a number int num,rem, sum = 0; printf("enter the number"); scanf("%d",&num); while( num!= 0 ) rem = num % 10; sum = sum + rem; num = num / 10; printf("sum of digits of entered number is %d",sum); Q. Program to print first 10 odd numbers using while loop int i = 1; while(i<= 20) if(i % 2!= 0) 14
i++; printf("\n%d",i); Q.Program to print first 10 even numbers using while loop int i = 1; while(i<= 20) if(i % 2 == 0) printf("\n%d",i); i++; Q.Program to find sum of series 1+2+3+...+n int i=1, sum = 0, num; printf("enter the Number "); scanf("%d",&num); while(i<=num) sum = sum + i; i++; printf("sum of series is %d ",sum); The do-while Loop do Block of statements; while (test condition); the do-while tests the condition after having executed the statements within the loop. do-while would execute its statements at least once, even if the condition fails for the first time. 15
E.g. main( ) do printf ( "Hello there \n") ; while ( 4 < 1 ) ; In this program the printf( ) would be executed once, since first the body of the loop is executed and then the condition is tested. The for Loop The for allows us to specify three things about a loop in a single line: (a) Setting a loop counter to an initial value. (b) Testing the loop counter to determine whether its value has reached the number of repetitions desired. (c) Increasing the value of loop counter each time the program segment within the loop has been executed. The general form of for statement is as under: for ( initialize counter ; test condition ; increment/decrement counter ) Statements://Block of for loop; When the for statement is executed for the first time, the value of counter is set to an initial value. Then the condition is tested. If condition is true then the body of the loop is executed for the first time. Upon reaching the closing brace of for, the value of counter gets incremented. Again the condition is checked and if result is true then statements within loop of body is executed. Body of loop is executed till condition is true. When condition gets false the control exits from the loop and is transferred to the statement (if any) immediately after the body of for loop. 1. Program to find first 10 even numbers using for loop int i; for ( i = 2 ; i <= 20 ; i = i + 2) printf("%d \n", i); 16
2. Fabonacci series using for loop int i, fab, s1 = 0,s2 = 1, num; printf("enter a number"); scanf("%d", &num); printf(" 0 \n 1 "); for ( i = 1; i <= num-2; i++) fab = s1 + s2; printf("\n %d ", fab); s1 = s2; s2 = fab; 3. Factorial of a number using for loop int i, fact = 1, num; printf("enter a number to find Factorial "); scanf("%d", &num); for ( i = 1; i <= num ; i++) fact = fact * i; printf ("\n Factorial of entered number %d is %d ", num, fact); 4. Program find number is prime or not using for loop int i, num; printf("enter a number to find prime or not "); scanf("%d", &num); for ( i = 2 ; i < num ; i++) if(num % i == 0) printf("enter number %d is not prime", num); 17
if ( num == i ) printf("enter number %d is prime", num); 5. Sum of series 1+2+...+n using for loop int i, sum = 0, num; printf("enter a number "); scanf("%d", &num); for ( i = 1; i <= num ; i++) sum = sum + i; printf ("\n Sum of entered series %d is %d ", num, sum); 6. Write a program using loop to print following : * * * * * * int i,j; for(i=1; i <= 5 ; i++) for(j = 1; j <=i; j++) printf(" * \t"); printf("\n"); Assignment 4. Q.1. Explain break and continue statement with example. The break Statement When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if. E.g. 18
main( ) int i = 1 ; while ( i <= 10 ) if ( i == 5 ) break ; printf ( "%d \n", i ) ; i++; printf( Back in Main function ); Output: 1 2 3 4 Back in Main function In this program when i equals 5, break takes the control outside the while loop to statement next to end of while loop. The continue Statement When continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is usually associated with an if. E.g. int i; for(i=1; i<=5; i++) if(i==2) continue; printf("%d\n",i); printf("in main function"); The output of the above program would be... 1 3 4 5 In main function 19
Here, when the value of i equals that of 2, the continue statement takes the control to the beginning of the for loop. Q. 2. Write syntax of if- statement. Q. 3. State any four control statement. Q. 4. State any decision making statements. Q. 5. Explain -if ladder with execution. Q. 6.Explain working of do-while loop with syntax and example. Q. 7.State difference between if and switch statement. if statement 1. The general form of if statement looks like this if ( test condition ) Statements; switch statement 1. switch (expression ) case value_1 : Block of statements; case value_2 : Block of statements; default : Block of statements ; 2. Only one condition can be checked to execute the 2. Multiple constant value can checked to execute block or to skip. statements. 3. Can test relational or logical expression. 3. Can test only for equality. 5. Same condition can be repeated number of times. 4.no two cases can have same constant in a switch Q. 8. Explain switch-case statement with syntax and small example. Q. 9. State use of break and continue statement. Q. 10. State and explain if, if-, nested if-, for loop, while loop with an example and flow chart. Q. 11. Difference between while and do-while loops. Q. 11. Programs 1. Write a program to swap the values of two integer numbers. 2. Write a program to find out largest number between 3 integer numbers. 3. Write a program to accept a character from user and find whether character is vowel or not. 4. Write a program to accept a character from user and find whether character is vowel or not, using switch case. 5. Write a program to display following menu and do operation as per user choice, using switch case: 1. Addition 2. Subtraction 3. Multiplication 4. Division 6. Write a program to display the sum of all even numbers from 1 to n. Accept n from user. 7. Write a program to calculate sum of digits of five digit integer number. 8. Write a program to generate a Fibonacci series of first 10 numbers. 9. Write a program to print factorial of a number. 10. Write a program to print whether number is prime or not. 11. Write a program using loop to print following : * * * * * * 12. Write a program to display odd numbers from 1 to n. where n is accepted from user. 13. Write a program to generate a Fibonacci series using while loop. 14. Write a program to print first 10 odd numbers using while loop. 20
15. Write a program to calculate sum of series 1+2+3+..+n using while loop. 16. If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred. 17. Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not. 21