Sei sulla pagina 1di 21

PROGRAMMING WITH C-LANGUGE - ASSIGNMENT 3

Instructions: You have to implement all the given programming objects using user-defined functions. 1. Write a program to calculate the volume of a box.
#include<stdio.h> #include<conio.h>

//This program calculates the volume of a box // Function Declaration float volume(float)

void main (void) ! clrscr()

float l"res printf(#This program calculates the volume of a s$uare box.%n#) printf(#&lease enter length of one side of given box'%n#) scanf(#(f#")l) res*volume(l) //function call

printf(#The volume of given box +ith e$ual sides is (g#"res)

getch() ,

//Function definition

float volume(float l) ! float res res*l - l - l return res ,

2. Write a program to evaluate the expression: a. A + b b. A B c. A/B d. emainder of A/B !se integer" float or double" exponent formats and variables for printing #ith different t$pes of modifiers.

%include&stdio.h' %include&conio.h'

//(unction declarations void fsum )void*+ void fsub )void*+ void fdiv )void*+ void dsum )void*+ void dsub )void*+

void ddiv )void*+ void isum )void*+ void isub )void*+ void idiv )void*+ void irem )void*+

void main )void* , clrscr)*+

char ch+ printf)-.his program performs the basic calculations on integer float and double t$pe of t#o variables a and b./n-*+ printf)-0lease enter data t$pe: f for float" d for double" i for integer./n-*+ ch1getche)*+ if ) ch112f2* , fsum)*+ fsub)*+ fdiv)*+ printf)-/n.he modulus operator cannot be applied on float values./n-*+ 3 if )ch112d2* , dsum)*+ dsub)*+ ddiv)*+ printf)-/n.he modulus operator cannot be applied on double data t$pe./n-*+ 3 if )ch112i2*

, isum)*+ isub)*+ idiv)*+ irem)*+ 3

getch)*+ 3

//function definitions void fsum )void* , float a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5f 5f-"6a"6b*+ res1 a+b+ printf)-.he sum of a and b is 5g-"res*+ 3

void fsub )void* , float a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5f 5f-"6a"6b*+ res1 a7b+ printf)-.he difference of a and b is 5f-"res*+ 3

void fdiv )void*

, float a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5f 5f-"6a"6b*+ res1 a/b+ printf)-A divided b$ b is 5g-"res*+ 3

void dsum )void* , double a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5lf 5lf-"6a"6b*+ res1 a+b+ printf)-.he sum of a and b is 5lf-"res*+ 3

void dsub )void* , double a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5lf 5lf-"6a"6b*+ res1 a7b+ printf)-.he difference of a and b is 5lf-"res*+ 3

void ddiv )void* , double a"b"res+ printf)-/n4nter values of a and b:/n-*+

scanf)-5lf 5lf-"6a"6b*+ res1 a/b+ printf)-A divided b$ b is 5lf-"res*+ 3

void isum )void* , int a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5d 5d-"6a"6b*+ res1 a+b+ printf)-.he sum of a and b is 5d-"res*+ 3

void isub )void* , int a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5d 5d-"6a"6b*+ res1 a7b+ printf)-.he difference of a and b is 5d-"res*+ 3

void idiv )void* , int a"b+ float res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5d 5d-"6a"6b*+ res1 a/b+

printf)-A divided b$ b is 5g-"res*+ 3

void irem )void* , int a"b"res+ printf)-/n4nter values of a and b:/n-*+ scanf)-5d 5d-"6a"6b*+ res1 a5b+ printf)- emainder of a/b is 5d-"res*+ 3

8. Write a program to evaluate a function: f)x* 1 ax2 + b if b '1 2 f)x* 1 ax + bx if b & 2

%include&stdio.h' %include&conio.h' //function declaration void evaluate )*+

void main )void* , clrscr)*+ printf)-.his progra evaluates a function depending on the value of b./n-*+ evaluate)*+ //function call getch)*+ 3

//function definition void evaluate)*

, float a"x"b"func+ printf)-4nter value of a./n-*+ scanf)-5f-"6a*+ printf)-4nter value of b./n-*+ scanf)-5f-"6b*+ printf)-4nter value of x./n-*+ scanf)-5f-"6x*+ if )b'12* , func1))a9)x9x** + b*+ printf)-:ince b'12 : f)x* 1 ax;2 + b 1 5g-"func*+ 3 if )b&2* , func1 ))a9x* + )b9x**+ printf)-:inceb&2 : f)x* 1 ax + bx 1 5g-"func*+ 3 3

