Ans 1. #include<stdio.
h>
int power(int x, int y);
void main()
int a,b,c;
printf("Enter a number:");
scanf("%d",&a);
printf("Enter the power:");
scanf("%d",&b);
c=power(a,b);
printf("Result:%d",c);
int power(int x, int y)
int i,r=1,t;
for(i=1;i<=y;i++)
t=x;
r=r*t;
return r;
Ans 2. #include<stdio.h>
int bill(int n);
void main()
int a,b,c;
printf("Enter the Mealcost :");
scanf("%d",&a);
c=bill(a);
printf("Total Bill is $%d",c);
int bill(int n)
float mealCost = n;
float tax, tip, total;
tax = 0.2 * mealCost;
tip = 0.15 * (mealCost + tax);
total = mealCost + tax + tip;
return total;
Ans 3. #include <math.h>
#include <stdio.h>
#include <conio.h>
double quad(int a, int b, int c);
void main()
double d, e, f;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &d, &e, &f);
quad(d,e,f);
getch();
double quad(int a, int b, int c)
double discriminant, root1, root2, realPart, imagPart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
Ans 4. #return type with no argument
#include<stdio.h>
#include<math.h>
void main()
{
printf("Enter a Number to Find Factorial: ");
printf("\nFactorial of a Given Number is: %d ",fact());
return 0;
}
void fact()
{
int i,fact=1,n;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
return fact;
}
#no return type with argument
#include<stdio.h>
#include<math.h>
void main()
{
int num;
printf("Enter a Number to Find Factorial: ");
scanf("%d",&num);
fact(num);
}
int fact(int n)
{
int i,fact=1;
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf("\nFactorial of a Given Number is: %d ",fact);
}
#no return type with no argument
#include<stdio.h>
#include<math.h>
void main()
{
fact();
}
int fact()
{
int i,fact=1,n;
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf("\nFactorial of a Given Number is: %d ",fact);
}
# function with return type and argument
#include<stdio.h>
#include<math.h>
void main()
{
int num;
printf("Enter a Number to Find Factorial: ");
scanf("%d",&num);
printf("\nFactorial of a Given Number is: %d ",fact(num));
}
int fact(int n)
{
int i,fact=1;
for(i=1; i<=n; i++)
{
fact=fact*i;
}
return fact;
}
Ans 5.
#include <stdio.h>
int binomialCoeff(int n, int k)
{
if (k > n)
return 0;
if (k == 0 || k == n)
return 1;
return binomialCoeff(n - 1, k - 1)+ binomialCoeff(n - 1, k);
}
void main()
{
int n = 5, k = 2;
printf("Value of C(%d, %d) is %d ", n, k,
binomialCoeff(n, k));
return 0;
}
Ans 6.
#include <stdio.h>
int bill(int unit)
{
float amt, total_amt, sur_charge;
if(unit <= 100)
{
amt = unit * 2.00;
}
else if(unit>100 && unit<= 200)
{
amt = 25 + ((unit-100) * 3.50);
}
else if(unit >= 251)
{
amt = 100 + ((unit-150) * 4.50);
}
sur_charge = amt * 0.10;
total_amt = amt + sur_charge;
return total_amt;
}
void main()
{
int u;
printf("Enter total units consumed: ");
scanf("%d", &u);
printf("Electricity Bill = Rs. %.2f", bill(u));
return 0;
}
Ans 7.
#include <stdio.h>
int hammingDistance(int n1, int n2)
{
int x = n1 ^ n2;
int setBits = 0;
while (x > 0) {
setBits += x & 1;
x >>= 1;
}
return setBits;
}
long bin1(int a)
{
long binarynum = 0;
int rem, temp = 1;
int num=a;
while (a!=0)
{
rem = a%2;
a = a / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
printf("Equivalent Binary Number of %d is: %ld\n",num, binarynum);
}
long bin2(int b)
{
long binarynum = 0;
int rem, temp = 1;
int num=b;
while (b!=0)
{
rem = b%2;
b = b / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
printf("Equivalent Binary Number of %d is: %ld\n",num, binarynum);
}
void main()
{
int u,v;
printf("Enter two numbers: ");
scanf("%d %d", &u,&v);
bin1(u);
bin2(v);
int val=hammingDistance(u,v);
printf("Humming distance is: %d",val);
}
Ans 8.
#include <stdio.h>
#include <math.h>
long prime(long n)
{
long t, r = 0, c, d;
while (1)
{
n++;
t = n;
while(t)
{
r = r*10;
r = r + t%10;
t = t/10;
}
if (r == n)
{
d = (int)sqrt(n);
for (c = 2; c <= d; c++)
{
if (n%c == 0)
break;
}
if (c == d+1)
break;
}
r = 0;
}
return n;
}
void main()
{
long u;
printf("Enter the numbers: ");
scanf("%ld", &u);
printf("Next Prime Palindrome is: %d",prime(u));
}
Ans 9.
#include <stdio.h>
#include <math.h>
#define SIZE 1019
void main()
{
int t,n,arr[SIZE];
int X=1,l;
printf("Enter the number of test cases:");
scanf("%d",&t);
if (t>=1&&t<=5)
{
for (int i=0;i<=t;i++)
{
printf("Enter the number of elements:");
scanf("%d",&n);
if (n>=1&&n<=100)
{
for (int j=0;j<=n-1;j++)
{
printf("Enter the elements:");
scanf("%d", &arr[i]);
X = X * arr[i];
}
printf("The value of X is:%d \n",X);
for (l=1; l<=X ;l++)
{
if ((X%l*l)==0)
{
printf("The value of P is:%d ",l);
break;
}
}
break;
}
}
}
}
Ans 10.
#include <stdio.h>
void swap(int, int);
void main()
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
void swap(int a, int b)
int temp;
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
Ans 11.
In a programming language if we divide a large program into the basic building blocks
known as function. The function contains the set of programming statements enclosed by {}.
A function can be called multiple times to provide reusability and modularity to the C
program. In other words, we can say that the collection of functions creates a program. The
function is also known as procedureor subroutinein other programming languages.
There are two types of Functions in C.
1. Library Function
2. User-Defined Function
Categorize user defined functions.
Category I: Functions with no arguments and no return values
Category 2: Functions with no arguments and with return values
Category 3: Functions with arguments and no return values
Category 4: Functions with arguments and with return values
Ans 12.
Function Declaration:
A function declaration tells the compiler about a function name and how to call the function.
The actual body of the function can be defined separately.
Function Definition:
A function definition in C programming consists of a function header and a function body
Return Type
Function Name
Parameter
Function Body
Actual and Formal Arguments:
Arguments which are mentioned in the function call is known as the actual argument.
Arguments which are mentioned in the definition of the function is called formal
arguments.
Calling Function and Called Function:
The calling function contains the input (the actual parameters) which is
given to the called function which then works on them because it contains
the definition, performs the procedure specified and returns if anything is to
be returned.
Example:
#include<stdio.h>
void swap(int a, int b);
void main()
{
int m = 22, n = 44;
// calling swap function by value
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
int m = 22, n = 44;
// calling swap function by reference
printf(" values before swap m = %d \nand n = %d", m, n);
swap(&m, &n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}
Ans 13:
CALL BY VALUE:
In call by value method, the value of the variable is passed to the function as parameter.
The value of the actual parameter can not be modified by formal parameter.
Different Memory is allocated for both actual and formal parameters. Because, value of actual
parameter is copied to formal parameter.
Example:
#include<stdio.h>
void swap(int a, int b);
void main()
{
int m = 22, n = 44;
// calling swap function by value
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}
Ans 14.
void func(int x,int y)
{
int z;
…..
return z;
}
Correct :
int func(int x,int y)
{
int z;
…..
return z;
}
Ans 15.
int func(int x,y)
{
int z;
…..
return z;
}
Correct:
int func(int x, int y)
{
int z;
…..
return z;
}
Ans 16.
int func(int x,int y)
{
…..
int sum(int t)
…
{
…
return(t+3);
}
…..
return z;
}
Correction:
A function Can return a single value and can use a single return statement.
Ans 17.
int func(int,x)
{
…..
return ;
}
Correction:
int func(int,x)
{
…..
Return x ;
}
A return statement can not be empty.
Ans 18.
int sum(int x,y);
int sum(int x,int y);
void sum(void,void);
void sum(x int ,y float);
correction:
int sum(int x,int y);
int sum(int x,int y);
void sum();
void sum(x int , float y);
Ans 19.
void func ( );
fun(void);
void fun(int x,int y);
fun ( )
Correction :
void func ( );
fun();
void fun(int x,int y);
fun ();
Since void is not a data type..
Ans 20.
#include <stdio.h>
float formula(int x);
void main()
float a;
float b = formula(a);
Ans 21.
#include <stdio.h>
void display(int a, int b);
void main()
int x,y;
display(x,y);
}
Ans 22.
#include <stdio.h>
char convert(char x);
Void main()
char a;
char b = convert(a);
Ans 23.
#include<stdio.h>
double process ( int a, float b, float c)
double d= a+b+c;
return d;
void mainI()
Int x;
float y,z;
double w= process(x,y,z);
Ans 24.
No, the order does not matter in c If a function uses another function that is textually written above
it in the file, then this will automatically be true. If the function uses a function that is "below it" in a
file, then the prototype should occur at the top of the file. So prototype has to be defined before
main function.
Ans 25.
The code will give an error in line
ch >= 45 ? return (3.14): return (6.28);
error: expected expression before ‘return’.
Ans 26.
Output:
a = 12.560000
area=-1.#QNAN0
Ans 27.
Output:
i=5 k=5l=5
Ans 28.
Output:
k = 38
Ans 29. – Ans 32.
Are repeated.
Ans 33.
Output:
z = 37
Ans 34.
Output:
i=136 a=136 k=0.000000
Ans 35.
Advantages Of Using Functions:
Use of functions enhances the readability of a program. A big code is always difficult to read.
Breaking the code in smaller Functions keeps the program organized, easy to understand
and makes it reusable.
The C compiler follows top-to-down execution, so the control flow can be easily managed in
case of functions. The control will always come back to the main() function.
It reduces the complexity of a program and gives it a modular structure.
Ans 36.
Yes any function (user-defined or from a library) can be called any number of times from
any function including itself(in case of user-defined, called recursion) except for the main
function. But a function can only be defined once.
Ans 37.
A: Output: 9
B: Output: 12 12
C: Output: k=38
D: Output: 12,12
E: Output: 100
F: Output: 5
G: Output: 2.000000