You are on page 1of 11

Chapter2LetUsCSolutions

letuscsolutions.weebly.com/chapter2.html

Ratethis(104Votes)

[C]Attemptthefollowing:
(a)Ifcostpriceandsellingpriceofanitemisinputthroughthekeyboard,writea
programtodeterminewhetherthesellerhasmadeprofitorincurredloss.Also
determinehowmuchprofithemadeorlossheincurred.
#include<stdio.h>
#include<conio.h>
main()
{
intcp,sp,l,p//cp=costprice,sp=sellingprice,l=loss,p=profit
clrscr()
printf("EnterCostPriceofanitem:RS")
scanf("%d",&cp)
printf("EnterSellingPriceofanitem:RS")
scanf("%d",&sp)
if(cp>sp)//LoopforLoss
{
l=cpsp
printf("YouhavemadeLOSS.YourLossisRS%d",l)
}
elseif(sp>cp)//LoopforProfit
{
p=spcp
printf("YouhavegainPROFIT.YourProfitisRS%d",p)
}
elseif(sp=cp)//LoopfornoLossnoProfit
{
printf("YouhaveneitherLossnorProfit")
}
getch()
}

(b)Anyintegerisinputthroughthekeyboard.Writeaprogramtofindoutwhetherit
isanoddnumberorevennumber.
#include<stdio.h>
#include<conio.h>
main()
{
intnum//num=number
clrscr()
printf("EnteranyintegertoknowweatheritsisEvenorOdd:")
scanf("%d",&num)
if(num%2==0)
{

printf("%disEvennumber",num)
}
else
{
printf("%disOddnumber",num)
}
getch()
}

(c)Anyyearisinputthroughthekeyboard.Writeaprogramtodeterminewhetherthe
yearisaleapyearornot.(Hint:Usethe%(modulus)operator)
#include<stdio.h>
#include<conio.h>
main()
{
intyear
clrscr()
printf("Enteranyyear:")
scanf("%d",&year)
if(year%4==0)
printf("%disaleapyear.",year)
else
printf("%disnotaleapyear.",year)
getch()
}

(d)AccordingtotheGregoriancalendar,itwasMondayonthedate01/01/1900.Ifany
yearisinputthroughthekeyboardwriteaprogramtofindoutwhatisthedayon
1stJanuaryofthisyear.
ComingSoon...

(e)Afivedigitnumberisenteredthroughthekeyboard.Writeaprogramtoobtainthe
reversednumberandtodeterminewhethertheoriginalandreversednumbersare
equalornot.
main()
{
inta,b,c,d,e,f,g,i,j
clrscr()
printf("Enterthefivedigitnumber\n")
scanf("%d",&a)
b=a%10
c=a/10
d=c%10
e=c/10
f=e%10
g=e/10
i=g%10
j=g/10
printf("Thereversenumberis%d%d%d%d%d",b,d,f,i,j)
printf("\nTheoriginalandreversenumberisnotequal")

getch()
}

(f)IftheagesofRam,ShyamandAjayareinputthroughthekeyboard,writea
programtodeterminetheyoungestofthethree.
main()
{
intram,shyam,ajay
clrscr()
printf("EnteragesofRam,Shayam,Ajay\n")
scanf("%d%d%d",&ram,&shyam,&ajay)
if(ram<shyam)
{
if(ram<ajay)
printf("Ramisyounger.")
else
printf("Ajayisyounger")
}
else
{
if(shyam<ajay)
printf("Shayamisyounger")
else
printf("Ajayisyounger")
}
getch()
}

(g)Writeaprogramtocheckwhetheratriangleisvalidornot,whenthethreeangles
ofthetriangleareenteredthroughthekeyboard.Atriangleisvalidifthesumofall
thethreeanglesisequalto180degrees.
#include<stdio.h>
#include<conio.h>
main()
{
floata1,a2,a3,sum//a1=angle1,a2=angle2,a3=angle3,sum=sumofallangles
clrscr()
printf("Enterthreeangleofatriangle:")
scanf("%f%f%f",&a1,&a2,&a3)
sum=a1+a2+a3
if(sum==180)
{
printf("TriangleisValid.")
}
else
{
printf("Triangleisinvalid.")
}
getch()
}

