Exercise - 8
Exercise - 8
int main() {
int i,n;
printf("Enter upto range: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\n",i);
}
return 0;
}
OUTPUT:
Enter upto range: 10
1
2
3
4
5
6
7
8
9
10
2. C PROGRAM TO PRINT n NATURAL NUMBERS AND THEIR SUM:
#include <stdio.h>
int main() {
int i,n,a;
printf("Enter upto range: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\n",i);
a=a+i;
}
printf("Total sum value is %d",a);
return 0;
}
OUTPUT:
Enter upto range: 10
1
2
3
4
5
6
7
8
9
10
Total sum value is 55
3. C PROGRAM (SQUARE NATURAL NUMBERS AND THEIR SUM):
#include <stdio.h>
int main() {
int i,n,a,b;
printf("Enter upto range: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
b=i*i;
printf("%d\n",b);
a=a+b;
}
printf("Total sum value is %d",a);
return 0;
}
OUTPUT:
Enter upto range: 5
1
4
9
16
25
Total sum value is 55
4. C PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS PERFECT
NUMBER:
#include <stdio.h>
int main() {
int i,n,a;
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=n-1;i++)
{
if(n%i==0)
{
printf("%d\n",i);
a=a+i;
}
}
printf("value is %d\n",a);
if(n==a)
{
printf("It is Perfect Number");
}
else
{
printf("It is not a perfect number");
}
return 0;
}
OUTPUT:
Enter the number: 6
1
2
3
value is 6
It is Perfect Number
5. C PROGRAM TO PRINT PERFECT NUMBERS UPTO A RANGE:
#include<stdio.h>
int main() {
int b,i,j,add;
printf("Enter the upto value: ");
scanf("%d",&b);
for(i=1;i<=b;i++)
{
add=0;
for(j=1;j<=i/2;j++)
{
if(i%j==0)
{
add=add+j;
}
}
if(add==i)
{
printf("%d\n",i);
}
}
return 0;
}
OUTPUT:
Enter the upto value: 1000
6
28
496
6. C PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS ARMSTRONG
NUMBER OR NOT:
#include <stdio.h>
#include <math.h>
int main() {
int i,n,y,rem,ans;
printf("Enter the number: ");
scanf("%d",&n);
y=n;
for(i=0;n!=0;n=n/10)
{
i++;
}
printf("Number of digits is %d\n",i);
n=y;
for(i=i;n>0;n=n/10)
{
rem=n%10;
ans=pow(rem,i)+ans;
}
printf("The answer is %d\n",ans);
if(ans==y)
{
printf("It is an armstrong number");
}
else
{
printf("It is not an armstrong number");
}
return 0;
}
OUTPUT:
Enter the number: 153
Number of digits is 3
The answer is 153
It is an armstrong number
Enter the number: 1253
Number of digits is 4
The answer is 723
It is not an armstrong number
7. C PROGRAM TO CALCULATE FACTORIAL VALUE:
#include <stdio.h>
int main() {
int i,n;
int factorial=1;
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
factorial=factorial*i;
}
printf("The Factorial value is %d",factorial);
return 0;
}
OUTPUT:
Enter the number: 5
The Factorial value is 120
8. C PROGRAM FOR FIBONACCI SERIES UPTO n NUMBERS:
#include <stdio.h>
int main() {
int i,n;
int a,b=1,c;
printf("Enter upto range: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\n",a);
c=a+b;
a=b;
b=c;
}
return 0;
}
OUTPUT:
Enter upto range: 6
0
1
1
2
3
5
9. C PROGRAM TO FIND POWER VALUE OF A NUMBER:
#include <stdio.h>
int main() {
int i,n,a,power=1;
printf("Enter the number: ");
scanf("%d",&n);
printf("Enter the power value: ");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
power=power*n;
}
printf("The Answer is %d\n",power);
return 0;
}
OUTPUT:
Enter the number: 16
Enter the power value: 5
The Answer is 1048576
10.C PROGRAM TO FIND ALL FACTORS OF A NUMBER:
#include <stdio.h>
int main() {
int i,n,c;
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
printf("%d\n",i);
c++;
}
}
printf("The Total count of factors is %d",c);
return 0;
}
OUTPUT:
Enter the number: 50
1
2
5
10
25
50
The Total count of factors is 6
11.C PROGRAM TO CHECK THE GIVEN NUMBER WHETHER IT IS
PALINDROME OR NOT
#include <stdio.h>
int main() {
int i,n,a,b,N;
printf("Enter the number: ");
scanf("%d",&n);
N=n;
for(i=1;i<=n;n=n/10)
{
a=n%10;
b=b*10+a;
}
if(b==N)
{
printf("It is a palindrome");
}
else
{
printf("It is not a palindrome");
}
return 0;
}
OUTPUT:
Enter the number: 2002
It is a palindrome
Enter the number: 1234
It is not a palindrome
12.C PROGRAM FOR REVERSE NUMBER:
#include <stdio.h>
int main() {
int i,n,a,b;
printf("Enter the number");
scanf("%d",&n);
for(i=1;n!=0;n=n/10)
{
a=n%10;
b=b*10+a;
}
printf("The Reverse number is %d",b);
return 0;
}
OUTPUT:
Enter the number: 12345
The Reverse number is 54321
13.C PROGRAM FOR PRIME NUMBER CHECKING:
#include <stdio.h>
int main() {
int i,n,a=0;
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a++;
}
}
if(a==2)
{
printf("It is a prime number");
}
else
{
printf("It is not a prime number");
}
return 0;
}
OUTPUT:
Enter the number: 4
It is not a prime number
14.C PROGRAM TO PRINT PRIME NUMBERS AND THEIR SUM:
#include <stdio.h>
int main() {
int i,n,j,c,a;
c=0,a=0;
printf("Enter upto value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\n",i);
a=a+i;
}
}
printf("Total sum of prime number is %d",a);
return 0;
}
OUTPUT:
Enter upto value: 20
2
3
5
7
11
13
17
19
Total sum of prime number is 77
15.C PROGRAM FOR SUM OF THE DIGITS:
#include <stdio.h>
int main() {
int i,n,s,t,add;
printf("Enter the number: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
t=n%10;
n=n/10;
add=add+t;
}
printf("Sum of the digits is %d",add);
return 0;
}
OUTPUT:
Enter the number: 123
Sum of the digits is 6
16.C PROGRAM FOR A INFINITY LOOP:
#include <stdio.h>
int main() {
int a,b,c;
for(;;)
{
printf("Enter the 1st value: ");
scanf("%d",&a);
printf("Enter the 2nd value: ");
scanf("%d",&b);
c=a*b;
printf("The Product of the values is %d\n",c);
}
return 0;
}
OUTPUT:
Enter the 1st value: 10
Enter the 2nd value: 10
The Product of the values is 100
Enter the 1st value: 10
Enter the 2nd value: 20
The Product of the values is 200
Enter the 1st value: 10
Enter the 2nd value: 30
The Product of the values is 300
Enter the 1st value:
17.C PROGRAM FOR PRODUCT OF THE DIGITS:
#include <stdio.h>
int main() {
int i,n,s,t,a=1;
printf("Enter the value: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
t=n%10;
n=n/10;
a=a*t;
}
printf("The product of digits is %d",a);
return 0;
}
OUTPUT:
Enter the value: 42
The product of digits is 8
18.C PROGRAM FOR COUNTING THE DIGITS OF A NUMBER:
#include <stdio.h>
int main() {
int i,n,a;
printf("Enter the value: ");
scanf("%d",&n);
for(i=1;i<=n;n=n/10)
{
i++;
}
printf("The Number of digits is %d",i);
return 0;
}
OUTPUT:
Enter the value: 1234567
The Number of digits is 7
19. C PROGRAM FOR SUM OF ALL INTEGERS B/W 100 & 200 WHICH ARE DIVISIBLE BY 9:
#include <stdio.h>
int main() {
int i,a,b;
i=100;b=200;
for(i=100;i<=b;i++)
{
if(i%9==0)
{
printf("%d\n",i);
a=a+i;
}
}
printf("The sum of Number is %d",a);
return 0;
}
OUTPUT:
108
117
126
135
144
153
162
171
180
189
198
The sum of Number is 1683
20.C PROGRAM FOR PRINTING CUBE VALUES UPTO n VALUES:
#include <stdio.h>
int main() {
int i,n,a,b;
printf("Enter upto range: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
b=i*i*i;
printf("%d\n",b);
a=a+b;
}
printf("Total sum value is %d",a);
return 0;
}
OUTPUT:
Enter upto range: 10
1
8
27
64
125
216
343
512
729
1000
Total sum value is 3025
21.C PROGRAM MULTIPLICATION TABLE:
#include <stdio.h>
int main() {
int i,n,r,t;
printf("Enter the table: ");
scanf("%d",&t);
printf("Enter the range: ");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
r=i*t;
printf("%d x %d = %d\n",i,t,r);
}
return 0;
}
OUTPUT:
Enter the table: 20
Enter the range: 10
1 x 20 = 20
2 x 20 = 40
3 x 20 = 60
4 x 20 = 80
5 x 20 = 100
6 x 20 = 120
7 x 20 = 140
8 x 20 = 160
9 x 20 = 180
10 x 20 = 200
22. C PROGRAM TO READ 10 NUMBERS FROM KEYBOARD AND THEIR SUM AND AVG:
#include <stdio.h>
int main() {
int i,a,n,s;
for(i=1;i<=10;i++)
{
printf("Enter number: ");
scanf("%d",&n);
s=s+n;
}
a=s/10;
printf("Total value is %d\n",s);
printf("Average of total is %d",a);
return 0;
}
OUTPUT:
Enter number: 01
Enter number: 02
Enter number: 03
Enter number: 04
Enter number: 05
Enter number: 06
Enter number: 07
Enter number: 08
Enter number: 09
Enter number: 10
Total value is 55
Average of total is 5
23.C PROGRAM FOR RIGHT ANGLE TRAINGLE ASTERISK:
#include <stdio.h>
int main() {
int i,j,n;
printf("Enter the value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the value: 7
*
**
***
****
*****
******
*******
24.C PROGRAM – RIGHT ANGLE TRIANGLE NUMBERS:
#include <stdio.h>
int main() {
int i,j,n;
printf("Enter the value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the value: 7
1
12
123
1234
12345
123456
1234567
25.C PROGRAM – RIGHT ANGLE TRIANGLE SAME NUMBERS:
#include <stdio.h>
int main() {
int i,j,n;
printf("Enter the value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the value: 7
1
22
333
4444
55555
666666
7777777