Unit 3-Control Flow Lect 1
Unit 3-Control Flow Lect 1
-Ujwala Wanaskar
CONTENTS:
printf(“put on lights”);
DIFFERENT FORMS OF IF STATEMENTS
1. simple if statement
2. if……else statement
3. Nested if…..else statement
4. else if ladder
SIMPLE IF STATEMENT
General form:
if(test expression)
{
Statement block;
}
Statement-x;
PROGRAM TO CHECK WHETHER TWO NUMBERS
ARE EQUAL OR NOT?
int main()
{
int num1,num2;
printf(“Enter any 2 numbers”);
scanf(“%d %d”,&num1,&num2);
if(num1==num2)
printf(“Two numbers are equal”);
return 0;
}
PROGRAM TO CHECK WHETHER THE WEIGHT OF A STUDENT IS LESS THAN
50KGS AND HEIGHT IS GREATER THAN 170CMS.
int main()
{
int wt,ht;
printf(“Enter Weight and height of a student”);
scanf(“%d %d”,&wt,&ht);
If(wt<50 && ht>170)
Printf(“You are inn………”);
Return 0;
}
THE IF……ELSE STATEMENT
REPRESENTATION USING FLOWCHART :
WAP TO FIND WHETHER A GIVEN NUMBER IS EVEN OR ODD
int main()
{
int num;
printf(“Enter any number”);
scanf(“%d”,&num);
If(num%2==0)
Printf(“Number is even”);
Else
Printf(“Number is odd”);
Return 0;
}
NESTING OF IF-ELSE STATEMENT
WRITE A PROGRAM IN C TO FIND A GREATEST NUMBER OUT OF THE THREE NUMBERS ENTERED BY
THE USER.
If(a>b) else
{ {
if(a>c) if(b>c)
printf(“a is greatest”); printf(“b is greatest”);
else else
printf(“”c is greatest”); printf(“c is greatest”);
} }
ELSE-IF LADDER
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
EXAMPLE :
#include<stdio.h> else if(number==100){
int main(){ printf("number is equal to 100");
int number=0; }
printf("enter a number:"); else{
scanf("%d",&number); printf("number is not equal to 10, 50 or 100");
if(number==10){
}
printf("number is equals to 10");
return 0;
}
}
else if(number==50){
printf("number is equal to 50");
}
YOU ARE WORKING ON AN E-COMMERCE WEBSITE. THE COMPANY HAS A SPECIAL OFFER WHERE:
IF A CUSTOMER BUYS MORE THAN 10 ITEMS OF A SINGLE PRODUCT, THEY GET A 5% DISCOUNT.
IF A CUSTOMER BUYS MORE THAN 50 ITEMS OF A SINGLE PRODUCT, THEY GET A 10% DISCOUNT.
#include <stdio.h> if(itemsPurchased > 10 && itemsPurchased <=
int main() { 50) {
int itemsPurchased = 25; // Fixed value for discountedPrice = totalPrice * 0.95; // 5%
this scenario discount
double originalPrice = 2.0; // Example price
per item printf("You get a 5%% discount! Total price
after discount: $%.2lf\n", discountedPrice);
double discountedPrice;
}
•As a result, the current iteration of the loop gets skipped and the
control moves on to the next iteration.
Y=xn
do while STATEMENT:
•The do/while loop is a variant of the while loop.
•This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the condition
is true.
Syntax:
do {
// code block to be executed
}
while (condition);
WAP TO PRINT NUMBERS FROM 0 TO 10 USING DO
WHILE LOOP
# include<stdio.h> return 0;
Int main() }
{
int i; O/P:0 1 2 3……..10
i=0;
do
{
Printf(“%d\t”,i);
i++;
}while(i<=10);
TRY…
WAP in C to print sum of numbers from 1 to 10.
for LOOP:
•The for loop is an entry-controlled loop that executes the
statements till the given condition.
•All the elements (initialization, test condition, and increment) are
placed together to form a for loop inside the parenthesis with
the for keyword.
•Syntax:
•for (init; condition; increment)
•{
•statement(s);
•}
WAP TO PRINT NUMBERS FROM 0 TO 10 USING FOR
LOOP
# include<stdio.h>
Int main()
{
int i;
For(i=0;i<=10;i++)
{
Printf(“%d\t”,i);
}
Return 0;
}
TRY….
WAP in C to print all even numbers from 1 to 50 using for loop.
WAP to find factorial of a given number
WAP to Fibonacci series upto n terms.
WAP to find sum of digits of a number.
WAP to check number is palindrome or not(ex 121)
WAP to check number is Armstrong number or not (ex.
153=1*1*1+5*5*5+3*3*3,370,371,407)
WAP to check number is prime or not.