For While Dowhile Programs-Alg+Flowchart
For While Dowhile Programs-Alg+Flowchart
R 1
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
1)To find sum of first n natural numbers using while
Algorithm :-
Step 1: Start
Step 6: Stop
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 2
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
1) Flowchart:-
Start
Read n
sum ¬ 0
i¬1
False
while
False (i<=n)
True
False
sum¬ sum + i
i++
print sum
Stop
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 3
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
1) /* program to find sum of first n natural numbers using while */
#include<stdio.h>
#include<conio.h>
void main( )
{
int i=1,sum=0,n;
clrscr( );
printf("Enter the value till which you want to print the numbers \n");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("sum of first %d numbers is %d",n,sum);
getch( );
}
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 4
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
2) To find sum of first n natural numbers using do-while
Algorithm :-
Step 1: Start
Step 6: Stop
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 5
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
Flowchart:-
Start
Read n
sum ¬ 0
i¬1
sum¬ sum + i
i++
False
while
(i<=n)
True
print sum
Stop
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 6
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
2) /* program to print first n natural numbers using do-while */
#include<stdio.h>
#include<conio.h>
void main( )
{
int i=1,sum=0,n;
clrscr( );
printf("Enter the value till which you want to print the numbers \n");
scanf("%d",&n);
printf("The first %d natural numbers are \n",n);
do
{
printf("%d\t",i);
i++;
}
while(i<=n);
getch( );
}
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 7
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
/* program to find sum of odd and even numbers till a given number n*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i=1,even_sum=0,odd_sum=0,n;
clrscr( );
printf("Enter the value till which you want to print the numbers \n");
scanf("%d",&n);
while(i<=n)
{
if(i%2==0)
even_sum=even_sum+i;
else
odd_sum=odd_sum+i;
i++;
}
printf("sum of even numbers till %d is %d\n",n,even_sum);
printf("sum of odd numbers till %d is %d",n,odd_sum);
getch( );
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,prev1,prev2,cur,i=3; // i is 3 as first 3 numbers are printed initially
clrscr( );
printf("Enter the value till which u want to print fibonacci numbers ");
scanf("%d",&n);
printf("The fibonacci numbers are\n");
prev2=0;
prev1=1;
cur=prev1+prev2; //current number is sum of previous two numbers
printf("%d\n%d\n%d\n",prev2,prev1,cur); // print first 3 numbers
while(i<n)
{
prev2=prev1; //change the value of prev2 to prev1
prev1=cur; //change the value of cur to prev2
cur=prev1+prev2; // current number is sum of previous two numbers
printf("%d\n",cur);
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 8
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
i++;
}
getch( );
}
/* program to find LCD and GCD of two numbers using Euclids algorithm */
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,m,r,lcm,gcd,p,q;
clrscr( );
printf("Enter two numbers\n");
scanf("%d%d",&m,&n);
p=m; // store the original value of m and n to calculate LCM
q=n;
while(n!=0) //Repeat till remainder is 0
{
r=m%n; // find the remainder
m=n; // m will have the value of GCD after termination of loop
n=r;
}
gcd=m;
lcm=p*q/gcd; // calculate LCM
printf("GCD of %d and %d is %d\n",p,q,gcd);
printf("LCM of %d and %d is %d\n",p,q,lcm);
getch( );
}
void main( )
{
int m,n,digit,rev=0;
clrscr( );
printf("Enter the number ");
scanf("%d",&n);
m=n;
while(n!=0)
{
digit=n%10;
n=n/10;
rev=rev*10+digit;
}
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 9
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
printf("Reversed number is %d\n",rev);
if(m==rev)
printf("Number is a palindrome\n");
else
printf("Number is not a palindrome\n");
getch( );
}
void main( )
{
int n,digit,sum=0;
clrscr( );
printf("Enter the number ");
scanf("%d",&n);
m=n;
while(n!=0)
{
digit=n%10;
sum+=digit;
n=n/10;
}
printf("sum of digits of given number is %d\n",sum);
getch( );
}
/* To find whether a given number is a prime or not and output suitable message */
#include <stdio.h>
#include<conio.h>
void main( )
{
int n, i, flag;
clrscr( );
printf("\n\n Enter the number ");
scanf("%d",&n);
flag=1;
for (i = 2; i <= n / 2; i++)
if (n % i = = 0)
{
flag = 0;
break;
}
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 10
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
if (flag)
printf("\n\n %d Is a Prime Number.. ",n);
else
printf("\n\n %d Is Not a Prime Number.. ",n);
} /* End of main( ) */
void main()
{
int n,prev1,prev2,cur,i=3;
clrscr();
printf("Enter the value till which u want to print fibonacci numbers ");
scanf("%d",&n);
printf("The fibonacci numbers are\n");
prev2=0;
prev1=1;
cur=prev1+prev2;
printf("%d\n%d\n%d\n",prev2,prev1,cur);
while(i<n)
{
Semester: I Section: ‘F’ Faculty: Sanjeetha.R 11
LAB PROGRAMS ON WHILE, DO-WHILE AND FOR
prev2=prev1;
prev1=cur;
cur=prev1+prev2;
printf("%d\n",cur);
i++;
}
getch();
}
void main()
{
int n,i=1,fact=1;
clrscr();
printf("Enter the numbers ");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of %d is %d",n,fact);
getch();
}