Home Work-4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

HOME WORK- 3

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;

printf("Enter a number ");

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;
}

5. Consider following C function:


int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is ?
The value returned by f(1) is 7

6. To Check Whether a Number can be Expressed as Sum of Two Prime Numbers.

#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;
}

7. Write a C program to find cube of any number using function.

#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;

printf("Enter radius of circle ");

scanf("%f",&a);

b=diameter(a);

c=circumference(a);

d=area(a);

printf("The diamater is %1.1f\n",b);


printf("The circumference is %1.1f\n",c);

printf("The area is %1.3f\n",d);

9. Write a C program to find sum of digits of a given number using function.

#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;

printf("Enter number ");

scanf("%d",&a);

b=sum(a);

printf("The sum of digits is %d",b);

return 0;

10. Write a C program to find LCM of two numbers using recursion.


#include <stdio.h>

int gcd(int a, int b) {

if (b == 0) {

return a;

}
return gcd(b, a % b);

// Find the least common multiple of two numbers using the gcd function

int lcm(int a, int b) {

return (a * b) / gcd(a, b);

int main() {

int a, b;

printf("Enter two positive integers: ");

scanf("%d %d", &a, &b);

printf("The LCM of %d and %d is %d\n", a, b, lcm(a, b));

return 0;

You might also like