[D] Answer the following:
(a) Write a function to calculate the factorial value of any integer entered through the keyboard.
Show Solutions
Hide Solutions
/* 4 (a) Write a function to calculate the factorial value of any integer entered through the keyboard. */
#include<stdio.h>
#include<conio.h>
int factorial(int n);
int main()
{
int n,fact;
printf("enter no. to find factorial ");
scanf("%d",&n);
fact=factorial(n);
printf("the factorial of %d is %d",n,fact);
getch();
return 0;
}
int factorial(int no)
{
int fact;
fact=no;
if(no==0)
{
fact=1;
return(fact);
}
else
{
fact=fact*factorial(no-1); //recursive function..it goes on until no==0 at last fact=1..
//it goes as fact*(fact-1)*(fact-2)*....1
return fact;
}
}
/*
understanding problem :
factoria of 0 is 1 so, if(no==0) fact=1;
for no 3:
step1:
fact=(fact=3)*factorial(2)
step2:
fact=(fact=3)*(fact=2)*factorial(1)
step3:
fact=(fact=3)*(fact=2)*(fact=1)*factorial(0)
since no==0 fact =1 returns so
finally, fact=3*2*1*1 which is results
*/
(b) Write a function power ( a, b ), to calculate the value of a raised to b.
Show Solutions
Hide Solutions
/* 4 (b) Write a function power ( a, b ), to calculate the value of a raised to b. */
#include<stdio.h>
#include<conio.h>
int power(int a,int b);
int main()
{
int a,b,value;
printf("enter the value of a and b: ");
scanf("%d%d",&a,&b);
value=power(a,b);
printf("the value of a = %d raised to b = %d is %d",a,b,value);
getch();
return 0;
}
int power(int a,int b)
{
int i,value;
value=a;
for(i=1;i<b;i++)
{
value=value*a;
}
return value;
}
/*
understanding problem :
value of a is multiplied b times through loop inside function.
*/
(c) Write a general-purpose function to convert any given year into its roman equivalent. The following table shows the roman equivalents of decimal numbers: Example: Roman equivalent of 1988 is mdcccclxxxviii Roman equivalent of 1525 is mdxxv
Show Solutions
Hide Solutions
/* 4 (c) Write a general-purpose function to convert any given year into its
roman equivalent. The following table shows the roman equivalents of decimal
numbers: Example: Roman equivalent of 1988 is mdcccclxxxviii Roman equivalent
of 1525 is mdxxv */
#include<stdio.h>
#include<conio.h>
int power(int a,int b);
int main()
{
int year;
printf("Enter the year in AD to convert to roman: ");
scanf("%d",&year);
printf("The equivalent year in roman is: ");
roman(year);
}
roman(int a)
{
int x,p,q,r,s,t;
x=a;
p=x/1000; //finding 1st digit among 4 digit of year
x=x%1000;
q=x/100; //finding 2nd digit among 4 digit of year
x=x%100;
r=x/10; //finding 3rd digit among 4 digit of year
s=x%10;
for (;p>=1;p--)
printf ("\n m");
if (q>5)
{
t = q -5;
printf("d");
for (;t>=1;t--)
printf("c");
}
else if (q<5)
{
for (;q>=1;q--)
printf("c");
}
else
printf ("d");
if (r>5)
{
t = r-5;
printf("l");
for (;t>=1;t--)
printf("x");
}
else if (r<5)
{
for(;r>=1;r--)
printf("x");
}
else
printf ("l");
if (s>5)
{
t=s-5;
printf("v");
for(;t>=1;t--)
printf("i");
}
else if (s<5)
{
for(;s>=1;s--)
printf("i");
}
else
printf("v");
getch();
return 0;
}
(d) Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.
Show Solutions
Hide Solutions
/* 4 (d) Any year is entered through the keyboard. Write a function to determine
whether the year is a leap year or not.*/
#include<stdio.h>
#include<conio.h>
int power(int a,int b);
int main()
{
int year;
printf ("Enter the year to be checked for a Leap Year: ");
scanf ("%d", &year);
if (year%4==0)
{
printf ("The year is a Leap Year. \n");
}
else
{
printf("The year is not a Leap Year. \n");
}
getch();
return 0;
}
/*
A leap year is a year containing one additional day (or, in the case of
lunisolar calendars, a month) in order to keep the calendar year synchronized
with the astronomical or seasonal year
*/
(e) A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.
Show Solutions
Hide Solutions
/* 4 (e) A positive integer is entered through the keyboard. Write a function
to obtain the prime factors of this number.*/
#include<stdio.h>
#include<conio.h>
void factor(int n);
int main()
{
int num;
printf("Enter a num: ");
scanf("%d",&num);
printf("\t\tprime factors are: ");
factor(num);
getch();
return 0;
}
void factor(int n)
{
static int i=2;//last value of i will be used for every function repeated.
if (i<=n)
{
if (n%i==0)
{
printf(" %d, ",i);
n=n/i;
}
else
i++;
factor(n);
}
return;
}
[F] Answer the following:
(a) Write a function which receives a float and an int from main( ), finds the product of these two and returns the product which is printed through main( ).
Show Solutions
Hide Solutions
/* f (a) Write a function which receives a float and an int from main(), finds the product
of these two and returns the product which is printed through main().*/
#include<stdio.h>
#include<conio.h>
float product(int x,float y);
int main()
{
int i;
float j, prod;
printf("Enter the no in int and float :");
scanf ("%d %f", &i, &j);
prod = product(i,j);
printf("Product is : %f", prod);
getch();
return 0;
}
float product (int x, float y)
{
float product;
product = x*y;
return (product);
}
(b) Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main( ) and print the results in main( ).
Show Solutions
Hide Solutions
/* f (b)Write a function that receives 5 integers and returns the sum,
average and standard deviation of these numbers. Call this function from
main( ) and print the results in main( ).*/
#include<stdio.h>
#include<conio.h>
int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);
int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;
float sd=0.0;
printf("Enter Five Numbers to find sum average and standard deviation: ");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
calc (a, b, c, d, e, &sum, &avg, &sd);//here address of sum
//avg and sd is sent
printf("\nSum = %f ", sum);
printf("\nAverage = %f ", avg);
printf("\nStandard Deviation(sd) = %f \n", sd);
getch();
return 0;
}
int calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)
{
float Calc=0.0;
*sum = a+b+c+d+e;
*avg = *sum / 5.0;
Calc = Calc + ( a - *avg) * ( a - *avg);
Calc = Calc +( c - *avg) * ( c - *avg);
Calc = Calc + ( d - *avg) * ( d - *avg);
Calc = Calc + ( e - *avg) * ( e - *avg);
*sd = sqrt((double)Calc/5.0);
}
(c) Write a function that receives marks received by a student in 3 subjects and returns the average and percentage of these marks. Call this function from main( ) and print the results in main( ).
Show Solutions
Hide Solutions
/* f (c)Write a function that receives marks received by a student in 3
subjects and returns the average and percentage of these marks. Call this
function from main( ) and print the results in main( ).*/
#include<stdio.h>
#include<conio.h>
int calc(int i,int j,int k,int full,float *avg,float *per);
int main()
{
int a,b,c,full;
float avg,per;
printf("\nEnter the marks received by the student in 3 subjects \n and the \
total possible marks for 1 paper: \n");
scanf("%d%d%d%d",&a,&b,&c,&full);
calc(a,b,c,full,&avg,&per);
printf("\nThe average marks is %f.\nThe percentage is %f",avg,per);
getch();
return 0;
}
int calc(int i,int j,int k,int full,float *avg,float *per)
{
*avg=(i+j+k)/3.0;
*per=(*avg/full)*100.0;
}
[J] Attempt the following:
(a) A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number: (1) Without using recursion (2) Using recursion
Show Solutions
Hide Solutions
/* j (a)A 5-digit positive integer is entered through the keyboard,
write a function to calculate sum of digits of the 5-digit number:
(1) Without using recursion (2) Using recursion*/
#include<stdio.h>
#include<conio.h>
int calc(int num);
int sum(int n);
int main()
{
int num,total,rec_total;
printf("Enter 5-digit positive integer : \n");
scanf("%d",&num);
total=calc(num);
printf("\n\n using recursion sum is: ");
rec_total=sum(num);
printf("%d",rec_total);
getch();
return 0;
}
int calc (int num)
{
int total,next,last;
last = num%10;
total = last;
next = (num/10)%10;
total = total+next;
next = (num/100)%10;
total = total+next;
next = (num/1000)%10;
total = total+next;
next = (num/10000)%10;
total = total+next;
printf("Sum of digit is: %d ",total);
}
int sum(int n)
{
if(n==0)
return 0;
else
return(n%10+sum(n/10));
}
(b) A positive integer is entered through the keyboard, write a program to obtain the prime factors of the number. Modify the function suitably to obtain the prime factors recursively.
Show Solutions
Hide Solutions
/* j (b) A positive integer is entered through the keyboard, write a program
to obtain the prime factors of the number. Modify the function suitably to
obtain the prime factors recursively.*/
#include<stdio.h>
#include<conio.h>
int factorial(int n);
int main()
{
int n,fact;
printf("enter no. to find factorial ");
scanf("%d",&n);
fact=factorial(n);
printf("the factorial of %d is %d",n,fact);
getch();
return 0;
}
int factorial(int no)
{
int fact;
fact=no;
if(no==0)
{
fact=1;
return(fact);
}
else
{
fact=fact*factorial(no-1); // recursive function..it goes on until no==0
// at last fact=1..
//it goes as fact*(fact-1)*(fact-2)*....1
return fact;
}
}
/*
understanding problem :
factoria of 0 is 1 so, if(no==0) fact=1;
for no 3:
step1:
fact=(fact=3)*factorial(2)
step2:
fact=(fact=3)*(fact=2)*factorial(1)
step3:
fact=(fact=3)*(fact=2)*(fact=1)*factorial(0)
since no==0 fact =1 returns so
finally, fact=3*2*1*1 which is results
*/
(c) Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89...
Show Solutions
Hide Solutions
/* j (c) Write a recursive function to obtain the first 25 numbers of a
Fibonacci sequence. In a Fibonacci sequence the sum of two successive
terms gives the third term. Following are the first few terms of the
Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89...
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int no=25,a,b,c,i;
a=0;
b=1;
printf("%d %d ",a,b);
for(i=3;i<=no;i++)
{
c=a+b;
printf(" %d ",c);
a=b;
b=c;
}
getch();
}
(d) A positive integer is entered through the keyboard, write a function to find the binary equivalent of this number using recursion.
Show Solutions
Hide Solutions
/* j (d) A positive integer is entered through the keyboard, write a
function to find the binary equivalent of this number using recursion.
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int dec2bin(int n);
int main()
{
int n;
printf("Enter a decimal no to find binary equivalent: ");
scanf("%d",&n);
dec2bin(n);
getch();
return 0;
}
dec2bin(int n)
{
int bin;
printf("The binary equivalent is from right to left: \n ");
while(n!=0)
{
bin=n%2;
printf("%d ",bin);
n=n/2;
}
}
(e) Write a recursive function to obtain the running sum of first 25 natural numbers.
Show Solutions
Hide Solutions
/* j (e) Write a recursive function to obtain the running sum of
first 25 natural numbers.
*/
#include<stdio.h>
#include<conio.h>
int natural_sum(int x);
int main()
{
int d,e;
d=25;
e=natural_sum(d);
printf("\n The sum of the first %d natural numbers is: %d",d,e);
getch();
return 0;
}
int natural_sum(int x)
{
int a;
if(x==1)
return(1);
else
{
a=x+natural_sum(x-1);//recursive function.
return(a);
}
}
(f) Write a C function to evaluate the series
sin(x)=x-(x^3/3!)+(x^5/5!)-(x^7/7!)+..... to five significant digits.
Show Solutions
Hide Solutions
/* j (f) Write a C function to evaluate the series
sin(x)=x-(x^3/3!)+(x^5/5!)-(x^7/7!)+..... to five significant digits.
*/
#include<stdio.h>
#include<conio.h>
float numerator(float x1,int j);
float denominator(int j);
int main()
{
float n,x,a,b,sum=0;
int i,j;
printf("Enter a number x: ");
scanf("%f",&x);
for(i=1,j=1;j<=19;j=j+2)
{
a=numerator(x,j); //for finding numerator value
b=denominator(j); //for finding denominator value
n=a/b; //n takes float value
if(i%2==0)
{
sum=sum-n;
}
else
{
sum=sum+n;
}
}
printf(" sin(%f) = %f",x,sum);
getch();
return 0;
}
float numerator(float x1,int j) //function for finding numerator value
{
float k=1;
int m;
for(m=1;m<=j;m++)
k=k*x1;
return(k);
}
float denominator(int j) //function for finding denominator value
{
int m;
float h=1.;
for(m=1;m<=j;m++)
h=h*m;
return(h);
}
(g) Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10 after circular shift y = 5, z = 8 and x = 10. Call the function with variables a, b, c to circularly shift values.
Show Solutions
Hide Solutions
/* j (g) Given three variables x, y, z write a function to circularly
shift their values to right. In other words if x = 5, y = 8, z = 10
after circular shift y = 5, z = 8, x =10 after circular shift
y = 5, z = 8 and x = 10. Call the function with variables a, b, c
to circularly shift values.
*/
#include<stdio.h>
#include<conio.h>
void fun(int x,int y,int z);
int main()
{
int x,y,z;
printf("Enter the values of x, y, z: ");
scanf("%d%d%d",&x,&y,&z);
fun(x,y,z);
getch();
return 0;
}
void fun(int x,int y, int z)
{
int i,t;
for(i=0;i<=2;i++)
{
t=z;
z=y;
y=x;
x=t;
printf("\n Afte r right shifting values %d time: ",i+1);
printf("\n x=%d y=%d z=%d",x,y,z);
}
}
(h) Write a function to find the binary equivalent of a given decimal integer and display it.
Show Solutions
Hide Solutions
/* j (h) Write a function to find the binary equivalent of a given
decimal integer and display it.
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int dec2bin(int n);
int main()
{
int n;
printf("Enter a decimal no to find binary equivalent: ");
scanf("%d",&n);
dec2bin(n);
getch();
return 0;
}
dec2bin(int n)
{
int bin;
printf("The binary equivalent is from right to left:\n ");
while(n!=0)
{
bin=n%2;
printf("%d ",bin);
n=n/2;
}
}
(i) If the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by area=squareroot of(s(s-a)(s-b)(s-c) where, S = ( a + b + c ) / 2
Show Solutions
Hide Solutions
/* j (i) If the lengths of the sides of a triangle are denoted by
a, b, and c, then area of triangle is given by area=squareroot
of(s(s-a)(s-b)(s-c) where, S = ( a + b + c ) / 2
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float areat(float a,float b,float c);
int main()
{
float x,y,z,area;
printf("\n Input the three sides of the triangle: ");
scanf("%f%f%f",&x,&y,&z);
area=areat(x,y,z);
printf("\n The area of the triangle is %f: ",area);
getch();
return 0;
}
float areat(float a,float b,float c)
{
float s,area;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return(area);
}
(j) Write a function to compute the distance between two points and use it to develop another function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use these functions to develop a function which returns a value 1 if the point (x, y) lines inside the triangle ABC, otherwise a value 0.
Show Solutions
Hide Solutions
/* j (j) Write a function to compute the distance between two points
and use it to develop another function that will compute the area of
the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3).
Use these functions to develop a function which returns a value 1
if the point (x, y) lines inside the triangle ABC, otherwise a
value 0.
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float geodis(int x1,int y1,int x2,int y2);
int pcheck(int a,int b,int c,int d,int e,int f,int g,int h,float area);
float areat(float a,float b,float c);
int main()
{
int a,b,c,d,e,f,g,h,i=0;
float dis1,dis2,dis3,area;
printf("\nInput the 3 coordinates x1,y1,x2,y2,x3,y3: ");
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
//calculating distance between points
dis1=geodis(a,b,c,d);
dis2=geodis(a,b,e,f);
dis3=geodis(c,d,e,f);
//calculating area by function
area=areat(dis1,dis2,dis3);
printf("\nThe area of the triangle is %f: ",area);
printf("\nEnter a point(x,y) to check if it lies in the triangle. ");
scanf("%d%d",&g,&h);
i=pcheck(a,b,c,d,e,f,g,h,area);
printf("\n The value of i now is %d ",i);
if(i==1)
printf("\n The point lies inside the triangle: ");
else
printf("\n The point lies outside the triangle: ");
getch();
return 0;
}
//function for finding distance between points
float geodis(int x1,int y1,int x2,int y2)
{
float distance,x3,y3;
x3=x2-x1;
y3=y2-y1;
distance=sqrt(pow(x3,2)+pow(y3,2));
return(distance);
}
//function for calculating area
float areat(float a,float b,float c)
{
float area,s;
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return(area);
}
//function for detecting wheather point is inside triangle
pcheck(int a,int b,int c,int d,int e,int f,int g,int h,float area)
{
float area1,area2,area3;
float dis1,dis2,dis3,dis4,dis5,dis6;
int total;
dis1=geodis(a,b,e,f);
dis2=geodis(c,d,e,f);
dis3=geodis(a,b,c,d);
dis4=geodis(a,b,g,h);
dis5=geodis(e,f,g,h);
dis6=geodis(c,d,g,h);
area1=areat(dis4,dis3,dis6);
area2=areat(dis2,dis5,dis6);
area3=areat(dis1,dis5,dis4);
printf("\n\n Area in pcheck is %f: ",area);
printf("\n Area 1,2,3 is %f: ",area1+area2+area3);
total=area1+area2+area3-area;
printf("\n Total area is %d",total);
if(total==0)
return(1);
else
return(0);
}
(k) Write a function to compute the greatest common divisor given by Euclid's algorithm, exemplified for J = 1980, K = 1617 as follows:
1980 / 1617 = 1 , 1980 - 1 * 1617 = 363
1617 / 363 = 4 , 1617 - 4 * 363 = 165
363 / 165 = 2 , 363 - 2 * 165 = 33
5 / 33 = 5 , 165 - 5 * 33 = 0
Thus, the greatest common divisor is 33.
Show Solutions
Hide Solutions
/* j (k) Write a function to compute the greatest common divisor given
by Euclid's algorithm, exemplified for J = 1980, K = 1617 as follows:
1980 / 1617 = 1 , 1980 - 1 * 1617 = 363
1617 / 363 = 4 , 1617 - 4 * 363 = 165
363 / 165 = 2 , 363 - 2 * 165 = 33
5 / 33 = 5 , 165 - 5 * 33 = 0
Thus, the greatest common divisor is 33.
*/
#include<stdio.h>
#include<conio.h>
int func(int j,int k);
int main()
{
int j,k,t,r,z;
printf("\nEnter two numbers: ");
scanf("%d%d",&j,&k);
z=func(j,k);
printf("\n The greatest common divisor among two numbers is %d: ",z);
getch();
return 0;
}
func(int j,int k)
{
int r=1,m,n=0,t;
if(k>j)
{
t=j;
j=k;
k=t;
}
if(j==k)
{
return j;
}
while(1)
{
r=j/k;
m=j-(r*k);
if(!(j%k))
n=k;
if(m==0)
break;
j=k;
k=m;
n=m;
}
return n;
}
/*
understanding problem :
The greatest no. which can divide both value.
for eg : 8 16
the ans is 8..as 8 and 16 are divided by 8.
*/
Comments
Post a Comment