Loop Programs Solutions

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

1.

Write a calculator program using while and switch


#include<stdio.h>
void main()
{
int a,b,ch;
while(1)
{
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
printf(“\n select your choice\n1:addition\n2:subtraction\n3:multiplication\n4:division\n5:modula”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“\n addition =%d”,a+b); break;
case 2: printf(“\n subtraction =%d”,a-b); break;
case 3: printf(“\n multiplication =%d”,a*b); break;
case 4: printf(“\n division =%d”,a/b); break;
case 5: printf(“\n modula =%d”,a%b); break;
default:exit();
}
}
}

2. Write a c program to check given value is palindrome or not


#include<stdio.h>
void main()
{
int n,t,rem;
printf(“enter n value”);
scanf(“%d”,&n);
t=n;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(t==rev)
printf(“given number is palindrome”);
else
printf(“given number is not a palindrome”);
}
3. Write a c program to check given value is prime or not
#include<stdio.h>
void main()
{
int n,count=0,i;
printf(“enter n value”);
scanf(“%d”,&n);
while(i<=n)
{
if(n%i==0)
count++;
i++;
}
if(count==2)
printf(“given number is prime”);
else
printf(“given number is not a prime”);
}

4. Write a c program to find given number factorial


#include<stdio.h>
void main()
{
int fact=1,i,n;
printf(“enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf(“%d factorial = %d”,n,fact);
}

5. Write a c program to print Fibonacci series


#include<stdio.h>
void main()
{
int a=0,b=1,c,n,i=1;
printf(“enter n value”);
scanf(“%d”,&n);
while(i<=n)
{
printf(“%d”,a);
c=a+b;
a=b;
b=c;
i++;
}
}

6. Write a c program to display 1 to n numbers using do while


#include<stdio.h>
void main()
{
int i=1,n;
printf(“enter n value”);
scanf(“%d”,&n);
do
{
Printf(“%d”,i);
i++;
}while(i<=n);
}
7. Write a c program to display n to 1 numbers using do while
#include<stdio.h>
void main()
{
int i=1,n;
printf(“enter n value”);
scanf(“%d”,&n);
do
{
printf(“%d”,n);
n--;
}while(n>=i);
}
8. Write a multiplication table using do-while and for
#include<stdio.h>
void main()
{
int i=1,n;
printf(“enter n value”);
scanf(“%d”,&n);
do{
printf(“%d * %d = %d\n”,n,i,n*i);
i++;
}while(i<=n);
}
// for loop

#include<stdio.h>
void main()
{
int i,n;
printf(“enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
printf(“%d * %d = %d\n”,n,i,n*i);
}

9. Write a c program to check given value is Armstrong number or


not(Armstrong number of 3 digits, the sum of cubes of each digits is equal
to the number itself)
#include<stdio.h>
#include<math.h>
void main()
{
int r1,r,n,t,sum=0,t1,count=0;
clrscr();
printf("enter n value");
scanf("%d",&n);
t=n;
t1=n;
while(n!=0)
{
r=n%10;
count++;
n=n/10;
}
while(t!=0)
{
r1=t%10;
sum=sum+pow(r1,count);
t=t/10;
}
if(t1==sum)
printf("\ngiven number is armstrong number");
else
printf("\n given number is not a armstrong number");
}

10. Write a c program to print 1 to n prime values using for loop


#include<stdio.h>
void main()
{
int i,n,j,count;
clrscr();
printf("enter n value");
scanf("%d",&n);
printf("1 to %d prime numbers\n",n);
for(i=1;i<=n;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%3d",i);
}
getch();
}
11. To find the sum of 1+ x2+x3+x4..xn
#include<stdio.h>
void main()
{
int i,n,sum=0;
printf(“enter x,n value”);
scanf(“%d%d”,&x,&n);
for(i=1;i<=n;i++)
sum=sum+pow(x,i);
printf(“sum=%d”,sum);
}

12. To find the sum of 1!+2!+3!+4!...n!


#include<stdio.h>
void main()
{
int i,n,sum=0,fact;
printf(“enter x,n value”);
scanf(“%d%d”,&x,&n);
for(i=1;i<=n;i++)
{
fact=1;
for(j=1;j<=i;j++)
sum=sum+fact*j;
}
printf(“sum=%d”,sum);
}

13. To find the sum of 1+2+3+…n


#include<stdio.h>
void main()
{
int i=1,n,sum=0;
printf(“enter n value”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i++;
}while(i<=n);
}
14. Write a c program to print given formats

#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(" *");
printf("\n");
}

#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(" %d",j);
printf("\n");
}

#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(" %d",i);
printf("\n");
}

#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
printf(" %d",j);
printf("\n");
}

#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
printf(" %d",i);
printf("\n");
}

#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=5;i>=1;i++)
{
for(k=30-i;k>=1;k--)
printf(" ");
for(j=1;j<=i;j++)
printf(" *%d",i);
printf("\n");
}
}

#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=5;i>=1;i++)
{
for(k=30-i;k>=1;k--)
printf(" ");
for(j=1;j<=i;j++)
printf(" %d",i);
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j,n,k,;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=30-i;k>=1;k--)
printf(" ");
for(j=1;j<=i;j++)
printf(" %d",i);
printf("\n");
}
for(i=n;i>=1;i--)
{
for(k=30-i;k>=1;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("* %d ",i);
printf("\n");
}
getch();
}

15. Write a c program to print Pascal triangular

#include<stdio.h>
void main()
{
int i,j,n,k,coeff=1;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(k=30-i;k>=1;k--)
printf(" ");
for(j=0;j<=i;j++)
{
if(j==0||i==0)
coeff=1;
else
coeff=coeff*(i-j+1)/j;
printf(" *%d",coeff);
}
printf("\n");
}
16.

#include<stdio.h>
#include<math.h>
main()
{
int i=0,k=0,n,fact=1,j,x;
float sum=0;
printf("\n Enter the value of n");
scanf("%d",&n);
printf("\n Enter the value of x");
scanf("%d",&x);
do {
fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
sum=sum+pow(-1,k)*pow(x,i)/fact;
i=i+2;
k++;
}while(i<=2n);
printf("\n sum=%f",sum);
}

You might also like