0% found this document useful (0 votes)
61 views19 pages

GOKUL (Function)

The document contains multiple C programs that demonstrate various programming concepts including printing natural numbers, calculating sums, checking for perfect and Armstrong numbers, calculating factorials, generating Fibonacci series, and manipulating digits of numbers. Each program is structured with functions and utilizes for loops to perform the required tasks. The examples illustrate fundamental programming techniques and problem-solving skills in C.

Uploaded by

362 - Ragul K -
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views19 pages

GOKUL (Function)

The document contains multiple C programs that demonstrate various programming concepts including printing natural numbers, calculating sums, checking for perfect and Armstrong numbers, calculating factorials, generating Fibonacci series, and manipulating digits of numbers. Each program is structured with functions and utilizes for loops to perform the required tasks. The examples illustrate fundamental programming techniques and problem-solving skills in C.

Uploaded by

362 - Ragul K -
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1. Write a C program to print n natural numbers using for loop.

#include <stdio.h>

int sum(int a,int n)

for(a;a<=n;a++)

return a;

int main()

int a,i,c;

printf("enter the a value=");

scanf("%d",&a);

for(i=1;i<=a;i++)

c=sum(i,a);

printf("\n%d",c);

}
2. Write a C program to print n natural numbers and their sum using
for loop and functions.
#include <stdio.h>

int sum(int a,int b)

int d=a+b;

return d;

int main()

int a,b,c,d;

printf("enter the a value=");

scanf("%d",&a);

printf("enter the a value=");

scanf("%d",&b);

c=sum(a,b);

printf("total=%d",c);

}
3. Write a C program to display the n terms of square natural number
and their sum using for loop.
#include <stdio.h>

int sum=0;

int square (int n)

int ans;

for(int i=1;i<=n;i++)

ans=i*i;

sum=sum+ans;

return ans ;

int main()

int ans,a;

printf("Enter the Number");

scanf("%d",&a);

for(int i=1;i<=a;i++)

ans=square(i);

printf("%d\n",ans);

printf("total=%d\n",sum);

return 0;

}
4. Write a C program to check whether a given number is a perfect
number or not using for loop.
#include <stdio.h>

int perfect(int a)

int sum=0;

for(i=1;i<a;i++)

if (a%i==0)

sum=sum+i;

return sum;

int main()

int n,result;

printf("Enter the number=");

scanf("%d",&n);

result=perfect(n);

if(result==n)

printf("%d it is a perfect number",n);

else

printf("%d it is not a perfect number",n);

return 0;

}
5. Write a C program to find the perfect numbers within a given
number of range using for loop.
#include <stdio.h>

int perfect(int a)

int sum=0;

for(i=1;i<a;i++)

if (a%i==0)

sum=sum+i;

if(sum==a)

return 1;

else

return 0;

int main()

int n,r;

printf("Enter the number=");

scanf("%d",&n);

for(i=1;i<=n;i++)

r=perfect(i);
if(r==1)

printf("%d is a perfect number\n",i);

else

printf("%d is not a perfect number\n",i);

return 0;

}
6. Write a C program to check whether a given number is an
armstrong number or not using for loop.
#include <stdio.h>

#include<math.h>

int armstrong(int n)

int n1=n;

int sum=0,a=0,b,c;

for(n;n!=0;n/=10)

a++;

for(n1;n1!=0;n1/=10)

c=n1%10;

b= pow(c,a);

sum=sum+b;

return sum;

int main()

int n,result;

printf("Enter the number");

scanf("%d",&n);

result=armstrong(n);

if(n==result)

printf("%d is a Armstrong number\n",n);

}
else

printf("%d is not a Armstrong number\n",n);

return 0;

}
7. Write a C program to calculate the factorial of a given number using
for loop.
#include <stdio.h>

int factorial(n)

int t=1,i;

for(i=1;i<=n;i++)

t=t*i;

return t;

int main()

int n,t,d;

printf("enter the number=");

scanf("%d",&n);

d=factorial(n);

printf("result=%d",d);

}
8. Write a C program to Fibonacci series upto n numbers using for
loop.

#include<stdio.h>

int fib(int n)

int a,b,c,i;

for(i=0;i<=n;i++)

c=a+b;

a=b;

b=c;

return c;

int main()

int a,b,c,n,i,d;

a=0;

b=1;

printf("enter the number=");

scanf("%d",&n);

d=fib(n);

printf("\n%d",d);

}
9. Write a C program to find the power of a number using for loop.
#include <stdio.h>

int power(int base ,int expo)

int c;

c=pow(base,expo);

return c;

int main()

int base,expo,c,d;

printf("enter the base value=");

scanf("%d",&base);

printf("enter the expo value=");

scanf("%d",&expo);

d=power(base,expo);

printf("%d",d);

}
11.Write a C program to check whether the given number is
palindrome or not using for loop.
#include <stdio.h>

int palin(int n)

int a=0,c,f;

for(;n>0;n/=10)

c=n%10;

a=a*10+c;

return a;

int main()

int a=0,b,c,n,f;

printf("enter the number= ");

scanf("%d",&n);

b=n;

f=palin(n);

printf("%d",f);

if(b==f)

printf("\nit is palindrome",b);

else

printf("\nit is not a palindrome",b);

return 0;

}
12.Write a C program to reverse the given number using for loop.
#include <stdio.h>

int palin(int n)

int a=0,c,f;

for(;n>0;n/=10)

c=n%10;

a=a*10+c;

return a;

int main()

int a=0,b,c,n,f;

printf("enter the number= ");

scanf("%d",&n);

b=n;

f=palin(n);

printf("%d",f);

}
15.Write a C program to find the sum of digits of the number using for
loop
#include <stdio.h>

int dig(int n)

int a=0,c,d;

for(;n>0;n/=10)

c=n%10;

a=a+c;

return a;

int main()

int a=0,n,c,d;

printf("enter the number=");

scanf("%d",&n);

d=dig(n);

printf("%d",d);

}
16.Write a C program to print infinite loop using for loop.
#include <stdio.h>

int infinite(int a)

a++;

return a;

int main()

int a=1,d;

d=infinite(a);

while(d>1)

printf("Hello World\n");

return 0;

}
17.Write a C program to find the product of the digits of number using
for loop.
#include <stdio.h>

int add(int n)

int a=1,c;

for(;n!=0;n/=10)

c=n%10;

a=a*c;

return a;

int main()

int a=1,b,n,c;

printf("enter the number=");

scanf("%d",&n);

b=add(n);

printf("total=%d",b);

}
18.Write a C program to find the count the digit of a number using for
loop.
#include <stdio.h>

int count(int n)

int c=0,a;

for(;n>0;n=n/10)

c++;

return c;

int main()

int n,c=0,a;

printf("Enter a number:");

scanf("%d",&n);

a=count(n);

printf("count=%d",a);

}
20.Write a C program to display the cube of the number upto given an
integer using for loop

#include <stdio.h>

int cube(int base)

int c;

c=pow(base,3);

return c;

int main()

int base,expo,c,d;

printf("enter the base value=");

scanf("%d",&base);

d=cube(base);

printf("cube=%d",d);

You might also like