Home Work-4
Home Work-4
Home Work-4
1. Write a program in C to check a given number is even or odd using the function.
#include <stdio.h>
void check(int a)
if(a%2==0)
printf("Even");
else
printf("Odd");
int main() {
int a;
scanf("%d",&a);
check(a);
return 0;
}
2. Write a program in C to check armstrong and perfect numbers using the function.
Input: - Input any number: 371
Output: -The 371 is an Armstrong number.
The 371 is not a Perfect number.
#include <stdio.h>
int check(int n)
{
int r,sum=0;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
return sum;
}
int main() {
int a,f;
printf("Enter a number ");
scanf("%d",&a);
f=check(a);
if(a==f)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
3. Write a program in C to print all perfect numbers in given range using the function.
#include <stdio.h>
int check(int n)
{
int r,sum=0;
for(int i = 1 ; i < n ; i++)
{
if(n % i == 0)
sum = sum + i ;
}
return sum;
}
int main() {
int a,f;
printf("Enter a number ");
scanf("%d",&a);
f=check(a);
if(a==f)
printf("perfect number ");
else
printf("not perfect number");
return 0;
}
4. Predict Output?
#include <stdio.h>
int main()
{
int (*ptr)(int ) = fun;
(*ptr)(3);
return 0;
}
int fun(int n)
{
for(;n > 0; n--)
printf("HI FUNCTIONS ");
return 0;
}
#include<stdio.h>
int checkprime(int j)
{
int c=1;
for(int i=2;i<j;i++)
{
if(j%i==0)
{
c=0;
return c;
break;
}
}
if(c==1)
return c;
}
int main()
{
int a,g,h,b,c;
printf("Enter a number ");
scanf("%d",&a);
b=2;
c=a-2;
while(b<=((a/2)+1))
{
g=checkprime(b);
h=checkprime(c);
if((g&&h)==1)
{
printf("%d +%d = %d\n",b,c,a);
}
b++;
c--;
}
return 0;
}
#include <stdio.h>
int cube(int n)
{
return (n*n*n);
}
int main()
{
int a,b;
printf("Enter a number ");
scanf("%d",&a);
b=cube(a);
printf("The cube of the number is %d",b);
}
8. Write a C program to find diameter, circumference and area of circle using function.
#include <stdio.h>
float diameter(int n)
return (2*n);
}
float circumference(int n)
return (2*3.14*n);
float area(int n)
return (3.14*n*n);
void main()
float a,b,c,d;
scanf("%f",&a);
b=diameter(a);
c=circumference(a);
d=area(a);
#include <stdio.h>
float sum(int n)
int a,s=0;
while(n>0)
a=n%10;
n=n/10;
s=s+a;
return s;
}
int main()
int a,b;
scanf("%d",&a);
b=sum(a);
return 0;
if (b == 0) {
return a;
}
return gcd(b, a % b);
// Find the least common multiple of two numbers using the gcd function
int main() {
int a, b;
return 0;