0% found this document useful (0 votes)
24 views

CPP Imp Codes

The document contains 15 code snippets written in C++ that demonstrate the use of conditional statements like if-else and switch case. Each code snippet prompts the user to input a value and then uses a conditional statement to output a determined response based on the input value. The code snippets cover examples like checking if numbers are equal, even/odd, positive/negative, determining the greatest of three numbers, calculating grades and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

CPP Imp Codes

The document contains 15 code snippets written in C++ that demonstrate the use of conditional statements like if-else and switch case. Each code snippet prompts the user to input a value and then uses a conditional statement to output a determined response based on the input value. The code snippets cover examples like checking if numbers are equal, even/odd, positive/negative, determining the greatest of three numbers, calculating grades and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Q1

#include <iostream>

Using namespace std;

Int main() {

Int num1, num2;

Cout << “Enter two integers: ;

Cin >> num1 >> num2;

If (num1 == num2) {

Cout << “Number1 and Number2 are equal”;

} else {

Cout << “Number1 and Number2 are not equal”;

Return 0;

Output: Number1 and Number2 are equal

Q2

#include <iostream>

Using namespace std;

Int main() {

Int num;

Cout << “Enter a number: “;

Cin >> num;

If (num % 2 == 0) {
Cout << num << “ is an even integer”;

} else {

Cout << num << “ is an odd integer”;

Return 0;

Output: 15 is an odd integer

Q3

#include <iostream>

Using namespace std;

Int main() {

Int num;

Cout << “Enter a number: “;

Cin >> num;

If (num > 0) {

Cout << num << “ is a positive number”;

} else if (num < 0) {

Cout << num << “ is a negative number”;

} else {

Cout << “Number is zero”;

Return 0;

Output: 15 is a positive number


Q4

#include <iostream>

Using namespace std;

Int main() {

Int year;

Cout << “Enter a year: “;

Cin >> year;

If ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

Cout << year << “ is a leap year.”;

} else {

Cout << year << “ is not a leap year.”;

Return 0;

Output: 2016 is a leap year.

Q5

#include <iostream>

Using namespace std;

Int main() {

Int age;

Cout << “Enter your age: “;

Cin >> age;


If (age >= 18) {

Cout << “Congratulation! You are eligible for casting your vote.”;

} else {

Cout << “Sorry, you are not eligible to vote.”;

Return 0;

Output: Congratulation! You are eligible for casting your vote.

Q6

#include <iostream>

Using namespace std;

Int main() {

Int height;

Cout << “Enter height in centimeters: “;

Cin >> height;

If (height < 150) {

Cout << “The person is Dwarf.”;

} else if (height >= 150 && height < 165) {

Cout << “The person is Average height.”;

} else {

Cout << “The person is Tall.”;

Return 0;

}
Output: The person is Dwarf.

Q7

#include <iostream>

Using namespace std;

Int main() {

Int num1, num2, num3;

Cout << “Enter three numbers: “;

Cin >> num1 >> num2 >> num3;

If (num1 >= num2 && num1 >= num3) {

Cout << “1st Number = “ << num1 << “, 2nd Number = “ << num2 << “,\n”

<< “3rd Number = “ << num3 << “ The 1st Number is the greatest among three”;

} else if (num2 >= num1 && num2 >= num3) {

Cout << “1st Number = “ << num1 << “, 2nd Number = “ << num2 << “,\n”

<< “3rd Number = “ << num3 << “ The 2nd Number is the greatest among three”;

} else {

Cout << “1st Number = “ << num1 << “, 2nd Number = “ << num2 << “,\n”

<< “3rd Number = “ << num3 << “ The 3rd Number is the greatest among three”;

Return 0;

Output: 3rd Number = 52 The 3rd Number is the greatest among three

Q8

#include <iostream>

Using namespace std;


Int main() {

Int mathMarks, phyMarks, chemMarks;

Cout << “Input the marks obtained in Physics: “;

Cin >> phyMarks;

Cout << “Input the marks obtained in Chemistry: “;

Cin >> chemMarks;

Cout << “Input the marks obtained in Mathematics: “;

Cin >> mathMarks;

Int totalMarks = mathMarks + phyMarks + chemMarks;

Int mathPhyTotal = mathMarks + phyMarks;

If ((mathMarks >= 65 && phyMarks >= 55 && chemMarks >= 50 && totalMarks >= 190) ||

(mathPhyTotal >= 140)) {

Cout << “The candidate is eligible for admission.”;

} else {

Cout << “The candidate is not eligible for admission.”;

Return 0;

Output: The candidate is not eligible for admission.

Q9

#include <iostream>

Using namespace std;

Int main() {
Int rollNo, physicsMarks, chemistryMarks, computerMarks;

String name;

Cout << “Input the Roll Number of the student: “;

Cin >> rollNo;

Cout << “Input the Name of the Student: “;

Cin >> name;

Cout << “Input the marks of Physics, Chemistry, and Computer Application: “;

Cin >> physicsMarks >> chemistryMarks >> computerMarks;

Int totalMarks = physicsMarks + chemistryMarks + computerMarks;

Float percentage = (totalMarks / 300.0) * 100;

Cout << “Roll No: “ << rollNo << “\n”

<< “Name of Student: “ << name << “\n”

<< “Marks in Physics: “ << physicsMarks << “\n”

<< “Marks in Chemistry: “ << chemistryMarks << “\n”

<< “Marks in Computer Application: “ << computerMarks << “\n”

<< “Total Marks = “ << totalMarks << “\n”

<< “Percentage = “ << percentage << “%\n”;

If (percentage >= 60) {

Cout << “Division = First”;

} else if (percentage >= 50) {

Cout << “Division = Second”;

} else if (percentage >= 40) {

Cout << “Division = Third”;

} else {

Cout << “Division = Fail”;

}
Return 0;

Output: Roll No: 784

Name of Student: James

Marks in Physics: 70

Marks in Chemistry: 80

Marks in Computer Application: 90

Total Marks = 240

Percentage = 80.00

Division = First

Q10

#include <iostream>

Using namespace std;

Int main() {

Int temperature;

Cout << “Enter temperature in centigrade: “;

Cin >> temperature;

If (temperature < 0) {

Cout << “Freezing weather”;

} else if (temperature >= 0 && temperature <= 10) {

Cout << “Very Cold weather”;

} else if (temperature > 10 && temperature <= 20) {

Cout << “Cold weather”;

} else if (temperature > 20 && temperature <= 30) {

Cout << “Normal in Temp”;


} else if (temperature > 30 && temperature <= 40) {

Cout << “Its Hot”;

} else {

Cout << “Its Very Hot”;

Return 0;

Output: Its Very Hot.

Q11

#include <iostream>

Using namespace std;

Int main() {

Char character;

Cout << “Enter a character: “;

Cin >> character;

If (isalpha(character)) {

Cout << “This is an alphabet.”;

} else if (isdigit(character)) {

Cout << “This is a digit.”;

} else {

Cout << “This is a special character.”;

Return 0;

}
Output: This is a special character.

Q12

Output: The alphabet is a consonant.

Q13

#include <iostream>

Using namespace std;

Int main() {

Char grade;

Cout << “Enter a grade: “;

Cin >> grade;

Switch (grade) {

Case ‘E’:

Cout << “Excellent”;

Break;

Case ‘V’:

Cout << “Very Good”;

Break;

Case ‘G’:

Cout << “Good”;

Break;

Case ‘A’:

Cout << “Average”;

Break;

Case ‘F’:
Cout << “Fail”;

Break;

Default:

Cout << “Invalid grade”;

Return 0;

Output: Enter a grade: E

Excellent

Q14

#include <iostream>

Using namespace std;

Int main() {

Int dayNumber;

Cout << “Enter a day number (1-7): “;

Cin >> dayNumber;

Switch (dayNumber) {

Case 1:

Cout << “Monday”;

Break;

Case 2:

Cout << “Tuesday”;

Break;

Case 3:

Cout << “Wednesday”;


Break;

Case 4:

Cout << “Thursday”;

Break;

Case 5:

Cout << “Friday”;

Break;

Case 6:

Cout << “Saturday”;

Break;

Case 7:

Cout << “Sunday”;

Break;

Default:

Cout << “Invalid day number”;

Return 0;

Output: Enter a day number (1-7): 4

Thursday

Q15

#include <iostream>

Using namespace std;

Int main() {

Int digit;

Cout << “Enter a digit (0-9): “;


Cin >> digit;

Switch (digit) {

Case 0:

Cout << “Zero”;

Break;

Case 1:

Cout << “One”;

Break;

Case 2:

Cout << “Two”;

Break;

Case 3:

Cout << “Three”;

Break;

Case 4:

Cout << “Four”;

Break;

Case 5:

Cout << “Five”;

Break;

Case 6:

Cout << “Six”;

Break;

Case 7:

Cout << “Seven”;

Break;

Case 8:

Cout << “Eight”;


Break;

Case 9:

Cout << “Nine”;

Break;

Default:

Cout << “Invalid digit”;

Return 0;

Output: Enter a digit (0-9): 4

Four

Send a message

You might also like