Professional Documents
Culture Documents
Exercise [D]
(a) Write a function to calculate the factorial value of any
integer entered through the keyboard.
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int num;
void func();
clrscr();
printf("Please enter any number: ");
scanf("%d",&num);
func(num);
getch();
}
void func(int n) {
int fact=1;
for(;n>=1;n--) {
fact=fact*n;
}
}
printf("\n\n\nValue of %d raised to the power of %d is = %d\n",a,b,c);
return 0;
}
------------------------------------------------------------------------------------------------------------
Roman
1
5
10
50
100
500
1000
i
v
x
l
c
d
m
Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int yr;
void func();
clrscr();
printf("Please enter the year: ");
scanf("%d",&yr);
printf("\n\nRoman Equivalent = ");
func(yr);
getch();
}
void func(int y) {
int d1,d2,d3,d4,d5,d6,d7,a,b,c,d,e,f;
char thsnd='m',hndr_5='d',hndr='c',ffty='l',tn='x',fv='v',one='i';
/******* Roman Convertion ********/
/* To find all thousands */
d1=y/1000;
a=y%1000;
for(;d1>=1;d1--) {
printf("%c",thsnd);
if(a==0)
break;
}
/* To find all five-hundreds */
d2=a/500;
b=a%500;
for(;d2>=1;d2--) {
printf("%c",hndr_5);
if(b==0)
break;
}
for(;d6>=1;d6--) {
printf("%c",fv);
if(f==0)
break;
}
/* To find all ones */
for(d7=f;d7>=1;d7--) {
printf("%c",one);
}
}
------------------------------------------------------------------------------------------------------------
if((y%4)==0)
printf("\nThis is a LEAP YEAR.\n");
else
printf("\nThis is NOT A LEAP YEAR.\n");
}
------------------------------------------------------------------------------------------------------------
for(i=2;i<=j;) {
if(j%i==0) {
j=j/i;
i=i+1;
}
getch();
}
________________________________________________________________________
Exercise [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( ).
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int i;
float j,k,product();
clrscr();
k=product(&i,&j);
printf("\nProduct = %.1f\n",k);
getch();
}
float product(int *a,float *b) {
float *c;
*c=(*a)*(*b);
return *c;
}
------------------------------------------------------------------------------------------------------------
/* sum of digits */
/* average */
a=a-(*av);
b=b-(*av);
c=c-(*av);
d=d-(*av);
e=e-(*av);
temp=((a*a)+(b*b)+(c*c)+(d*d)+(e*e))/4;
/* standard deviation */
*ssd=sqrt(temp);
return 0;
}
------------------------------------------------------------------------------------------------------------
Exercise [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
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
long num,s=0,ch;
clrscr();
printf("Enter any number: ");
scanf("%ld",&num);
printf("\n\nChoose: 1: obtain sum of digits non-recursively\n\n");
printf("
return n;
}
}
-----------------------------------------------------------------------------------------------------------
d=d/b;
printf("%d ",b);
}
else
b++;
}
break;
case 2:
printf("\n\n\nPrime factors: ");
factors(d);
break;
default:
printf("\n\nwrong input!");
break;
}
getch();
}
int factors (int n) {
int b=2;
if(n==1)
return 1;
else {
while(n!=1) {
if((n%b)==0) {
n=factors(n/b);
printf("%d ",b);
}
/* recursive function */
else
b++;
}
return n;
}
}
------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main() {
unsigned i,num=25,c=1;
clrscr();
for(i=0;i<num;i++) {
printf("%u ",fib(c));
c++;
}
getch();
}
fib(unsigned n) {
if(n==0)
return 0;
if(n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}
------------------------------------------------------------------------------------------------------------
printf("%d",n%2);
n= binary( n/2 ); /* recursive function */
return n;
}
}
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
func(&x,&y,&z);
/* Call by reference */
int num;
void binary();
clrscr();
printf("\t\tDecimal Integer to Binary conversion\n\n\n");
printf("Enter the number: ");
scanf("%d",&num);
printf("\n\n\nBinary equivalent: ");
binary(num);
gotoxy(20,10);
printf("<%c%c%c%c%c",196,196,196,196,196);
printf(" read right to left");
getch();
}
void binary(int n) {
while(n!=0) {
printf("%d",n%2);
n=n/2;
}
}
------------------------------------------------------------------------------------------------------------
#include<math.h>
void main() {
int s1,s2,s3,s;
int area;
clrscr();
printf("enter 3 sides of triangle: \n\n");
scanf("%d%d%d",&s1,&s2,&s3);
s=s1+s2+s3/2;
area=func(s1,s2,s3,s);
printf("\narea = %d",area);
getch();
}
func(int i, int j, int k, int h) {
int ar,area;
ar=sqrt(h*((h-i)*(h-j)*(h-k)));
return (ar);
}
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
1.
AMAR KALDATE19 May 2014 10:58
Write a C function to evaluate the series sin(x) = x - (x3/3!) + ( x5/5!) - (x7/7!)
+ ........
to five significant digits.
#include
float sin(int);
void main()
{
int no;
float sum;
printf("Please Enter The No\t");
scanf("%d", &no);
sum = sin(no);