prowessapps.in Loop Programs in C

 C Program to calculate the sum of numbers from 1 to 10.

#include<stdio.h>
int main( ) {
 int i, sum=0;
 for(i=1; i<=10; i++)
 {
  sum = sum+i;
 }
 printf("SUM IS : %d\n"+sum);
 return 0;
 }
OUTPUT :
SUM IS : 55

 C Program to find the factorial of a given number.

#include<stdio.h>
int main( ) {
 int i, fact=1, n;
 printf("Enter Value of N: ");
 scanf("%d", &n);
 for(i=n; i>=1; i--)
 {
  fact = fact*i;
 }
 printf("FACTORIAL IS : %d\n",fact);
 return 0;
}
OUTPUT :
Enter Value of N: 4
FACTORIAL IS : 24

 C Program to check the given number is prime or not.

#include<stdio.h>
#include<math.h>
int main( ) {
 int i, n, flag=1;
 printf("Enter Value of N: ");
 scanf("%d", &n);
 for(i=2; i<=sqrt(n); i++) {
  if(n%i == 0) {
    flag = 0;
    break;
   }
  }
  if(flag == 1) { 
  printf("%d is PRIME NO.\n",n);
  }
  else {
   printf("%d is NOT PRIME NO.\n",n);
  }
 return 0;
}
OUTPUT :
Enter Value of N: 17
17 is PRIME NO.
Enter Value of N: 9
9 is NOT PRIME NO.

 C Program to count the digits in a given number.

#include<stdio.h>
int main( ) {
 int n, dig=0;
 printf("Enter Value for N : ");
 scanf("%d", &n);
 while(n!=0){
  dig++;
  n = n/10;
 }
 printf("TOTAL DIGITS : %d\n",dig);
 printf("\n");
 return 0;
}
OUTPUT :
Enter Value for N: 1232
TOTAL DIGITS : 4

 C Program to check given number is PALINDROME or NOT.

#include<stdio.h>
int main( ) {
 int n, rev = 0, temp;
 printf("Enter Value for N : ");
 scanf("%d", &n);
 temp = n;
 while(temp!=0) {
  rev = rev*10 + temp%10;
  temp = temp/10;
 }
 if(rev == n) {
  printf("N is PALINDROME\n");
 }
 else {
  printf("N is NOT PALINDROME\n");
 }
 printf("\n");
 return 0;
}
OUTPUT :
Enter Value for N: 1221
N is PALINDROME

 C Program to find x to the power y (xy) using loop.

#include<stdio.h >
int main( ) {
 int x,y,res=1,i;
 printf("X : ");
scanf("%d",&x);
printf("Y : ");
scanf("%d",&y);
for(i=0; i < y; i++)
{
res = res*x;
}
printf("X^Y IS : %d\n",res);
return 0;
}
OUTPUT :
X : 8
Y : 2
X^Y IS : 64


 C Program to check a given number is armstrong or not.

#include<stdio.h>
#include<math.h>
int main( ) {
 int n, sum =0, temp, rem, digits = 0;
 printf("Enter a number : ");
 scanf("%d",&n);
 temp = n;
 while(temp != 0) {
  digits++;
  temp = temp/10;
 }
 temp = n;
 while(temp != 0) {
  rem = temp%10;
  int power = pow(rem, digits);
  sum = sum + power;
  temp = temp/10;
 }
 if(n == sum) {
  printf("%d is an armstrong number");
 }
 else {
  printf("%d is not an armstrong no.");
 }
 return 0;
}
OUTPUT :
Enter a number : 371
371 is an armstrong number

 C Program to check a given number is Perfect number or not.

#include<stdio.h>
int main( ) {
 int n, sum = 0, i ;
 printf("Enter a number : ");
 scanf("%d",&n);
 for(i = 1; i<=n/2; i++) {
  if(n%i==0) {
  sum = sum+i;
  }
 }
 if(sum == n) {
  printf("%d is a perfect no.\n",n);
 }
 else {
  printf("%d is not a perfect no.\n",n);
 }
 return 0;
}
OUTPUT :
Enter a number : 28
28 is a perfect no.

 C Program to calculate hcf and lcm of two number.

#include<stdio.h>
int main( ) {
 int a,b,temp,gr,low, lcm;
 printf("Enter two numbers : ");
 scanf("%d%d",&a,&b);
 gr = (a >= b)?a:b;
 low = (a < b)?a:b;
 while(low!=0) {
  temp = gr % low;
  gr = low;
  low = temp;
 }
 printf("HCF = %d\n",gr);
 lcm = a*b/gr;
 printf("LCM = %d\n",lcm); 
 return 0;
}
OUTPUT :
Enter two number : 30 40
HCF = 10
LCM = 120

 C Program to convert decimal to binary number.

#include<stdio.h>
int main( ) {
 int n, c, k;
 printf("Enter a number ");
 scanf("%d",&n);
 printf("Binary number of %d =\n",n );
 for(c = 15;c >=0; c--) {
  k = n >> c;
  if(k & 1)
  printf("1");
  else
  printf("0");
 }
 printf("\n");
 return 0;
}
OUTPUT :
Enter a number 12
Binary number of 12 =
0000000000001100

 C Program to print Armstrong numbers from 100 to 500.

#include<stdio.h>
int main() {
 int i,r,sum,temp;
 for(i=100;i < = 500;i++) {
  temp = i;
  sum = 0;
  while(temp!=0) {
   r = temp%10;
   temp = temp/10;
   sum = sum+(r*r*r);
  }
  if(sum==i)
   printf("%d ",i);
 }
 return 0;
}
OUTPUT :
153 370 371 407




CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved