GOKUL (Function)
GOKUL (Function)
#include <stdio.h>
for(a;a<=n;a++)
return a;
int main()
int a,i,c;
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 d=a+b;
return d;
int main()
int a,b,c,d;
scanf("%d",&a);
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 ans;
for(int i=1;i<=n;i++)
ans=i*i;
sum=sum+ans;
return ans ;
int main()
int ans,a;
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;
scanf("%d",&n);
result=perfect(n);
if(result==n)
else
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;
scanf("%d",&n);
for(i=1;i<=n;i++)
r=perfect(i);
if(r==1)
else
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;
scanf("%d",&n);
result=armstrong(n);
if(n==result)
}
else
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;
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;
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 c;
c=pow(base,expo);
return c;
int main()
int base,expo,c,d;
scanf("%d",&base);
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;
scanf("%d",&n);
b=n;
f=palin(n);
printf("%d",f);
if(b==f)
printf("\nit is palindrome",b);
else
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;
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;
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;
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 c;
c=pow(base,3);
return c;
int main()
int base,expo,c,d;
scanf("%d",&base);
d=cube(base);
printf("cube=%d",d);