(h)Findtheabsolutevalueofanumberenteredthroughthekeyboard.

SubmittedbyRahulRaina
voidmain()
{
inti,j
printf("Enternumber:")
scanf("%d",&i)
if(i<0)
j=i
else
j=i
printf("%d",j)
getch()
}

(i)Giventhelengthandbreadthofarectangle,writeaprogramtofindwhetherthe
areaoftherectangleisgreaterthanitsperimeter.Forexample,theareaofthe
rectanglewithlength=5andbreadth=4isgreaterthanitsperimeter
#include
#include
main()
{
intl,b,a,p//l=length,b=breadth,a=area,p=perimeterofrectangle
clrscr()
printf("EnterLengthofarectangle:")
scanf("%d",&l)
printf("EnterBreadthofarectangle:")
scanf("%d",&b)
p=2*(l+b)
a=l*b
if(a>p)
{//"\n"isusefornewline
printf("\nYes!Area[%d]isgreaterthatitsperimeter[%d]",a,p)
}
else
{
printf("\nNo!Area[%d]isnotgreaterthatitsperimeter[%d]",a,p)
}
getch()
}

(j)Giventhreepoints(x1,y1),(x2,y2)and(x3,y3),writeaprogramtocheckifall
thethreepointsfallononestraightline.
#include<stdio.h>
#include<conio.h>
main()
{
intx1,y1,x2,y2,x3,y3,m1,m2//m1=slope1,m2=slope2
clrscr()
printf("Entercoordinatesof1stpoint(x1,y1):")
scanf("%d%d",&x1,&y1)
printf("Entercoordinatesof2ndpoint(x2,y2):")
scanf("%d%d",&x2,&y2)

printf("Entercoordinatesof3rdpoint(x3,y3):")
scanf("%d%d",&x3,&y3)
m1=(y2y1)/(x2x1)
m2=(y3y2)/(x3x2)
if(m1==m2)
{
printf("Thegivenpointfallononestraightline.")
}
else
{
printf("Thegivenpointdoesnotfallononestraightline.")
}
getch()
}

(k)Giventhecoordinates(x,y)ofacenterofacircleanditsradius,writeaprogram
whichwilldeterminewhetherapointliesinsidethecircle,onthecircleoroutsidethe
circle.
(Hint:Usesqrt()andpow()functions)
ComingSoon...

(l)Givenapoint(x,y),writeaprogramtofindoutifitliesonthexaxis,yaxisorat
theorigin,viz.(0,0).
#include
#include
main()
{
intx,y
clrscr()
printf("Entercoordinatesofapoint(x,y)=")
scanf("%d%d",&x,&y)
if(x==0&&y!=0)
{
printf("Thepointliesonyaxis.")
}
elseif(y==0&&x!=0)
{
printf("Thepointliesonxaxis.")
}
elseif(x==0&&y==0)
{
printf("Thepointliesonorigin.")
}
getch()
}

[G]Attemptthefollowing:
(a)Anyyearisenteredthroughthekeyboard,writeaprogramtodeterminewhether
theyearisleapornot.Usethelogicaloperators&&and||.

#include<stdio.h>
#include<conio.h>
main()
{
intyear
clrscr()
printf("Enteranyyear:")
scanf("%d",&year)
if((year%4==0&&year%100!=0)||year%400==0)
{
printf("%disaLeapYear.")
}
else
{
printf("%disnotaLeapYear.")
}
getch()
}

(b)Anycharacterisenteredthroughthekeyboard,writeaprogramtodetermine
whetherthecharacterenteredisacapitalletter,asmallcaseletter,adigitoraspecial
symbol.ThefollowingtableshowstherangeofASCIIvaluesforvarious
characters.CharactersASCIIValuesforvariouscharacters.

#include
#include
main()
{
chara
clrscr()
printf("Enteranysingleletter,digitorspecialsymbol:")
scanf("%c",&a)
if(a>=65&&a<=90)
{
printf("YouenteredaCAPITALLETTER.\n")
}
if(a>=97&&a<=122)
{
printf("YouenteredaSMALLLETTER.\n")
}
if(a>=48&&a<=57)
{
printf("YouenteredaDIGIT.\n")
}
if((a>=0&&a<=47)||(a>=58&&a<=64)||(a>=91&&a<=96)||(a>=123&&a<=127))

{
printf("YouenteredanSPECIALSYMBOL.\n")
}
getch()
}

