0% found this document useful (0 votes)
15 views12 pages

Loops - While Loop

Uploaded by

are85reh
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
15 views12 pages

Loops - While Loop

Uploaded by

are85reh
Copyright
© © All Rights Reserved
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/ 12

Example 1.2 Rewrite the example 1.

2 using ternary
Example 1.1 Rewrite the following program without operators
using the AND (&&) operators. #include <stdio.h>
# include <stdio.h> int main()
int main( ) {
{ int x;
float m1, m2, m3, m4, m5 ; printf("Enter any number\n");
float percentage; scanf("%d",&x);
printf ( "Enter marks in five subjects \n" ) ; If(x==1)
scanf ( "%f %f %f %f %f", &m1, &m2, &m3, &m4, &m5 ) ; printf("It's Sunday");
percentage = ( m1 + m2 + m3 + m4 + m5 ) / 500 * 100 ; else
if ( percentage >= 60 ) printf("It’s NOT Sunday");
printf ( "First division\n %.2f", percentage ) ; return 0;
if ( percentage >= 50 && percentage < 60 )
}
printf ( "Second division\n %.2f", percentage ) ;
if ( percentage >= 40 && ( percentage < 50)
printf ( "Third division\n %.2f" , percentage ) ;
if ( percentage < 40 )
printf ( "Fail\n %.2f", percentage ) ;
return 0;}
# include <stdio.h>
# include <stdio.h> int main( )
int main( ) {
{ float m1, m2, m3, m4, m5 ;
float m1, m2, m3, m4, m5 ; float percentage;
printf ( "Enter marks in five subjects \n" ) ;
float percentage;
scanf ( "%f %f %f %f %f", &m1, &m2, &m3, &m4,
printf ( "Enter marks in five subjects \n" ) ; &m5 ) ;
scanf ( "%f %f %f %f %f", &m1, &m2, &m3, &m4, &m5 ) ; percentage = ( m1 + m2 + m3 + m4 + m5 ) / 500 * 100 ;
percentage = ( m1 + m2 + m3 + m4 + m5 ) / 500 * 100 ; if ( percentage >= 60 )
if ( percentage >= 60 ) printf ( "First division\n %.2f", percentage ) ;
printf ( "First division\n %.2f", percentage ) ; else if (percentage >=50 )
if ( percentage >= 50 && percentage < 60 ) printf ( "Second division\n %.2f", percentage ) ;
printf ( "Second division\n %.2f", percentage ) ; else if ( percentage >=40)
if ( percentage >= 40 && ( percentage < 50) printf ( "Third division\n %.2f" , percentage ) ;
printf ( "Third division\n %.2f" , percentage ) ; else
if ( percentage < 40 ) printf ( "Fail\n %.2f", percentage ) ;
printf ( "Fail\n %.2f", percentage ) ; return 0;}
return 0;
}
Example 1.2 using ternary operator Example 1.2 using if-else
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int x; int x;
printf("Enter any number\n"); printf("Enter any number\n");
scanf("%d",&x); scanf("%d",&x);
printf(x==1? "It's Sunday": "It's NOT Sunday"); If(x==1)
return 0; printf("It's Sunday");
} else
printf("It’s NOT Sunday");
return 0;
}
Loop means to perform a set
of instructions repeatedly.
The while Loop
This involves repeating some The while loop is ideally suited for situations where we
portion of the program either a wish to repeat some instructions till a condition remains
specified number of times or until a true.
particular condition is satisfied. This
repetitive operation is done through Example: 1.1
#include <stdio.h>
a loop control instruction. There are int main(){
three ways to repeat a part of a
program. int i = 1 ;
(a) Using a while statement while ( i <= 5 )
(b) Using a for statement {
(c) Using a do-while statement printf ( " i= %d My first While loop\n",i ) ;
i=i+1;
}
return 0;
}
1) #include <stdio.h>
2) #include <conio.h>
Example: 1.2
3) int main(){
• Write a C program to calculate simple interest
for 3 sets of values of principal, number of 4) int principal, years, count ;
years and rate of interest. 5) float rate, simple_interest ;
6) count = 1 ;
START
7) while ( count <= 3 )

initialise
8) {
9) printf ( "\nEnter values of principal, years and rate " ) ;

test
False 10) scanf ( "%d %d %f", &principal, &years, &rate ) ;
11) simple_interest = principal * years * rate / 100 ;
True

