Professional Documents
Culture Documents
For While Dowhile Programs
For While Dowhile Programs
For While Dowhile Programs
#include<stdio.h>
#include<conio.h>
void main( )
{
int i;
clrscr( );
printf("The equivalent ascii values of the following characters are\n");
for(i='a';i<='z';i++)
{
printf("ascii value of %c is %d\n",i,i);
}
getch( );
}
/* program to find whether numbers from 200 to 300 are divisible by 7, find its
sum, and count them */
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,sum=0,count=0;
clrscr( );
printf("The numbers divisible by 7 between 200 and 300 are\n ");
for(i=200;i<=300;i++)
{
if(i%7==0)
{
printf("%d is divisible by 7\n",i);
sum=sum+i;
count++;
}
}
printf("sum of numbers divisible by 7 is %d\n",sum);
printf("The total number of numbers divisble by 7 is %d\n",count);
getch( );
}
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( );
}
void main( )
{
int n,fib1,fib2,fib3,i=3;
clrscr( );
printf("Enter the value till which u want to print fibonacci numbers ");
scanf("%d",&n);
printf("The fibonacci numbers are\n");
fib1=0;
fib2=1;
printf("%d\t %d\t",fib1,fib2);
for(i=3;i<=n;i++)
{
fib3=fib1+fib2;
printf("%d\t",fib3);
fib1=fib2;
fib2=fib3;
}
getch( );
}
void main( )
{
int n,m,r,lcm,gcd,p,q;
clrscr( );
printf("Enter two numbers\n");
scanf("%d%d",&m,&n);
p=m;
q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=p*q/gcd;
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;
}
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 i=0,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);
for(i=1;i<=n;i++)
printf("%d\t",i);
getch( );
}
/* program to find whether numbers from 200 to 300 are divisible by 7, find its
sum, and count them */
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,sum=0,a,b;
clrscr( );
printf("Enter the starting and ending range ");
scanf("%d%d",&a,&b);
for(i=a;i<=b;i++)
sum=sum+i;
printf("sum of numbers between %d and %d is %d\n",a,b,sum);
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( );
}
void main( )
{
int i=1,prod=1,n;
clrscr( );
printf("Enter value till which u want to see multiplication table of 2 \n");
scanf("%d",&n);
do
{
prod=2*i;
printf("2 * %d = %d\n",i,prod);
i++;
}
while(i<=n);
getch( );
}
void main( )
{
int i=1,prod=1,n,no;
clrscr( );
printf("Enter the number of which u want to generate multiplication table\n");
scanf("%d",&no);
printf("\nEnter value till which u want to see multiplication table of %d \n",no);
scanf("%d",&n);
do
{
prod=no*i;
printf("%d * %d = %d\n",no,i,prod);
i++;
}
while(i<=n);
getch( );
}
void main( )
{
int i=1,prod=1,n,no;
clrscr( );
printf("Enter the number of which u want to generate multiplication table\n");
scanf("%d",&no);
printf("\nEnter value till which u want to see multiplication table of %d \n",no);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
prod=no*i;
printf("%d * %d = %d\n",no,i,prod);
}
getch( );
}
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);
while(i<=n)
{
printf("%d\t",i);
i++;
}
getch( );
}
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( );
}
/* 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( );
}
/* 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;
}
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( ) */
while(i<=n)
{
sum=sum+i;
i++;
}
printf("sum of first %d numbers is %d",n,sum);
getch( );
}
/* 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( );
}
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)
{
prev2=prev1;
prev1=cur;
cur=prev1+prev2;
printf("%d\n",cur);
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;
q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=p*q/gcd;
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;
}
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;
}
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)
{
prev2=prev1;
prev1=cur;
cur=prev1+prev2;
printf("%d\n",cur);
i++;
}
getch();
}