(c)Acertaingradeofsteelisgradedaccordingtothefollowingconditions:
(i)Hardnessmustbegreaterthan50
(ii)Carboncontentmustbelessthan0.7
(iii)Tensilestrengthmustbegreaterthan5600
Thegradesareasfollows:
Gradeis10ifallthreeconditionsaremet
Gradeis9ifconditions(i)and(ii)aremet
Gradeis8ifconditions(ii)and(iii)aremet
Gradeis7ifconditions(i)and(iii)aremet
Gradeis6ifonlyoneconditionismet
Gradeis5ifnoneoftheconditionsaremet
Writeaprogram,whichwillrequiretheusertogivevaluesofhardness,carbon
contentandtensilestrengthofthesteelunderconsiderationandoutputthegradeof
thesteel.
#include<stdio.h>
#include<conio.h>
main()
{
floathard,carbon,tensile
clrscr()
printf("EnterHardnessofsteel:")
scanf("%f",&hard)
printf("EnterCarboncontentofsteel:")
scanf("%f",&carbon)
printf("EnterTrnsilestrengthofsteel:")
scanf("%f",&tensile)
if(hard>50&&carbon<0.7&&tensile>5600)
printf("\nThegradeofsteelis10")
elseif(hard>50&&carbon<0.7)
printf("\nThegradeofsteelis9")
elseif(carbon<0.7&&tensile>5600)
printf("\nThegradeofsteelis8")
elseif(hard>50&&tensile>5600)
printf("\nThegradeofsteelis7")
elseif(hard>50||carbon<0.7||tensile>5600)
printf("\nThegradeofsteelis6")
else
printf("\nThegradeofsteelis5")
getch()
}

(d)Alibrarychargesafineforeverybookreturnedlate.Forfirst5daysthefineis50
paise,for610daysfineisonerupeeandabove10daysfineis5rupees.Ifyoureturn
thebookafter30daysyourmembershipwillbecancelled.Writeaprogramtoaccept
thenumberofdaysthememberislatetoreturnthebookanddisplaythefineorthe
appropriatemessage.
#include<stdio.h>
#include<conio.h>
main()
{
intdays
clrscr()
printf("Enterthenumberofdaysthememberislatetoreturnthebook:")
scanf("%d",&days)
if(days<=5)
printf("\nYoumustpay50paisafine...")//'\n'isusedfornewline
elseif(days>=6&&days<=10)
printf("\nYoumustpay1rupeefine...")
elseif(days>10&&days<30)
printf("\nYoumustpay5rupeesfine...")
elseif(days>=30)
printf("\nYourmembershipiscancelled...")
getch()
}

(e)Ifthethreesidesofatriangleareenteredthroughthekeyboard,writeaprogram
tocheckwhetherthetriangleisvalidornot.Thetriangleisvalidifthesumoftwo
sidesisgreaterthanthelargestofthethreesides.
#include<stdio.h>
#include<conio.h>
main()
{
floats1,s2,s3//s1=side1,s2=side2,s3=side3
clrscr()
printf("Enterthreesidesoftriangleinascendingorder:\n")
scanf("%f%f%f",&s1,&s2,&s3)
if(s1+s2>s3)
printf("\nThetriangleisvalid.")
else
printf("\nThetriangleisinvalid.")
getch()
}

(f)Ifthethreesidesofatriangleareenteredthroughthekeyboard,writeaprogramto
checkwhetherthetriangleisisosceles,equilateral,scaleneorrightangledtriangle.
#include<stdio.h>
#include<conio.h>
main()
{
floata1,a2,a3//a1=angle1,a2=angle2,a3=angle3
clrscr()

printf("Enterthreeanglesofatriangle:\n")
scanf("%f%f%f",&a1,&a2,&a3)
if((a1+a2+a3)==180)
{
if(a1==a2&&a1==a3&&a2==a3)
printf("\nThetriangleisEquilateral.")
elseif(a1==a2||a1==a3||a2==a3)
printf("\nThetriangleisIsosceles.")
elseif(a1==90||a2==90||a3==90)
printf("\nThetriangleisRightAngled.")
elseif(a1!=a2&&a1!=a3&&a2!=a3)
printf("\nThetriangleisScalene")
}
else
printf("Thetriangleisnotvalid")
getch()
}

