Presentation is loading. Please wait.

Presentation is loading. Please wait.

WHILE, DO-WHILE AND FOR LOOPS

Similar presentations


Presentation on theme: "WHILE, DO-WHILE AND FOR LOOPS"— Presentation transcript:

1 WHILE, DO-WHILE AND FOR LOOPS

2 Program to check whether a given number is prime or not.
void main() { int i=1, n, count=0; printf(“Enter a number”); scanf(“%d”,&n); while(i<=n) { if(n%i==0) count++; i++; } if(count==2) printf(“It is prime number”); else printf(“Not a prime number”);

3 Class work Problems 1 write a C program to print “Welcome to C programming” 5 times, each time in new line. 2 Write a C program to display all the prime numbers between 50 and 100.

4 Program to find the factorial of a given integer using entry control while loop
void main() { int i=1, n, fact=1; printf(“Enter the number”); scanf(“%d”, &n); while(i<=n) fact = fact * i; i++; } printf(“Factorial=%d”, fact);

5 Program to find the factorial of a given integer using exit control do-while loop
void main() { int i=1, n, fact=1; printf(“Enter the number”); scanf(“%d”, &n); do fact = fact * i; i++; } while(i<=n); printf(“Factorial=%d”, fact); }

6 Difference between While and Do- While Loop
It is an Entry Control Loop. Condition or Test expression is checked at the beginning. It is an Exit Control Loop. Condition or Test expression is checked at the last. Statements inside the while loop will be executed only if the condition or test expression is true. Statements inside the do-while loop will execute at least once even if the condition or test expression is false.

7 Difference Example void main() { int a=5,b=6; while(a>b)
printf(“\n%d”, a); a++; } getch(); do } while(a>b);

8 Sum of digits of a number
void main() { int n, i, sum=0, rem; printf(“Enter any number”); scanf(“%d”, &n); while(n>0) rem = n % 10; sum = sum + rem; n = n / 10; } printf(“Sum of digits of number=%d”, sum);

9 Program to reverse a number
void main() { int n, rev=0, rem; printf(“Enter any number”); scanf(“%d”, &n); while(n>0) rem = n % 10; rev = rev * 10 + rem; n = n / 10; } printf(“Reverse of given number=%d”, rev);

10 Classwork problems 1 write a program to check whether a given number is Harshad number or not using do-while loop or exit control loop. 2 write a program to print all the harshad numbers between 1 and 100 using entry control loop or while loop. 3 write a program to check whether a given number is palindrome or not.

11 For loop for(initialization ; test expression ; updation) { statements; }

12 Example to print first n odd natural numbers
void main() { int limit, i; printf(“Enter the limit”); scanf(“%d”, &limit); for(i=1 ; i<=limit ; i++) if( i % 2!= 0 ) printf(“%d”, i); } getch();

13 Classwork problems 1 write a program to check whether the given number is perfect or not using for loop.

14 Program to generate Fibonacci series
void main() { int a=0, b=1, c, i, limit; printf(“Enter the limit”); scanf(“%d”, &limit); if(limit==1) printf(“%5d”, a); else if(limit==2) printf(“%5d%5d”, a, b); else { for(i=1 ; i<=n-2 ; i++) c = a + b; printf(“%5d”, c); a = b; b = c; } getch()

15 Classwork problems 1 write a program to generate the Lucas sequence. 2 write a program to check whether a given 3 digit number is armstrong or not?

16 Program to generate Lucas Sequence
void main() { int a=1,b=3,c,n,i; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); if(n==1) printf("%5d",a); else if(n==2) printf("%5d%5d",a,b); else { for(i=1;i<=n-2;i++) c=a+b; printf("%5d",c); a=b; b=c; } getch();

17 Program to check whether a given integer is a Harshad number or not.
void main() { int n,sum=0,i,rem,num; clrscr(); printf("Enter the number\n"); scanf("%d",&n); num=n; for(;n>0;) rem=n%10; sum=sum+rem; n=n/10; } if(num%sum==0) printf("number is Harshad Number"); else printf("number is not a Harshad Number"); getch();

18 Program to check whether a number is Armstrong or not.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int n,i,rem,num,digit_count=0,sum=0; clrscr(); printf("Enter the number\n"); scanf("%d",&n); num=n; while(n>0) n=n/10; digit_count++; } n=num; do rem=n%10; sum=sum+pow(rem,digit_count); } while(n>0); if(num==sum) printf("number is Armstrong Number"); else printf("number is not an Armstrong Number"); getch();

19 Unconditional Statements
1 break 2 continue 3 goto

20 Program to check prime number using break statement.
void main() { int n,i,flag=0; clrscr(); printf("Enter the number\n"); scanf("%d",&n); for(i=2;i<=n/2;i++) if(n%i==0) flag=1; break; //Using break Here… } if(flag==0) printf("Prime number"); else printf("Not a prime number"); getch();

21 Program using Continue Statement.
void main() { int i; clrscr(); for(i=200;i<=300;i++) if(i%2==0) continue; // Continue used Here… printf("%5d",i); } getch();

22 goto statement example 1
//To count the number of digits of a number. void main() { int i,n,count=0,L1; clrscr(); printf("Enter the number to count its number of digits"); scanf("%d",&n); L1: if(n>0) n=n/10; count++; goto L1; } printf("digit count=%d",count); getch();


Download ppt "WHILE, DO-WHILE AND FOR LOOPS"

Similar presentations


Ads by Google