32
Let Us C / Chapter 5 (Functions & Pointers) 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) {

Ch-5 Test you skills

Embed Size (px)

DESCRIPTION

C Language

Citation preview

Let Us C / Chapter 5 (Functions & Pointers)

Let Us C / Chapter 5 (Functions & Pointers)

Exercise [D]

(a) Write a function to calculate the factorial value of any integer entered through the keyboard.Solution:

#include#include

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\nFactorial value = %d \n",fact);

}

------------------------------------------------------------------------------------------------------------

(b) Write a function power ( a, b ), to calculate the value of a raised to b.Solution:

#include#include

void main() {

int num1,num2 ;clrscr();

printf("Please enter the value of a: ");scanf("%d",&num1);

printf("\n\nPlease enter the value of b: ");scanf("%d",&num2);

power(num1,num2);

getch();

}

power(int a , int b) {

int c=1,i;

for(i=1;i=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);

}

}

------------------------------------------------------------------------------------------------------------

(d) Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.Solution:

#include#includevoid main() {

int yr;void func();clrscr();

printf("Please enter the year: ");scanf("%d",&yr);

func(yr);

getch();

}void func(int y) {

if((y%4)==0)

printf("\nThis is a LEAP YEAR.\n");

else

printf("\nThis is NOT A LEAP YEAR.\n");

}

------------------------------------------------------------------------------------------------------------

(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.Solution:

#include#include

void main() {

int i,j,k;clrscr();

printf("enter the number: ");

scanf("%d",&j);

printf("\n\nprime factors:");

for(i=2;i