(g)Inacompany,workerefficiencyisdeterminedonthebasisofthetimerequiredfor
aworkertocompleteaparticularjob.Ifthetimetakenbytheworkerisbetween23
hours,thentheworkerissaidtobehighlyefficient.Ifthetimerequiredbytheworker
isbetween34hours,thentheworkerisorderedtoimprovespeed.Ifthetimetaken
isbetween45hours,theworkerisgiventrainingtoimprovehisspeed,andifthe
timetakenbytheworkerismorethan5hours,thentheworkerhastoleavethe
company.Ifthetimetakenbytheworkerisinputthroughthekeyboard,findthe
efficiencyoftheworker.
#include<stdio.h>
#include<conio.h>
main()
{
floath//h=hours
clrscr()
printf("Enterthetimetakenbytheworker(InHours):")
scanf("%f",&h)
if(h>=2&&h<=3)
printf("\nWorkerishighlyefficient.")
elseif(h>=3&&h<=4)
printf("\nWorkershouldimprovehisspeed.")
elseif(h>=4&&h<=5)
printf("\nWorkershouldtaketrainingtoimprovethespeed.")
elseif(h>=5)
printf("\nWorkerhastoleavethecompany.")
getch()
}

(h)Thepolicyfollowedbyacompanytoprocesscustomerordersisgivenbythe
followingrules:
(a)IfacustomerorderislessthanorequaltothatinstockandhascreditisOK,
supplyhasrequirement.
(b)IfhascreditisnotOKdonotsupply.Sendhimintimation.
(c)IfhascreditisOkbuttheiteminstockislessthanhasorder,supplywhatisin
stock.Intimatetohimdatathebalancewillbeshipped.
WriteaCprogramtoimplementthecompanypolicy.
ComingSoon...

[K]Attemptthefollowing:
(a)Usingconditionaloperatorsdetermine:
(1)Whetherthecharacterenteredthroughthekeyboardisalowercasealphabetor
not.
#include<stdio.h>
#include<conio.h>
main()
{
charch//ch=character
clrscr()
printf("Enteranycharctertofindoutweatheritislowercaseornot:")
scanf("%c",&ch)
(ch>=97&&ch<=122?printf("\nTheinputcharacterislowercase."):printf("\nTheinputcharacterisnotlower
case."))
getch()
}

(2)Whetheracharacterenteredthroughthekeyboardisaspecialsymbolornot.
#include<stdio.h>
#include<conio.h>
main()
{
charch//ch=character
clrscr()
printf("Enteranycharctertofindoutweatheritisspecialsymbolornot:")
scanf("%c",&ch)
((ch>=0&&ch<=47)||(ch>=58&&ch<=64)||(ch>=91&&ch<=96)||(ch>=123&&ch<=127)?printf("\nTheinput
characterisspecialsymbol."):printf("\nTheinputcharacterisnotspecialsymbol."))
getch()
}

(b)Writeaprogramusingconditionaloperatorstodeterminewhetherayearentered
throughthekeyboardisaleapyearornot.
#include<stdio.h>
#include<conio.h>
main()
{

intyear
clrscr()
printf("Enteranyyear:")
scanf("%d",&year)
((year%4==0&&year%100!=0)||(year%400==0)?printf("\n%disaleapyear.",year):printf("\n%disnotaleap
year.",year))
getch()
}

(c)Writeaprogramtofindthegreatestofthethreenumbersenteredthroughthe
keyboardusingconditionaloperators.
#include<stdio.h>
#include<conio.h>
main()
{
intn1,n2,n3//n1=1stnumber,n2=2ndnumber,n3=3rdnumber
clrscr()
printf("Enterthreenumbers:\n")
scanf("%d%d%d",&n1,&n2,&n3)
(n1>n2&&n1>n3?printf("\n%disgreater.",n1):(n2>n3&&n2>n1?printf("\n%disgreater.",n2):printf("\n%dis
greater.",n3)))
getch()
}
BacktoTop

You might also like