body of loop
12) printf ( "Simple interest = Rs. %.2f", simple_interest ) ;
13) count = count + 1 ;
increment
STOP 14) }
15) getch();
16) return 0 ;
17) }
Example: 1.3. Indefinite execution Example: 1.4 Decrement
#include <stdio.h> Operation
#include <stdio.h>
#include <conio.h> #include <conio.h>
int main(){ int main(){
int i = 1 , j=0; int i = 5 ;

while ( i <= 10 )
while ( i >= 1 )
printf ( "value of j = %d and value of i= %d\n", j++,i ) ;
{
getch();
printf ( "Make the computer literate\n" ) ;
return 0 ;
i = i - 1 ; // i- - OR - -i;
}
}
getch();
return 0 ;
}
Example: 1.5 (float as loop Example: 1.6 Find mistake if any
counter)
1) #include <stdio.h> 1) # include <stdio.h>
2) #include <conio.h> 2) int main( )
3) int main(){ 3) {
4) float a = 10.0 ; 4) int i = 1 ;
5) while ( a <= 10.5 )
5) while ( i <= 10 ) ;
6) {
6) {
printf ( "Raindrops on roses...\n" ) ;
printf ( "...and whiskers on kittens\n" ) ; printf ( "%d\n", i ) ;
7) a = a + 0.1 ; i=i+1;
8) } 7) }
9) getch(); 8) return 0 ;
10) return 0 ; 9) }
11) }
Example: 1.7 Connecting Logical Example: 1.8 Connecting Logical
Operators (&&) Operators (OR)
1) # include <stdio.h> 1) # include <stdio.h>
2) int main( ) 2) int main( )
3) { 3) {
4) int i = 12 ; 4) int i = 12 ;
5) int j = 8;
5) int j = 8;
6) while ( i >= 10 && j <= 15 )
6) while ( i >= 10 || j <= 15 )
7) {
printf ( "value of i = %d and value of j = %d\n", i, j ) ; 7) {
i = i + 1; printf ( "value of i = %d and value of j = %d\n", i, j ) ;
j=j+1; i = i + 1;
8) } j=j+1;
9) return 0 ;
8) }
10) }
9) return 0 ;
10) }
Example: 1.9 Continuous loop until a certain Compound Assignment Operator.
condition is met
1) # include <stdio.h> 1) i += 1. is the same as i = i+1.
2) #include <conio.h> 2) j += 10. is the same as j = j + 10
3) k -= 1. is the same as k = k - 1.
3) int main( ) 4) k /= 2. is the same as k = k / 2.
4) { 5) A %= 2 is the same as A = A
5) char x ; % 2.

6) while ( x != 'z' )
7) {
printf ( "\nEnter your value (any Alphabet except z)\t" ) ;
scanf (" %c", &x);
printf ("you entered %c", x);
8) }
9) getch();
10) return 0 ;
11) }
Post and Pre Increment/Decrement
 In the statement while ( i++ < 10 ), first 1) # include <stdio.h>
the comparison of value of i with 10 is 2) int main( )
performed, and then the incrementation of i
takes place. Since the incrementation of i 3) {
happens after the comparison, here the ++ 4) int i = 0 ;
operator is called a post-incrementation operator.
 In the statement while ( ++i <= 10 ), first 5) printf ("First value of i = %d in while ( i++ < 10 ) \n", i );
incrementation of i takes place, then the 6) while ( i++ < 10 )
comparison of value of i with 10 is performed.
printf ( "Inside while loop %d\n", i ) ;
Since the incrementation of i happens before the
comparison, here the ++ operator is called a 7) return 0 ;
pre-incrementation operator.
8) }
1) # include <stdio.h>
Example 1.10 2) # include <conio.h>
3) int main( )
Write a C program for a simple POS (Point-of-Sale)
system that: 4) {
5) int item,price, sum ;
1. Continuously asks the user to enter the quantity of
items and price per item. 6) printf ("\t Welcome to XYZ POS.\n\n");
2. Calculates and displays the amount for each entry 7) while ( item != 0 )
(quantity * price).
8) {
3. Keeps a running total of all items' costs.
4. Exits and displays the total amount when the user 9) printf ( "Enter quantity of items or 0 to exit \
enters 0 as the quantity. n" ) ;
10) scanf ("%d", &item);
11) if (x==0) break;
12) printf ( "Enter price per item\n" ) ;
13) scanf ("%d", &y);
14) sum = sum + x*y;
15) printf ("Amount Rs: %d \n\n", x*y);
16) }
17) printf ("Total amount Rs: %d", sum);
18) getch();
19) return 0 ;
1) # include <stdio.h> 19) if (matchsticks-user>1)
Game
Write a program for a 2) # include <conio.h> 20) {
matchstick game being 3) int main( ) 21) computer=5-user;
played between the 4) {
computer and a user. 22) printf("\nComputer's Turn..\n" );
Your program should 5) int matchsticks=21, user, computer;
6) printf("\t Don't enter number less than 1 and 23) printf("\nComputer chooses:%d",
ensure that the
greater 4. Otherwise, computer wins.\n"); computer);
computer always wins.
24)
Rules for the game are
25) matchsticks=matchsticks-user-computer;
as follows: 7) while (matchsticks>=1)
26) continue;
1. There are 21 8) {
27) }
matchsticks. 9) printf("\nNumber of matchsticks
available right now is %d.", matchsticks);
2. The computer asks
10) printf("\n\nYour Turn...\n\n"); 28) if(matchsticks==1)
the player to pick 1,
2, 3, or 4 11) printf("\nPick up the matchstick(s)-- (1-4): "); 29) break;
matchsticks. 30) }
12) scanf ("%d", &user);
3. After the person 13) if (user>4)
31) matchsticks--;
picks, the computer 32) printf("\nComputer Wins");
does its picking. 14) {
15) printf("Invalid Selection");
4. Whoever is forced to 33) getch();
pick up the last 16) break;
34) return 0 ;
matchstick loses the 17) }
35) }

You might also like