View Course Path

Functions in C, call by reference, call by value – Full explanation

Write a program to take a number from a user and send it to a function to find and print the cube of the number

//send the number into the function and print the cube of the number in the function itself
#include<stdio.h>
#include<conio.h>
void cube(int);
void main()
{
clrscr();
int i;
printf("Enter number for which you want to find cube\n");
scanf("%d",&i);
cube(i);
getch();
}
void cube (int x)
{
int cube;
cube=x*x*x;
printf("cube is %d",cube);
}

Similarly, write a program to calculate the cube of a number in a function and print the result in the main function

//send number to function and print cube in the main function
#include<stdio.h>
#include<conio.h>
int cube(int);
void main()
{
int i,cube1;
clrscr();
printf("Enter number you want the cube of\n");
scanf("%d",&i);
cube1=cube(i);
printf("Cube is %d",cube1);
getch();
}
int cube(int x)
{
int cube1;
cube1=x*x*x;
return(cube1);
}

Write a program to send the radius of a circle to a function, calculate its area and print the result in the main function

//send radius of a circle to a function and print area in the main function
#include<stdio.h>
#include<conio.h>
float area(int);
void main()
{
clrscr();
int i;
float areacircle;
printf("Enter radius of the circle\n");
scanf("%d",&i);
areacircle=area(i);
printf("Area is %0.2f",areacircle);
getch();
}
float area(int x)
{
float areaofcircle;
areaofcircle=3.14*x*x;
return(areaofcircle);
}

Now, write a program to send the radius of five circles to a function, calculate their areas and print the result in the main function

//take in radius of five circles and calculate area using function
#include<stdio.h>
#include<conio.h>
float area(int);
void main()
{
clrscr();
int a,b,c,d,e;
printf("Enter radius of circle 1\n");
scanf("%d",&a);
printf("Enter radius of circle 2\n");
scanf("%d",&b);
printf("Enter radius of circle 3\n");
scanf("%d",&c);
printf("Enter radius of circle 4\n");
scanf("%d",&d);
printf("Enter radius of circle 5\n");
scanf("%d",&e);
printf("Area of circle is %0.2f\n",area(a));
printf("Area of circle is %0.2f\n",area(b));
printf("Area of circle is %0.2f\n",area(c));
printf("Area of circle is %0.2f\n",area(d));
printf("Area of circle is %0.2f\n",area(e));

getch();
}
float area(int u)
{
float A1;
A1=3.14*u*u;
return(A1);
}

Write a program to read a four-digit number and send it to a function to add the digits. Print the result in the main function.

//four digit positive integer entered from keyboard, write a function to add the digits
#include<stdio.h>
#include<conio.h>
int sum(int);
void main()
{
clrscr();
int a,addition;
printf("Enter four digit positive number\n");
scanf("%d",&a);
addition=sum(a);
printf("The sum of the digits is %d",addition);
getch();
}

int sum(int x)
{
int a,b,c,sumdigit;
a=x%10;
x=x/10;
b=x%10;
x=x/10;
c=x%10;
x=x/10;
sumdigit = a+b+c+x;
return(sumdigit);
}

WAP with a function to calculate the factorial of a number. Print the result in the main function.

//write a function to calculate factorial of a number
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
clrscr();
int a;
printf("Enter number for which factorial is to be found\n");
scanf("%d",&a);
printf("The factorial of this number is %d", factorial(a));
getch();
}

int factorial(int x)
{
int product=1,a;
for(a=x;a>1;a--)
{
product=product*a;
}
return(product);
}

Write a program with a function to reverse the number entered by a user. Print the result in the main function.

//write a program to reverse the number entered by a user
#include<conio.h>
#include<stdio.h>
#include<math.h>
int reverse(int);
void main()
{
clrscr();
int x;
printf("Enter a number to be reversed\n");
scanf("%d",&x);
printf("The reversed number is %d",reverse(x));
getch();
}
int reverse(int a)
{
int b, newnum=0;
while(a>0)
{
b=a%10;
a=a/10;
newnum=newnum*10+b; //IKFT
}
return(newnum);
}

Write a program with a function to calculate the highest common factor (HCF)/Greatest common divisor (GCD) of two numbers. Print the result in the function itself.

//write a function to calculate the GCD/HCF of two numbers
#include<stdio.h>
#include<conio.h>
void gcd(int,int);
void main()
{
clrscr();
int a, b;
printf("Enter the two numbers you wish to calculate the GCD/HCF of\n");
scanf("%d%d",&a,&b);
gcd(a,b);
}

void gcd(int m, int n)
{
int c,x,z;
float y;
for(c=1;c<=m;c++)
{
x=m%c;
y=n%c;
if(x==0 && y==0)
{
z=c;
}
}
printf("greatest cd is %d",z);
getch();
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.