Print 1 to 10
/* Print 1 to 10 using while loop */ #include<stdio.h> int main() { int i=1; while(i <= 10) { printf("%d\n",i); i++; }
return 0; }
Factorial
/* Calculate factorial using while loop */ #include<stdio.h> int main() { int a,f,i; printf("Enter a number: "); scanf("%d",&a); f=1; i=1; while(i<=a) { f = f * i; i++; } printf("Factorial: %d",f); return 0; }
Power
/* calculate power using while loop */ #include<stdio.h> int main() { int a, b, i, p; printf("Enter value of a: "); scanf("%d",&a); printf("Enter value of b: "); scanf("%d",&b); p=1; i=1; while(i<=b) { p = p * a; i++; } printf("Power : %d",p); return 0; }
Sum of digits
/* Sum of digits using while loop */ #include <stdio.h> int main() { int n, s = 0; printf("Please enter a number: "); scanf("%d",&n); while (n > 0) { s = s + n%10; n = n/10; } printf("Sum of digits is: %d", s); return 0; }
Reverse Number
/* Reverse a Number using while loop */ #include <stdio.h> int main() { int n, r = 0; printf("Please enter a number: "); scanf("%d",&n); while (n > 0) { r = r * 10; r = r + n%10; n = n/10; } printf("Reversed number is = %d: ", r); return 0; }
Palindrome Number
/* Palindrome number using while loop */ #include <stdio.h> int main() { int n, r = 0,t; printf("Please enter a number: "); scanf("%d",&n); t=n; while (n > 0) { r = r * 10; r = r + n%10; n = n/10; } if(t==r) printf("Number is Palindrome"); else printf("Number is not Palindrome"); return 0; } ------------------------------------------------------------------ /* Palindrome number using for loop */ #include <stdio.h> int main() { int n, r, t; printf("Please enter a number: "); scanf("%d",&n); t=n; for(r=0;n>0;n=n/10) { r = r * 10; r = r + n%10; } if(t==r) printf("Number is Palindrome"); else printf("Number is not Palindrome"); return 0; }
Fibonacci Series
/* Fibonacci Series using while loop */ // 0 1 1 2 3 5 8 13... #include<stdio.h> int main() { int n,i,a,b,c; printf("Enter a number: "); scanf("%d",&n); i=1; a=0; b=1; while(i<=n) { printf("%d ",a); c = a + b; a = b; b = c; i++; } return 0; }
Prime Number
/* Prime Number using while loop */ #include<stdio.h> int main() { int a,i,f; printf("Enter a number: "); scanf("%d",&a); f=0; i=2; while(i <= a/2) { if(a%i == 0) { f=1; break; } i++; } if(f==0) printf("Prime Number") else printf("Not Prime Number"); return 0; } ------------------------------------------------------- /* Prime number using for loop */ #include<stdio.h> int main() { int a,i,f; printf("Enter a number: "); scanf("%d",&a); f=0; for(i=2;i <= a/2;i++) { if(a%i == 0) { f=1; break; } } if(f==0) printf("Prime Number") else printf("Not Prime Number"); return 0; }
Armstrong Number
/* Check Armstrong Number */
#include<stdio.h>
int main()
{
int n, sum = 0, t, r;
printf("Please enter a number: ");
scanf("%d",&n);
for(t=n;t>0;t=t/10)
{
r = t%10;
sum = sum + r*r*r;
}
if ( n == sum )
printf("%d is an armstrong number.",n);
else
printf("%d is not an armstrong number.",n);
return 0;
}
#include<stdio.h>
int main()
{
int n, sum = 0, t, r;
printf("Please enter a number: ");
scanf("%d",&n);
for(t=n;t>0;t=t/10)
{
r = t%10;
sum = sum + r*r*r;
}
if ( n == sum )
printf("%d is an armstrong number.",n);
else
printf("%d is not an armstrong number.",n);
return 0;
}
Armstrong Numbers upto N
/* Print Armstrong Numbers upto N */
#include<stdio.h>
int main()
{
int i,j,sum,n;
printf("Please enter the value of N: ");
scanf("%d",&n);
for(i=2;i<=500;i++)
{
for(j=i,sum=n;j>=1;j=j/10)
sum=sum+(j%10)*(j%10)*(j%10);
if(sum==i)
printf("%d is Armstrong.\n",i);
}
return 0;
}
#include<stdio.h>
int main()
{
int i,j,sum,n;
printf("Please enter the value of N: ");
scanf("%d",&n);
for(i=2;i<=500;i++)
{
for(j=i,sum=n;j>=1;j=j/10)
sum=sum+(j%10)*(j%10)*(j%10);
if(sum==i)
printf("%d is Armstrong.\n",i);
}
return 0;
}
Compound Interest
/* Compund Interest */
#include<stdio.h>
#include<math.h>
int main()
{
float p,r,i,t,ci,a;
printf("Type the amount : ");
scanf("%f",&p);
printf("Type the interest rate : ");
scanf("%f",&r);
printf("Type the period in years: ");
scanf("%f",&t);
i=1+(r/100);
// ci=pow(i,t);
ci=1;
for(a=1;a<=t;a++)
ci=ci*i;
ci=p*ci-p;
printf("Your compound interest is : %.2f",ci);
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
float p,r,i,t,ci,a;
printf("Type the amount : ");
scanf("%f",&p);
printf("Type the interest rate : ");
scanf("%f",&r);
printf("Type the period in years: ");
scanf("%f",&t);
i=1+(r/100);
// ci=pow(i,t);
ci=1;
for(a=1;a<=t;a++)
ci=ci*i;
ci=p*ci-p;
printf("Your compound interest is : %.2f",ci);
return 0;
}
Just Give Your Feedback ConversionConversion EmoticonEmoticon