Академический Документы
Профессиональный Документы
Культура Документы
[D] a.Write a function to calculate the factorial value of any integer entered through the
keyboard.
Ans:#include<stdio.h>
long Factorial(int);
int main(int argc, char *argv[])
{
int num;
long i;
printf("Enter a number: ");
scanf("%d",&num);
i=Factorial(num);
printf("Factorial of %d is: %ld\n",num,i);
return 0;
}
long Factorial(int num)
{
int i;
long j=1;
for(i=1;i<=num;i++)
j=j*i;
return j;
}
}
long power(int a,int b)
{
int i;
long j=1;
for(i=1;i<=b;i++)
j=j*a;
return j;
}
[D] 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:
Ans:#include <stdio.h>
void roman(int);
int main(int argc, char *argv[])
{ int yr;
printf("Enter yr");
scanf("%d",&yr);
roman(yr);
return 0;
}
void roman(int a)
{ int i;
if(a<5)
{
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<10&&a>=5)
{
printf("%c",'V');
for(i=1;i<=a%5;i++)
printf("%c",'I');
}
if(a<50&&a>=10)
{ for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<100&&a>=50)
{ for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<500&&a>=100)
{
for(i=1;i<=a/100;i++)
printf("%c",'C');
a=a-100*(i-1);
for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<1000&&a>=500)
{
for(i=1;i<=a/500;i++)
printf("%c",'D');
a=a-500*(i-1);
for(i=1;i<=a/100;i++)
printf("%c",'C');
a=a-100*(i-1);
for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
a=a-500*(i-1);
}
if(a>=1000)
{ for(i=1;i<=a/1000;i++)
printf("%c",'M');
a=a-1000*(i-1);
for(i=1;i<=a/500;i++)
printf("%c",'D');
a=a-500*(i-1);
for(i=1;i<=a/100;i++)
printf("%c",'C');
a=a-100*(i-1);
for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
a=a-500*(i-1);
}
}
[D] d.Any year is entered through the keyboard. Write a function to determine whether the
year is a leap year or not.
Ans:/*Rule 1: A year is called leap year if it is divisible by 400.
For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are
also leap year.
For example: 2004, 2008, 1012 are leap year.
*/
#include <stdio.h>
void leap(int);
int main(int argc, char *argv[])
{ int yr;
printf("Enter the yr");
scanf("%d",&yr);
leap(yr);
return 0;
}
void leap(int yr)
{
if(yr%400==0)
printf("Leap Yr");
else if(yr%4==0&&yr%100!=0&&yr%400!=0)
printf("Leap Yr");
else
printf("Not Leap Yr");
}
[D] e.A positive integer is entered through the keyboard. Write a function to obtain the prime
factors of this number.
Ans:#include <stdio.h>
#include<conio.h>
void prime(int);
int main(int argc, char *argv[])
{ int num;
printf("\nEnter a number:");
scanf("%d",&num);
prime(num);
getch();
return 0;
}
[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( ).
Ans:#include <stdio.h>
#include<math.h>
void std_avg(float*,float*,float*,int*);
int main(int argc, char *argv[])
{
int num[4],i;
float std,avg,sum;
/*They are variable*/
printf("\nEnter 5 number:");
for(i=0;i<5;i++)
scanf("%d",&num[i]); /*Array is entered through loop*/
std_avg(&std,&avg,&sum,&num); /*Address of array is passed*/
printf("Std %f",std);
printf("\nAvg %f",avg);
printf("\nSum %f",sum);
return 0;
}
void std_avg(float *s,float*a,float*su,int*p)
{
int i;
*su=0;
*s=0;
for(i=0;i<5;i++)
*su=*su+*(p+i);
*a=*su/5;
for(i=0;i<5;i++)
*s=*s+(*(p+i)-*a)*(*(p+i)-*a);
*s=*s/5;
*s=sqrt(*s);
}
[J] a.(a) A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number:
[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.
Ans:#include<stdio.h>
void prime(int);
void prime_rec(int);
int main()
{
int x;
printf("\nInput an integer\n");
scanf("%d",&x);
printf("Prime Factors Are: ");
prime_rec(x);
}
}
}
}
}
}
[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...
Ans:#include <stdio.h>
void recursive(int,int,int);
int main(int argc, char *argv[])
{ int a,b,d;
a=b=1;
d=25;
printf("Fb series is ");
recursive(d-1,a,b);
return 0;
}
void recursive(int g,int a,int b)
{ int c;
if(g==25)
{printf("%d %d ",a,b);
recursive(g-1,a,b); /*used so that function can be called more than once*/
}
else
{printf("%d ",a+b);
c=a;
a=b;
b=c+b;
if(g>0) /*Necessary to terminate the function call*/
recursive(g-1,a,b);
}
}
[J] d.A positive integer is entered through the keyboard, write a function to find the binary
equivalent of this number using recursion.
Ans:#include <stdio.h>
void binary(int);
int main(int argc, char *argv[])
int no;
printf("Enter the no");
scanf("%d",&no);
binary(no);
return 0;
}
void binary(int n)
{ int y;
if(n>0)
{ y=n;
n=n/2;
binary(n);
printf("%d",y%2);
}
}
[J] e.Write a recursive function to obtain the running sum of first 25 natural numbers.
Ans:#include <stdio.h>
int sum(int,int);
int main(int argc, char *argv[])
{
int no,s1;
printf("Enter the number");
scanf("%d",&no);
s1=sum(no,1);
printf("%d",s1);
return 0;
}
int sum(int s,int i)
{
int y;
/*Change the value of i to find the consecutive sum of Nth
digit starting from s i.e. s+(s+1)+...(s+N)*/
if(i==6)
{
y=0;
return 0;
}
else
{ i=i+1;
y=s+sum(s+1,i);
}
return y;
{
int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return f;
}
[J] h.Write a function to find the binary equivalent of a given decimal integer and display it.
Ans:#include<stdio.h>
int main(int argc, char *argv[])
{
long int decimalNumber,remainder,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0)
{
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: \n",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return 0;
}
[J] k.Write a function to compute the greatest common divisor given by Euclids algorithm,
exemplified for J = 1980, K = 1617 as follows:
/*
Enjoy The World
Happy Coding
*/
Answer : #include
main()
{
int power (a,b);
int a, b, result;
printf("Enter the value of a and b:");
scanf ("%d %d", &a, &b);
result=power(a,b);
printf("%d raised to %d is %d", a, b, result);
}
power (int a, int b)
{
int calculation=1, calc;
for (calc=1; calc <=b; calc++) { calculation=calculation*a; continue; } return(calculation); }
hide
Answer:#include
main()
{
int year;
int convert (int year);
{
printf("Note:Enter a four year digit year.\n\n");
printf("Enter the year that you wanna convert to Roman: " );
scanf ("%d", &year);
if (year> 1999)
{
printf("Invalid Year.Please enter again.\n\n");
}
}
convert(year);
convert(int year)
{
int i;
printf("\nYear converted to Roman:");
{
printf("m");
}
printf("dcc");
break;
case 8:
printf("dccc");
break;
case 9:
printf("dcccc"); //this part you may think is wrong..9 -> cm
break; //but i have taken a hint from the example in the question.
}
case 5:
printf("l");
break;
case 6:
printf("lx");
break;
case 7:
printf("lxx");
break;
case 8:
printf("lxxx");
break;
case 9:
printf("lxxxx"); //had it not been for this example, it would have been xc
break;
}
break;
case 3:
printf("iii");
break;
case 4:
printf("iv");
break;
case 5:
printf("v");
break;
case 6:
printf("vi");
break;
case 7:
printf("vii");
break;
case 8:
printf("viii");
break;
case 9:
printf("ix");
break;
}
printf ("\n\n");
return 0;
}
Answer: #include
main()
{
int leap_year(year);
int year, lp;
printf("Enter the year:");
scanf ("%d", &year);
lp=leap_year(year);
if (lp)
{
printf("\nThe entered year is a leap year.");
}
else
{
printf("\nThe entered year is not a leap year.");
}
}
leap_year(int y)
{
int lp;
if (y%4==0)
{
lp=1;
}
else
lp=0;
return(lp);
Answer:#include
main()
{
int number;
int prime(int number);
int primefactor(int number);
{
int factor,ifprime;
for (factor=2; factor<=num;
)
{
prime(factor);
//so that the factors are only prime and nothing else.
if (ifprime)
{
if(num%factor==0) //diving by all the prime numbers less than the number itself.
{
printf("%d ", factor);
num=num/factor;
continue;
}
else
{
factor++;//this cannot be made a part of the for loop
}
}
}
return 0;
}
func(num);
getch();
}
void func(int n) {
int fact=1;
for(;n>=1;n--) {
fact=fact*n;
}
printf("\n\nFactorial value = %d \n",fact);
}
------------------------------------------------------------------------------------------------------------
power(num1,num2);
getch();
}
power(int a , int b) {
int c=1,i;
for(i=1;i<=b;i++) {
c=c*a;
if(i==b) {
break;
}
}
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';
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;
}
/* To find all hundreds */
d3=b/100;
c=b%100;
for(;d3>=1;d3--) {
printf("%c",hndr);
if(c==0)
break;
}
/* To find all fifties */
/***********************/
d4=c/50;
d=c%50;
for(;d4>=1;d4--) {
printf("%c",ffty);
if(d==0)
break;
}
/* To find all tens */
/********************/
d5=d/10;
e=d%10;
for(;d5>=1;d5--) {
printf("%c",tn);
if(e==0)
break;
}
/* To find all fives */
d6=e/5;
f=e%5;
for(;d6>=1;d6--) {
printf("%c",fv);
if(f==0)
break;
}
/* To find all ones */
for(d7=f;d7>=1;d7--) {
printf("%c",one);
}
------------------------------------------------------------------------------------------------------------
#include<conio.h>
void main() {
int i,j,k;
clrscr();
printf("enter the number: ");
scanf("%d",&j);
printf("\n\nprime factors:");
for(i=2;i<=j;) {
if(j%i==0) {
j=j/i;
printf(" %d ",i); /* print the divisor */
}
else
i=i+1;
getch();
}
________________________________________________________________________
Exercise [F]
#include<conio.h>
void main() {
int i;
float j,k,product();
clrscr();
getch();
}
float product(int *a,float *b) {
float *c;
*c=(*a)*(*b);
return *c;
}
------------------------------------------------------------------------------------------------------------
#include<math.h>
void main() {
int d1,d2,d3,d4,d5,i,sum,avg;
float sd;
clrscr();
printf("enter five digits: \n\n");
scanf("%d%d%d%d%d",&d1,&d2,&d3,&d4,&d5);
func(d1,d2,d3,d4,d5,&sum,&avg,&sd);
printf("\tsum = %d\n\taverage = %d\n\tstandard deviation = %f ",sum,avg,sd);
getch();
}
func(int a, int b, int c, int d, int e, int *s, int *av, float *ssd) {
float temp;
*s=a+b+c+d+e;
*av=(a+b+c+d+e)/5;
/* 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;
}
------------------------------------------------------------------------------------------------------------
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int s1,s2,s3,*avg,*prcnt;
void func();
clrscr();
printf("Please enter the marks of 3 subjects: \n");
scanf("%d%d%d",&s1,&s2,&s3);
func(s1,s2,s3,&avg,&prcnt);
printf(" Average = %d\nPercentage = %d%\n",avg,prcnt);
getch();
}
void func(int a, int b, int c, int *d, int *f) {
*d=(a+b+c)/3;
*f=(a+b+c)/3;
}
_____________________________________________________________________
Exercise [J]
else {
s=s+n%10;
n=sum(n/10);
return n;
}
}
-----------------------------------------------------------------------------------------------------------
switch(ch) {
case 1:
printf("\n\n\nPrime factors: ");
while(d!=1) {
if(d%b==0) {
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);
/* recursive function */
printf("%d ",b);
}
else
b++;
}
return n;
}
}
------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main() {
int i=25,j;
clrscr();
j=recsum(i);
printf("Addition of 25 natural numbers = %d",j);
getch();
}
int recsum(int n) {
if(n==1)
return 1;
else
n = n + recsum(n-1); /* recursive addition */
return n;
}
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
func(&x,&y,&z);
/* Call by reference */
f=*c;
*a=f;
*b=d;
*c=e;
}
------------------------------------------------------------------------------------------------------------
}
------------------------------------------------------------------------------------------------------------
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);
}
------------------------------------------------------------------------------------------------------------
d2=a;
getch();
}
Function C Programing
1.Write a function to calculate the factorial value of any integer entered
through the keyboard.
#include<stdio.h>
#include<conio.h>
long fact(int n)
{
int i;
long f=1;
for (i=1;i<=n;i++)
f*=i;
return (f);
}
void main()
{
int n,r;
clrscr();
aa:
printf("Please enter a value for n & r(r<=n)");
scanf("%d %d",&n,&r);
if(r>n)
{
printf("r is greter than n. ");
goto aa;
}
printf("\n %d! = %ld",n,fact(n));
printf("\n %dP%d = %ld",n,r,fact(n)/fact(n-r));
printf("\n %dC%d = %ld ",n,r,fact(n)/(fact(r)*fact(n-r)));
getch();
}
2.Write a function power (a,b) to calculate the value of a raised to b.
#include<stdio.h>
#include<conio.h>
#include<math.h>
double powr(int x,int y);
void main()
{
int n;
double x,result;
clrscr();
printf("Programme for calculation of 'x' to the power 'n' : ");
printf("\nEnter a value for x : ");
scanf("%lf",&x);
printf("\nEnter a intiger value for n : ");
scanf("%d",&n);
result = powr(x, n);
printf("\n%.2lf\ to the power %d is : %.3lf",x,n,result);
getch();
}
double powr(int x,int y)
{
double c;
c= pow(a,b);
return c;
}
3.Write a function to calculate LCM of two numbers
#include<stdio.h>
#include<conio.h>
int lcm(int x,int y);
int gcd(int p,int q);
void main()
{
int a,b;
clrscr();
scanf("%d%d",&a,&b);
printf("The lcm is %d",lcm(a,b));
getch();
}
int lcm(int x,int y)
{
int m,v;
m=gcd(x,y);
v=(x*y)/m;
return v;
}
int gcd(int p,int q)
{
int c;
do{
c=p%q;
p=q;
q=c;
}
while(c!=0);
return p;
}
else
printf("\n%d is not a leap year.",year);
return 0;
}
6.A prime 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 factor of 35 are 5 and 7
#include<stdio.h>
#include<conio.h>
int primf(int n);
void main()
{
int n,c;
clrscr();
printf("Prime factor : Enter a number :");
scanf("%d",&n);
c=primf(n);
getch();
}
int primf(int n)
{
int i;
for(i=2;n!=1;i++)
{
if((n%i)==0)
{
n=n/i;
printf("%3d",i);
i=1;
}
}
return 0;
}
7.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 prd(float x,int y);
void main()
{
float a,c;
int b;
clrscr();
printf("Please enter a float & a intger number : ");
scanf("%f%d",&a,&b);
c=prd(a,b);
printf("Product = %.3f",c);
getch();
}
float prd(float x,int y)
{
float d;
d =x*y;
return d;
}
8. Write a program which 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>
#include<math.h>
int sum(int p,int q,int r,int s,int v);
float av1(int p,int q,int r,int s,int v);
double dv1(int p1,int q1,int r1,int s1,int v1);
float av;
void main()
{
int a,b,c,d,e,sum1;
long float dv;
clrscr();
printf("Enter five intger number :\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum1=sum(a,b,c,d,e);
av=av1(a,b,c,d,e);
dv=dv1(a,b,c,d,e);
printf("\n\nThe avarage is %.3f",av);
printf("\n\nThe sum is %d",sum1);
printf("\n\nThe dev is %.3lf",dv);
getch();
}
float av1(int p,int q,int r,int s,int v)
{
return (p+q+r+s+v)/5;
}
int sum(int p,int q,int r,int s,int v)
{
return (p+q+r+s+v);
}
double dv1(int p1,int q1,int r1,int s1,int v1)
{
double t,g;
t=pow((av-p1),2)+pow((av-q1),2)+pow((av-r1),2)+pow((av-s1),2)+pow((av-v1),2);
g=pow((t/5),.5);
return g;
}
9. A 5 digit positive integer is entered through the keyboard, write a
function to calculate sum of digits of the 5 digit number.
(i)
(ii)
Using recursion
Without using recursion
Using recursion
#include<stdio.h>
#include<conio.h>
long int Sum(long int digits );
void main()
{
long int num,n,c;
clrscr();
printf("\nEnter 5 digits number : ");
scanf("%ld",&n);
c = Sum(n);
printf("%ld",c);
getch();
}
long int Sum(long int digits )
{
if(digits < 10)
return digits;
else
return digits%10 + Sum(digits/10);
}
#include<stdio.h>
#include<conio.h>
void main()
{
long int n;
clrscr();
printf("\nEnter 5 digits number : ");
scanf("%ld",&n);
long int(long int n);
getch();
}
long int(long int n)
{
int rdgt;
long int num,sum = 0;
num = n;
do
{
rdgt = n%10;
n = n/10;
sum = (sum + rdgt);
}while(n !=0);
printf("\nSum of Digits of the number %ld is %ld ",num,sum);
return 0;
}
11. write a recursive function to obtain the first 25 numbers of a
Fibonacci sequence. In a Fibonacci sequences the sum of two successive
terms given the third term. Following are the first few term of the
Fibonacci sequence:
1
1
2
3
5
8
13
21
34
55. . .
#include<stdio.h>
#include<conio.h>
long int fib(int m);
void main()
{
int i,n;
clrscr();
printf("First 25 Fibonacci sequence are :\n");
for(i=1;i<=25;i++)
{
printf("%ld\n",fib(i));
}
getch();
}
long int fib(int m)
{
if(m<=2)
return 1;
else
return fib(m-1)+fib(m-2);
}
9. A 5 digit positive integer is entered through the keyboard, write a
function to calculate sum of digits of the 5 digit number.
(i)
(ii)
Using recursion
Without using recursion
Using recursion
#include<stdio.h>
#include<conio.h>
long int Sum(long int digits );
void main()
{
long int num,n,c;
clrscr();
printf("\nEnter 5 digits number : ");
scanf("%ld",&n);
c = Sum(n);
printf("%ld",c);
getch();
}
long int Sum(long int digits )
{
if(digits < 10)
return digits;
else
return digits%10 + Sum(digits/10);
}
void main()
{
long int n;
clrscr();
printf("\nEnter 5 digits number : ");
scanf("%ld",&n);
long int(long int n);
getch();
}
long int(long int n)
{
int rdgt;
long int num,sum = 0;
num = n;
do
{
rdgt = n%10;
n = n/10;
sum = (sum + rdgt);
}while(n !=0);
printf("\nSum of Digits of the number %ld is %ld ",num,sum);
return 0;
}