2.
DevelopaCprogramthattakesastudent's marksasinputand
displaystheirgradebasedonthefollowingcriteria:90andabove:
GradeA75to89:GradeB60to74:GradeC50to59:GradeDBelow
50:GradeFChooseasuitablecontrolstructuretoimplementthislogic
efficiently.
PROGRAMCODE:
#include<stdio.h>
voidmain()
{
intmarks;
printf("Enterthestudent's marks(0to100):");
scanf("%d",&marks);
if(marks<0||marks>100)
printf("\nInvalidmarks!Pleaseenteravaluebetween0and100.");
elseif (marks>=90)
printf("\nGrade:A");
elseif (marks>=75)
printf("\nGrade:B");
elseif (marks>=60)
printf("\nGrade:C");
elseif (marks>=50)
printf("\nGrade:D");
else
printf("Grrade:F\n");
}
OUTPUT:
Enterthestudent's marks(0to100):90
Grade:A
[Link]
PANNumber,AADHAR_Number,APAAR_Id,DrivingLicense,
[Link]
theinput,[Link]
appropriatecontrolstructuretohandlemultiplepossibleIDmatches.
AssumeallUniqueidentificationareofintegertype.
PROGRAMCODE:
#include<stdio.h>
intmain()
{
intchoice,id;
printf("\n------KYCVerificationSystem------");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("Enteryourchoice(1-5):");
scanf("%d",&choice);
switch(choice)
{
case1:
printf("\nPANVerified!!");
break;
case2:
printf("\nAADHARVerified!!");
break;
case3:
printf("\nAPAARVerified!!");
break;
case4:
printf("\nDrivingLicenseVerified!!");
break;
case5:
printf("\nPassportVerified!!");
break;
default:
printf("\nNotVerified!!");
}
return0;
}
OUTPUT:
------KYCVerificationSystem------
[Link]
[Link]
[Link]
[Link]
[Link](1-5):2
AADHARVerified!!
[Link]
[Link]
displaytherootsbasedonthegivencoefficients.
PROGRAMCODE:
#include<stdio.h>
#include<math.h>
intmain()
{
floata,b,c,root1,root2,disc,real,imag;
printf("Entercoefficientsofquadratic equation(a,b,c):");
scanf("%f%f%f",&a,&b,&c);
if(a==0&&b==0)
printf("\nInvalidCoefficients!");
elseif (a==0)
{
root1=-c/b;
printf("\nLinearEquationwithroot:%.2f",root1);
}
else
{
disc=b*b-4*a*c;
printf("\nDiscriminant=%.2f",disc);
if(disc>0)
{
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("\nTwodistinctrealroots:%.2fand%.2f",root1,
root2);
}
elseif(disc==0)
{
root1=-b/(2*a);
printf("\nOnerealroot:%.2f",root1);
}
else
{
real=-b/(2*a);
imag=sqrt(-disc)/(2*a);
printf("\nComplexroots:%.2f+%.2fiand%.2f-%.2fi",
real,imag,real,imag);
}
}
return0;
}
OUTPUT:
Entercoefficientsofquadratic equation(a,b,c):1-32
Discriminant=1.00
Twodistinctrealroots:2.00and1.00
[Link]
real-time,butthehardwaredoesn'tsupportbuilt-intrigonometric
[Link](x)
usingaseriesexpansionmethodforimprovedperformance.
PROGRAMCODE:
#include<stdio.h>
#include<math.h>
#definePI3.14
voidmain()
{
floatsum,term,x,nume;
intdeg;
inti=2,fact=1;
printf("Enterangleindegrees:");
scanf("%d",°);
x=(deg*PI)/180;
sum=x;
nume=x;
do
{
fact=fact*i*(i+1);
nume=-nume*x*x;
term=nume/fact;
sum+=term;
i+=2;
}while(fabs(term)>=0.0001);
printf("\nApproximatedsin(%d)=%.2f",deg,sum);
}
OUTPUT:
Enterangle in degrees:45
Approximatedsin(45)=0.71
[Link]
[Link]
[Link],
display:"Keyword'' foundinthecoursedescription."Otherwise,
display:"Keyword'' notfoundinthecoursedescription.
#include<stdio.h>
#include<string.h>
#defineMAX100
intmain()
{
chardescription[MAX];
charkeyword[100];
printf("Enterthecoursedescription:");
fgets(description,MAX,stdin);
printf("Enterthekeywordtosearch:");
scanf("%s",keyword);
if(strstr(description,keyword))
printf("\nKeyword'%s'foundinthecoursedescription.",keyword);
else
printf("\nKeyword'%s'notfoundinthecoursedescription.",keyword);
return0;
}
OUTPUT:
Enterthecoursedescription:C is aprogramminglanguage
Enterthekeywordtosearch:pro
Keyword'pro'foundinthecoursedescription.
[Link].
Useafunctiontocheckifthestudenthaspassed(minimum40marks
ineachsubject).Displaytheaverageandwhetherthestudentpassed
orfailed.
PROGRAMCODE:
#include<stdio.h>
intPassed(intm1,intm2,intm3)
{
if(m1>=40&&m2>=40&&m3>=40)
return1;
elsereturn0;
}
voidmain()
{
intsubject1,subject2,subject3;
floataverage;
printf("EntermarksforSubject1:");
scanf("%d",&subject1);
printf("EntermarksforSubject2:");
scanf("%d",&subject2);
printf("EntermarksforSubject3:");
scanf("%d",&subject3);
average=(subject1+subject2+subject3)/3.0;
printf("\nAverageMarks:%.2f",average);
if(Passed(subject1,subject2,subject3))
printf("\nResult:PASS");
else
printf("\nResult:FAIL");
}
OUTPUT:
EntermarksforSubject1:90
EntermarksforSubject2:89
EntermarksforSubject3:93
AverageMarks:90.67
Result:PASS
[Link],twoaccountbalancesneedtobeswapped
[Link]
[Link]
balancesbeforeandafterswapping.
PROGRAMCODE:
#include<stdio.h>
voidswap(int*a,int*b)
{
inttemp=*a;
*a=*b;
*b=temp;
}
voidmain()
{
intbalance1,balance2;
printf("EnterbalanceforAccount1:");
scanf("%d",&balance1);
printf("EnterbalanceforAccount2:");
scanf("%d",&balance2);
printf("\nBeforeSwapping:\n");
printf("\nAccount1Balance:%d",balance1);
printf("\nAccount2Balance:%d",balance2);
swap(&balance1,&balance2);
printf("\nAfterSwapping:\n");
printf("\nAccount1Balance:%d",balance1);
printf("\nAccount2Balance:%d",balance2);
}
OUTPUT:
EnterbalanceforAccount1:50000
EnterbalanceforAccount2:2000
BeforeSwapping:
Account1Balance:50000
Account2Balance:2000
AfterSwapping:
Account1Balance:2000
Account2Balance:50000
PART:B
[Link]
[Link]
[Link]
whetherabookwithaspecificBookIDisavailableintheshelfusing
binarysearch.
PROGRAMCODE:
#include<stdio.h>
intmain()
{
intn,i,key,low,high,mid,found=0;
printf("Enternumberofbooks:");
scanf("%d",&n);
intbooks[n];
printf("Enter%dBookIDsinascendingorder:",n);
for(i=0;i<n;i++)
scanf("%d",&books[i]);
printf("EnterBookIDtosearch:");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(books[mid]==key)
{
found=1;
break;
}
elseif(books[mid]<key)
low=mid +1;
else
high=mid-1;
}
if(found)
printf("Bookisavailable.\n");
else
printf("Bookisnotavailable.\n");
return0;
}
OUTPUT:
Enternumberofbooks:5
Enter5BookIDsin ascendingorder:101102103104105
EnterBookIDtosearch:103
Bookisavailable.
3.Asportsteacherhasrecordedthescoresofstudentsina100-meter
[Link],theteacherwantsthescores
arrangedindescendingorder(fromhighesttolowest).Thetaskisto
developaCprogramtosortthescoresusingBubbleSort.
PROGRAMCODE:
#include<stdio.h>
intmain()
{
intn,i,j, temp;
printf("Enternumberofstudents:");
scanf("%d",&n);
intscores[n];
printf("\nEnterthescores:");
for(i=0;i<n;i++)
scanf("%d",&scores[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(scores[j]<scores[j+1])
{
temp=scores[j];
scores[j] =scores[j+1];
scores[j+1]=temp;
}
}
}
printf("\nScoresin descendingorder:");
for(i=0;i<n;i++)
printf("%d",scores[i]);
return0;
}
OUTPUT:
Enternumberofstudents:5
Enterthescores:90 98 78 56 88
Scoresin descendingorder:98 90 88 78 56
[Link]
[Link],thesystem
[Link],thesystemmustcheckthe
[Link]
implementtheseoperationsinCwithoutusingbuilt-instring
functions.
PROGRAMCODE:
#include<stdio.h>
intmain()
{
charfirst[50],last[50],full[100];
inti=0,j=0,length=0;
printf("Enterfirstname:");
scanf("%s",first);
printf("Enterlastname:");
scanf("%s",last);
while(first[i]!='\0')
{
full[length++]=first[i++];
}
full[length++]='';
while(last[j] !='\0')
{
full[length++]=last[j++];
}
full[length]='\0';
printf("FullName:%s\n",full);
printf("Length:%d\n",length);
if(length>20)
printf("Nametoolongforscreen.\n");
else
printf("Namefitsthescreen.\n");
return0;
}
OUTPUT:
Enterfirstname:Sukanya
Adya
Enterlastname:Nyamagoudra
Patil
FullName:Sukanya
Adya PatilNyamagoudra
Length:13
10
Namefitsthescreen.
[Link]
[Link],thesystemsimulatesa
swapofthevalues(preview)withoutactuallychangingtheoriginal
[Link],itupdatestheactualvalues(permanentswap).
ThetaskistoimplementbothbehaviorsusingCallbyValueandCallby
ReferenceinC.
PROGRAMCODE:
#include<stdio.h>
voidswapByValue(inta,intb)
{
inttemp=a;
a=b;
b=temp;
printf("\nPreviewSwap(ByValue):%d%d",a,b);
}
voidswapByReference(int*a,int*b)
{
inttemp=*a;
*a=*b;
*b=temp;
printf("\nActualSwap(ByReference):%d%d",*a,*b);
}
intmain()
{
intx,y;
printf("Entertwocurrencyvalues:");
scanf("%d%d",&x,&y);
swapByValue(x,y);
printf("\nAfterCall byValue:%d%d",x,y);
swapByReference(&x,&y);
printf("\nAfterCall byReference:%d%d",x,y);
return0;
}
OUTPUT:
Entertwocurrencyvalues:1020
PreviewSwap(ByValue):2010
AfterCall byValue:1020
ActualSwap(ByReference):2010
AfterCall byReference:2010
[Link],
includingtitle,author,[Link]
structurethatcanholdthesedetailsanddevelopaCprogramto
displaythelistofbooksentered.
PROGRAMCODE:
#include<stdio.h>
structBook
{
chartitle[50];
charauthor[50];
intyear;
};
intmain()
{
intn,i;
printf("Enternumberofbooks:");
scanf("%d",&n);
structBookbooklist[n];
for(i=0;i<n;i++)
{
printf("\nEntertitle,author,andyearforbook%d:",i+1);
scanf("%s%s%d",booklist[i].title,booklist[i].author,&booklist[i].year);
}
printf("\nLibraryBookDetails:\n");
for(i=0;i<n;i++)
{
printf("\nBook%d:%sby%s(%d)",i+1,booklist[i].title,
booklist[i].author,booklist[i].year);
}
return0;
}
OUTPUT:
Enternumberofbooks:1
Entertitle,author,andyearforbook1:CDennis1978
LibraryBookDetails:
Book1:CbyDennis (1978)