<. Write a program #hich as=s for the student>s age. ?n the basis of the given age" it decides #hether the student can appear for the contest or not. @f the age is bet#een A and 1A then it displa$s the message Bstudent can appear for contestC" other#ise Bstudent cannot appear for contestC.
%include &stdio.h' %include &conio.h'

//.his program tests eligibilit$ for contest entr$. //(unction declaration void eligibilit$Dchec=)int*+ void main )void* , clrscr)*+ int age+ printf)-.his program decides #hether a student is eligible for contest or not./n-*+ printf)-/n0lease enter the student2s age:/n-*+ scanf)-5d-"6age*+ eligibilit$Dchec=)age*+ getch)*+ 3 void eligibilit$Dchec= )int age* , if )age'1 A 66 age&1 1A* printf)-/n.he student can appear for contest.-*+ else printf)-/n.he student can not appear for contest.-*+ 3

A. Write a program to calculate discount to be given to the customer on purchasing of items. .a=e the price and number of products purchased b$ the customer as input. @f the price is more than AEEE and the number of products purchased is more than A" then the discount is paid as A5" other#ise the discount given on product is Fero.
%include &stdio.h' %include &conio.h'

//.his program calculates discount on item purchase. //(unction declaration void discount)float"int*+ void main )void* , clrscr)*+ float result"price+ int number+ printf)-.his progrm calculates discount to be given to customers./n-*+ printf)-/n4nter the total price of products purchased:/n-*+ scanf)-5f-"6price*+ printf)-/n4nter the number of products purchased:/n-*+ scanf)-5d-"6number*+ discount)price"number*+ //function call

getch)*+ 3 //(unction definition void discount) float price" int number* , float discount+ if )price'AEEE 66 number 'A* , discount 1 E.EA 9 price+ printf)-/n.he discount on purchased items is :. 5g-"discount*+ 3 else printf)-/n.here is no discount on this purchase.-*+ 3

G. Write a program to find #hether a Huadratic eHuation has real or imaginar$ roots. ax2 + bx + c 1 E if the roots are real then displa$ the results.
%include &stdio.h' %include &conio.h' %include &math.h'

//.his program determines the nature of Huadratic roots.

//(unction declaration void HuadDroots)void*+ void main )void* , clrscr)*+ printf)-.his program determines the nature of roots of Huadratic eHuation of the form:/nax;2 + bx + c 1 E-*+ HuadDroots)*+ getch)*+ 3 //(unction definition void HuadDroots )void* , float a"b"c"discriminant"x1"x2+ printf)-/n0lease enter values for eHuation such that b ' a 6 c./n-*+ printf)-/n4nter the value of a:/n-*+ scanf)-5f-"6a*+ printf)-Io# enter the value of b:/n-*+ scanf)-5f-"6b*+ printf)-Io# enter the value of c:/n-*+ scanf)-5f-"6c*+ discriminant1)b9b*7)<9a9c*+ x11)7b + )sHrt)discriminant** */ )29a*+ x21)7b 7 )sHrt)discriminant** */ )29a*+ //(unction call

if )discriminant 11 E* printf)-.he eHuation has t#o eHual roots and their values are:/nx115g x215g-"x1"x2*+ if )discriminant ' E* printf)-.he eHuation has t#o distinct real roots and their values are:/nx115g x21 5g-"x1"x2*+ 3

J. Write a program to find #hether the $ear is a leap $ear. .a=e the $ear as input.

%include &stdio.h' %include &conio.h' //.his program determines if given $ear is leap $ear or not. //(unction declaration void ifDleap)long*+ void main )void* , clrscr)*+ long $ear+ printf)-@n modern sense" the first leap $ear #as 1JA2./n-*+ printf)-4nter an$ $ear after 1JA2 to determine if it is a leap $ear or not./n-*+ scanf)-5ld-"6$ear*+ ifDleap)$ear*+ getch)*+ 3 void ifDleap) long $ear* , int dig1"dig2+ //$ears #ith double Fero at end)centur$ $ears* have to be long n$ear+ //divided b$ <EE to chec= if its a leap $ear or not dig1 1 $ear51E+ n$ear 1 $ear/1E+ dig2 1 n$ear51E+ if )dig111E 66 dig211E* , if )$ear 5 <EE 11 E* printf)-/n.he $ear 5ld is a leap $ear-"$ear*+ else printf )-/n.he given $ear is not a leap $ear-*+ 3 else , if )$ear 5 < 11 E* //$ears #ith no Fero at end are divided b$ < printf )-/n.he $ear 5d is a leap $ear-"$ear*+ else printf)-/n.he given $ear is not a leap $ear-*+ 3 3

