Note: Try to solve these questions on your own before checking the solutions.
Filter:
Sort:
Question 1
Write a C program that prompts the user to input grades for Arithmetic, Algebra, Geometry, and Data Analysis. The program should compute the Total Grade, finding the area with the Lowest Grade.
Example:
Enter Arithmetic grade: 85
Enter Algebra grade: 90
Enter Geometry grade: 78
Enter Data Analysis grade: 88
Total Grade: 341.00
Lowest grade is: 78.00 in Geometry
Solution
#include<stdio.h>intmain(){floatarithmetic,algebra,geometry,analysis;floattotal,min_grade;printf("Enter Arithmetic grade: ");scanf("%f",&arithmetic);printf("Enter Algebra grade: ");scanf("%f",&algebra);printf("Enter Geometry grade: ");scanf("%f",&geometry);printf("Enter Data Analysis grade: ");scanf("%f",&analysis);total=arithmetic+algebra+geometry+analysis;printf("Total Grade: %.2f\n",total);min_grade=arithmetic;if(algebra<min_grade)min_grade=algebra;if(geometry<min_grade)min_grade=geometry;if(analysis<min_grade)min_grade=analysis;printf("Lowest grade is: %.2f in ",min_grade);if(min_grade==arithmetic)printf("Arithmetic\n");elseif(min_grade==algebra)printf("Algebra\n");elseif(min_grade==geometry)printf("Geometry\n");elseprintf("Data Analysis\n");return0;}
#include<iostream>
#include<iomanip>usingnamespacestd;intmain(){floatarithmetic,algebra,geometry,analysis;floattotal,min_grade;cout<<"Enter Arithmetic grade: ";cin>>arithmetic;cout<<"Enter Algebra grade: ";cin>>algebra;cout<<"Enter Geometry grade: ";cin>>geometry;cout<<"Enter Data Analysis grade: ";cin>>analysis;total=arithmetic+algebra+geometry+analysis;cout<<"Total Grade: "<<fixed<<setprecision(2)<<total<<"\n";min_grade=arithmetic;if(algebra<min_grade)min_grade=algebra;if(geometry<min_grade)min_grade=geometry;if(analysis<min_grade)min_grade=analysis;cout<<"Lowest grade is: "<<fixed<<setprecision(2)<<min_grade<<" in ";if(min_grade==arithmetic)cout<<"Arithmetic\n";elseif(min_grade==algebra)cout<<"Algebra\n";elseif(min_grade==geometry)cout<<"Geometry\n";elsecout<<"Data Analysis\n";return0;}
Question 2
Write a function named `LeapYear` that receives a year (integer) and displays whether it is a Leap Year or not.
Example:
Enter year: 2024
2024 is a Leap Year
Solution
#include<stdio.h>voidLeapYear(intyear){if((year%4==0&&year%100!=0)||(year%400==0)){printf("%d is a Leap Year\n",year);}else{printf("%d is not a Leap Year\n",year);}}intmain(){inty;printf("Enter year: ");scanf("%d",&y);LeapYear(y);return0;}
#include<iostream>usingnamespacestd;voidLeapYear(intyear){if((year%4==0&&year%100!=0)||(year%400==0)){cout<<year<<" is a Leap Year\n";}else{cout<<year<<" is not a Leap Year\n";}}intmain(){inty;cout<<"Enter year: ";cin>>y;LeapYear(y);return0;}
Question 3
Write a function named WeatherState that receives temperature (°C) and displays a message based on the status:
Temp < 0: Freezing weather
Temp 0 - 10: Very Cold weather
Temp 10 - 20: Cold weather
Temp 20 - 30: Normal in Temp
Temp 30 - 40: It’s Hot
Temp >= 40: It’s Very Hot
Example:
Enter temperature: 15
Cold weather
Solution
#include<stdio.h>voidWeatherState(inttemp){if(temp<0){printf("Freezing weather\n");}elseif(temp>=0&&temp<10){printf("Very Cold weather\n");}elseif(temp>=10&&temp<20){printf("Cold weather\n");}elseif(temp>=20&&temp<30){printf("Normal in Temp\n");}elseif(temp>=30&&temp<40){printf("It's Hot\n");}else{printf("It's Very Hot\n");}}intmain(){intt;printf("Enter temperature: ");scanf("%d",&t);WeatherState(t);return0;}
#include<iostream>usingnamespacestd;voidWeatherState(inttemp){if(temp<0){cout<<"Freezing weather\n";}elseif(temp>=0&&temp<10){cout<<"Very Cold weather\n";}elseif(temp>=10&&temp<20){cout<<"Cold weather\n";}elseif(temp>=20&&temp<30){cout<<"Normal in Temp\n";}elseif(temp>=30&&temp<40){cout<<"It's Hot\n";}else{cout<<"It's Very Hot\n";}}intmain(){intt;cout<<"Enter temperature: ";cin>>t;WeatherState(t);return0;}
Question 4
Write a function TriangleType that receives 3 integer edges and checks if the triangle is:
Write a function VowelTest that checks if an input character is a Vowel (a, e, i, o, u) or Consonant.
Example:
Enter a character: e
e is a Vowel
Solution
#include<stdio.h>voidVowelTest(charc){if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){printf("%c is a Vowel\n",c);}else{printf("%c is a Consonant\n",c);}}intmain(){charch;printf("Enter a character: ");scanf(" %c",&ch);VowelTest(ch);return0;}
#include<iostream>usingnamespacestd;voidVowelTest(charc){if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){cout<<c<<" is a Vowel\n";}else{cout<<c<<" is a Consonant\n";}}intmain(){charch;cout<<"Enter a character: ";cin>>ch;VowelTest(ch);return0;}
Question 6
Write a function CharTest that checks if an input is:
Trace the following program options to find which expression evaluates to true when $x = 5$ and $y = 10$:
A) x > y B) x == y C) x != y && x > 0 D) x >= 5 && y < 10
Solution
C
C
Question 10
Trace the following program and determine the printed output:
intnumber=0;if(number=0){printf("The number is zero.");}else{printf("The number is not zero.");}
Warning: Note the operator used inside the if statement condition.
Solution
// Uses assignment (=) instead of comparison (==) inside if statement and invalid print;// correct to if (number == 0)
// Uses assignment (=) instead of comparison (==) inside if statement and invalid print;// correct to if (number == 0)
Question 11
Write a function IsValidDate() that takes day, month, and year as integer inputs and returns 1 (true) if the date is valid or 0 (false) if it is invalid. You must account for different month lengths and leap years (February has 29 days in a leap year).
In main(), prompt the user for the day, month, and year, pass them to the function, and print whether the date is valid or invalid.
Example:
Enter day, month, year: 29 2 2023
Date is Invalid
Solution
#include<stdio.h>intIsValidDate(intday,intmonth,intyear);intmain(){intday,month,year;printf("Enter day, month, year: ");scanf("%d %d %d",&day,&month,&year);if(IsValidDate(day,month,year)){printf("Date is Valid\n");}else{printf("Date is Invalid\n");}return0;}intIsValidDate(intday,intmonth,intyear){if(year<=0||month<1||month>12||day<1){return0;}intmaxDays=31;if(month==4||month==6||month==9||month==11){maxDays=30;}elseif(month==2){intisLeap=(year%4==0&&year%100!=0)||(year%400==0);maxDays=isLeap?29:28;}returnday<=maxDays;}
#include<iostream>usingnamespacestd;boolIsValidDate(intday,intmonth,intyear);intmain(){intday,month,year;cout<<"Enter day, month, year: ";cin>>day>>month>>year;if(IsValidDate(day,month,year)){cout<<"Date is Valid"<<endl;}else{cout<<"Date is Invalid"<<endl;}return0;}boolIsValidDate(intday,intmonth,intyear){if(year<=0||month<1||month>12||day<1){returnfalse;}intmaxDays=31;if(month==4||month==6||month==9||month==11){maxDays=30;}elseif(month==2){boolisLeap=(year%4==0&&year%100!=0)||(year%400==0);maxDays=isLeap?29:28;}returnday<=maxDays;}
Question 12
Write a function PlayRPS() that takes the player’s choice as a character ('R', 'P', or 'S') and generates a random computer choice. Print both choices and determine the winner (Player wins, Computer wins, or Tie).
In main(), prompt the user for their choice, call the function, and let the game play out.
Example:
Enter choice (R = Rock, P = Paper, S = Scissors): R
Computer chose: Scissors
Player wins!
Solution
#include<stdio.h>
#include<stdlib.h>
#include<time.h>voidPlayRPS(charplayerChoice);intmain(){srand(time(NULL));charchoice;printf("Enter choice (R = Rock, P = Paper, S = Scissors): ");scanf(" %c",&choice);PlayRPS(choice);return0;}voidPlayRPS(charplayerChoice){intrandNum=rand()%3;charcompChoice;if(randNum==0){compChoice='R';}elseif(randNum==1){compChoice='P';}else{compChoice='S';}if(compChoice=='R'){printf("Computer chose: Rock\n");}elseif(compChoice=='P'){printf("Computer chose: Paper\n");}else{printf("Computer chose: Scissors\n");}if(playerChoice==compChoice){printf("It's a tie!\n");}elseif((playerChoice=='R'&&compChoice=='S')||(playerChoice=='P'&&compChoice=='R')||(playerChoice=='S'&&compChoice=='P')){printf("Player wins!\n");}else{printf("Computer wins!\n");}}
#include<iostream>
#include<cstdlib>
#include<ctime>usingnamespacestd;voidPlayRPS(charplayerChoice);intmain(){srand(time(NULL));charchoice;cout<<"Enter choice (R = Rock, P = Paper, S = Scissors): ";cin>>choice;PlayRPS(choice);return0;}voidPlayRPS(charplayerChoice){intrandNum=rand()%3;charcompChoice;if(randNum==0){compChoice='R';}elseif(randNum==1){compChoice='P';}else{compChoice='S';}if(compChoice=='R'){cout<<"Computer chose: Rock"<<endl;}elseif(compChoice=='P'){cout<<"Computer chose: Paper"<<endl;}else{cout<<"Computer chose: Scissors"<<endl;}if(playerChoice==compChoice){cout<<"It's a tie!"<<endl;}elseif((playerChoice=='R'&&compChoice=='S')||(playerChoice=='P'&&compChoice=='R')||(playerChoice=='S'&&compChoice=='P')){cout<<"Player wins!"<<endl;}else{cout<<"Computer wins!"<<endl;}}
Question 13
Trace the following program and determine the printed output values of $x$, $y$, and $z$:
intx=5;inty=0;intz=10;if(x>3||++y>0){z+=5;}if(x<3&&++y>0){z+=10;}printf("x = %d, y = %d, z = %d\n",x,y,z);
Note: Pay attention to short-circuit evaluation rules for conditional operators || and &&.
Solution
// Output: x = 5, y = 0, z = 15// Explanation: In both if statements, the first condition determines the outcome, so short-circuiting skips evaluating ++y.
// Output: x = 5, y = 0, z = 15// Explanation: In both if statements, the first condition determines the outcome, so short-circuiting skips evaluating ++y.