unit 4 c programming vvimp qns set
unit 4 c programming vvimp qns set
void main()
{
int fact,no;
clrscr();
printf("\n Enter number: "); scanf("%d",&no);
fact=factorial(no);
printf("\n Factorial number=%d",fact);
getch();
}
Output
Enter number :5
Factorial of number is :120
b) sqrt ()
This function returns the square root of a specified number.
Syntax:
sqrt( arg)
Example:
The below code explains the most known mathematical
function sqrt() by taking ‘n’ values to compute the square root
for the different ‘n’ values.
#include <stdio.h>
#include <math.h>
int main()
{
double n,output;
printf("Enter a
number\n");
scanf("%lf", &n);
output = sqrt(n);
printf("Square root
of %.2lf = %f",
n,output);
return 0;
}
5 Explain call by value with example. S-23 4M
ANS:
The call by value method of passing arguments to a function
copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the
parameter inside the function have no effect on the argument.
return;
}
Program:
#include <stdio.h>
/* function declaration */ void swap(int x, int y);
int main ()
{
return 0;
}
void swap(int x, int y) { int temp;
temp = x; /* save the value of x */ x = y; /* put y into x
*/
y = temp; /* put temp into y */
return;
}
#include <stdio.h>
#include <conio.h>
int fibonacci(int n)
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
int main()
int n;
scanf("%d", &n);
return 0; }
7 Write a C program to find Factorial of number using recursion. S-23 4M
ANS:
#include<stdio.h>
#include<conio.h>
int main()
{
4. int i,fact=1,number;
6. scanf("%d",&number);
7. for(i=1;i<=number;i++)
8. {
9. fact=fact*i;
10. }
10. printf("Factorial of %d is: %d",number,fact);
11. return 0;
}
{
float area;
area=3.14*radius*radius;
printf("Area of circle= %f",area);
}
void main() //main function
{
float r;
printf("Enter the radius of circle : ");
scanf("%f", &r);
circle_area(r);
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
11. int i,fact=1,number;
14. for(i=1;i<=number;i++)
15. {
16. fact=fact*i;
17. }
12. printf("Factorial of %d is: %d",number,fact);
13. return 0;
}
12 S-22,W- 4M
List the categories of functions and explain any one with
19
example.
For example:
void add()
Sr.
No Call by value Call by reference
.
else
return(no*factorial(no-1));
}
void main()
{
int fact,no;
clrscr();
printf("\n Enter number: ");
scanf("%d",&no);
fact=factorial(no);
printf("\n Factorial number=%d",fact);
getch();
}
16 Distinguish between call by value and call by reference. W-23,s- 4M
ANS: 19
Advantages:
Reduces length of the program
Reduces unnecessary calling of a function
Useful when same solution is to be applied many times.
20 Explain User defined function with example. S-18 4M
ANS:
Example:
#include<stdio.h>
#include<conio.h>
void myFunc(int a)
{
printf("The value is: %d",a);
}
void main()
{
myFunc(10);
getch():
}
21 Write a program to accept marks of four subjects as input from S-18 4M
user. Calculate and display total and percentage marks of
students using function.
ANS:
#include<stdio.h>
#include<conio.h>
void main() {
float marks[4];
float total=0.0, perc=0.0;
int i;
clrscr();
for(i=1;i<=4;i++)
{
printf("Enter marks of subject %d",i);
scanf("%f%",&marks[i]);
}
for(i=1;i<=4;i++){
total=total+marks[i];
}
printf("Total is :%f",total);
perc=total/4;
printf("Percentage is %f",perc);
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter any number to store in \"num\" variable: ");
scanf("%d", &num);
printf("\nValue of num = %d", num);
printf("\nAddress of num = %u", &num);
getch();
return 0;
}
24 If the value of a number (N) is entered through keyboard. Write W-18 6M
a program using recursion to calculate and display factorial of
number(N).
ANS:
#include<stdio.h>
#include<conio.h>
int factorial(int
num)
{
if(num==1)
{
return 1;
}
else
{
return(num*factorial(num-1));
}
}
void main()
{
int num;
int result;
clrscr();
printf("Enter a
number");
scanf("%d",&num);
result=factorial(num);
printf("Factorial of %d is
%d",num,result);
getch();
}
25 State any four math functions with its use. S-18 2M
(Note: Any other relevant math function shall be considered)
ANS:
Math Functions:
sqrt() - square root of an integer abs() - absolute value of an
integer
sin() - compute the sine value of an input
value cos()- compute the cosine value of an
input value pow()- compute the power of a
input value
floor()- round down the input value
ceil()- round up the input value
26 plain following functions: getchar( )
putchar( )
getch( )
putch( ) with suitable examples.
ANS:
getchar( ) -
It is function from stdio.h header file. This function is used to
input a single character.
The enter key is pressed which is followed by the character
that is typed. The character that is entered is echoed.
Syntax:
ch=getchar();
Example:
void main()
{
char ch;
ch = getchar();
printf("Input Char Is :%c",ch);
}
During the program execution, a single character gets or read
through the getchar(). The given value is displayed on the
screen and the compiler waits for another character to be typed.
If you press the enter key/any other characters and then only
the given character is printed through the printf function.
putchar( ) -
It is used from standard input (stdio.h) header file.
This function is the other side of getchar. A single character
is displayed on the screen.
Syntax:
putchar(ch);
void main()
{
void main()
{
char ch='a';
putch(ch)
}
getch():
getch() method pauses the Output Console until a
key is pressed.
It does not use any buffer to store the input
character.
putch(): is used to display a single character on the
console without buffering.
27 Develop a program to find factorial of a number using S-18 4M
recursion.
(Note: Any other relevant logic shall be considered)
ANS:
#include<stdio.h>
#include<conio.h>
int factorial(int
num)
{
if(num==1)
{
return 1;
}
else
{
return(num*factorial(num-1));
}
}
void main()
{
int num;
int result;
clrscr();
printf("Enter a number");
scanf("%d",&num);
result=factorial(num);
printf("Factorial of %d is %d",num,result);
getch();
}
28 Develop a program to find diameter, circumference and area of S-18 6M
circle using function.
(Note: Any other relevant logic shall be considered)
ANS:
#include<stdio.h>
#include<conio.h>
void circle(float r)
{
float diameter,circumference,area;
diameter=2*r;
printf("\n Diameter=%f",diameter);
circumference=2*3.14*r;
printf("\n Circumference=%f",circumference);
area=3.14*r*r;
printf("\n Area=%f",area);
}
void main()
{
float radius;
clrscr();
printf("\n Enter radius:");
scanf("%f",&radius);
circle(radius);
getch();
}