K. Write a program to find the value of L for a particular value of M @f M 1 1 then L 1 ax + b @f M 1 2 then L 1 ax2 + b2 @f M 1 8 then L 1 7ax @f M 1 < then L 1 a + x .he values of a" x" b" = are supplied through =e$board.
%include &stdio.h' %include &conio.h'

//.his program finds the value of L for a particular value of M. //(unction declaration float $Dvalue)int*+

void main )void* , clrscr)*+ int =1E+ float $+ printf)-.his program finds the value of L for a particular value of =./n-*+ printf)-4nter the value of = in range of 1 to <:/n-*+ scanf)-5d-"6=*+ $ 1 $Dvalue)=*+ //function call printf)-/n/n(or M15d : L15g-"="$*+ getch)*+ 3

float $Dvalue)int =* , float a"b"x"$+

//(unction definition

printf)-4nter the value of a:/n-*+ scanf)-5f-"6a*+ printf)-4nter the value of b:/n-*+ scanf)-5f-"6b*+ printf)-4nter the value of x:/n-*+ scanf)-5f-"6x*+ if )=111* , $ 1 )a9x* + b+ return $+ 3 if )=112* , $ 1 )a9x9x* + )b9b*+ return $+ 3 if )=118* , $ 1 )7a9x*+ return $+ 3 if )=11<* , $ 1 a + x+ return $ + 3 3

N. Write the above program using Os#itch> statement.


%include&stdio.h' %include&conio.h'

//.his program finds value of $ for a particular value of =. //function declaration void $value)void*+

void main )void*

, clrscr)*+ printf)-.his program calculates the value of $ for a particular value of =./n-*+ $value)*+ //function call

getch)*+ 3 //function definition void $value)void* , int =+ float a"b"x"$+ printf)-4nter value of = in range of 1 to <./n-*+ scanf)-5d-"6=*+ printf)-4nter value of a:/n-*+ scanf)-5f-"6a*+ printf)-4nter value of b:/n-*+ scanf)-5f-"6b*+ printf)-4nter value of x:/n-*+ scanf)-5f-"6x*+ s#itch)=* , case 1: $1a9x+b+ printf)-(or = 1 1 : L 1 5g-"$*+ brea=+ case 2: $1a9x9x + b9b+ printf)-(or = 1 2 : L 1 5g-"$*+ brea=+ case 8: $17a9x+ printf)-(or = 1 8 : L 1 5g-"$*+ brea=+ case <: $1 a+x+ printf)-(or = 1 < : L 1 5g-"$*+ brea=+ default: printf)-4nter correct value of =-*+ brea=+ 3 3

1E. Write a program to calculate the sum of even numbers upto 1EE.

%include &stdio.h' %include &conio.h' //.his program calculates the sum of even numbers upto 1EE. //(unction declaration int evenDsum)void*+

void main )void* , clrscr)*+ int sum+ printf)-.his program calculates the sum of even numbers upto 1EE./n- *+ sum1 evenDsum)*+ printf)-.he sum of even numbers upto 1EE is 5d-"sum*+ getch)*+ 3

//function definition int evenDsum )void* , int n11"sum1E+ #hile )n&11EE* , if )n52 11 E* sum 1 sum + n+ n+++ 3 return sum+ 3

11. Write a program to generate (ibonacci series. (ibonacci series is the one in #hich ever$ number is generated b$ sum of t#o previous terms. .he first t#o terms are generating terms. 4xample: 1 1 2 8 A K 18 21PPPPP. I terms .a=e t#o initial values of the series as input from user and generate the remaining series till 1Eth term
%include &stdio.h' %include &conio.h'

//.his program generates the fibonacci series.

//(unction declaration void fiboDseries)void*+

void main )void* , clrscr)*+

printf)-.his program generates the fibonacci series for given first t#o terms./n-*+ fiboDseries)*+

getch)*+ 3 //function definition

void fiboDseries)void* , int n1"n2"n8+ printf)-0lease enter the first term of the series:/n-*+ scanf)-5d-"6n1*+ printf)-0lease enter the second term of the series:/n-*+ scanf)-5d-"6n2*+ printf)-5d 5d -"n1"n2*+ for)int = 11+ =&1K+ =++* , n81 n1+n2+ printf)-5d -"n8*+ n11n2+ n21n8+ 3 3

Potrebbero piacerti anche