For While Dowhile Programs

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

/* program to print ascii values from a to z */

#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( );
}

/* 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( );
}

/* program to print fibonacci numbers */


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to find LCD and GCD of two numbers */


#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( );
}

/* program to find whether the number is a palindrome or not */


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to print first n natural numbers using for */


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to find sum of digits of the given number */


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to generate multiplication table of 2*/


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to generate multiplication table of n*/


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to generate multiplication table of n using for*/


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to print 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);
printf("The first %d natural numbers are \n",n);
while(i<=n)
{
printf("%d\t",i);
i++;
}
getch( );
}

/* program to perform simple arithmetic operations using switch and do while*/


#include<stdio.h>
#include<conio.h>
void main( )
{
int ch,a,b,c;
char ch1;
clrscr( );
do
{
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
printf("Enter 1-Addition, 2- subtraction, 3-multiplication, 4-division\n");
scanf("%d",&ch);
switch(ch)
{
case 1: c=a+b;
printf("sum of %d and %d is %d",a,b,c);
break;
case 2: c=a-b;
printf("difference of %d and %d is %d",a,b,c);
break;
case 3: c=a*b;
printf("product of %d and %d is %d",a,b,c);
break;
case 4: if(b>0)
{
c=a/b;
printf("sum of %d and %d is %d",a,b,c);
}
else
printf("divide by zero -error\n");
break;
default: printf("invalid choice\n");
}
printf("\nDo you want to continue? press y or Y to continue\n");
ch1=getch( );
}
while(ch1=='y'||ch1=='Y');
getch( );
}

/* 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( );
}

/* 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( ) */

/* program to generate prime numbers in a given range */


#include <stdio.h>
#include<conio.h>
void main( )
{
int n,m, i,j,flag;
clrscr( );
printf("\n\n Enter the range ");
scanf("%d%d",&m,&n);
for(i=m;i<=n;i++)
{
flag=1;
for (j = 2; j <= i / 2; j++)
if (i % j == 0)
{
flag = 0;
break;
}
if(flag)
printf("%d\n",i);
}
getch( );
}

/*Program to convert a binary number to its decimal equivalent */


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=0,dec=0,n,digit,temp;
printf("Enter the number\n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
digit=n%10;
dec=dec+digit*(pow(2,i));
n=n/10;
i++;
}
printf("the number in binary is %d in decimal is %d\n",temp,dec);
}

/*Program to convert a decimal number to its binary equivalent */


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=0,dec=0,n,digit,temp;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
digit=n%2;
dec=dec+digit*(pow(10,i));
n=n/2;
i++;
}
printf("the number in decimal is %d in decimal is %d\n",temp,dec);
getch();
}

/* 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( );
}

/* 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( );
}

/* program to print fibonacci numbers */


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to find whether the number is a palindrome or not */


#include<stdio.h>
#include<conio.h>

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( );
}

/* program to find sum of digits of the given number */


#include<stdio.h>
#include<conio.h>

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( ) */

/* program to generate prime numbers in a given range */


#include <stdio.h>
#include<conio.h>
void main( )
{
int n,m, i,j,flag;
clrscr( );
printf("\n\n Enter the range ");
scanf("%d%d",&m,&n);
for(i=m;i<=n;i++)
{
flag=1;
for (j = 2; j <= i / 2; j++)
if (i % j == 0)
{
flag = 0;
break;
}
if(flag)
printf("%d\n",i);
}
getch( );
}

/* program to print fibonacci numbers */


#include<stdio.h>
#include<conio.h>

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();
}

You might also like