Flow of Control [Set – 1]
#include <iostream> using namespace std; int main() { int num; cout << "Enter an integer: "; cin >> num; if (num % 2 == 0) cout << "The number is Even." << endl; else cout << "The number is Odd." << endl; return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; cout << "Absolute value: " << abs(num) << endl; return 0; }
#include <iostream> using namespace std; int main() { int quantity; float price, total; cout << "Enter quantity: "; cin >> quantity; cout << "Enter price per item: "; cin >> price; total = quantity * price; if (total > 5000) total *= 0.9; // Apply 10% discount cout << "Total expenses: " << total << endl; return 0; }
#include <iostream> using namespace std; int main() { float costPrice, sellingPrice; cout << "Enter Cost Price: "; cin >> costPrice; cout << "Enter Selling Price: "; cin >> sellingPrice; if (sellingPrice > costPrice) cout << "Profit: " << sellingPrice - costPrice << endl; else if (sellingPrice < costPrice) cout << "Loss: " << costPrice - sellingPrice << endl; else cout << "No Profit No Loss." << endl; return 0; }
#include <iostream> using namespace std; int main() { int ram, sulabh, ajay; cout << "Enter the ages of Ram, Sulabh, and Ajay: "; cin >> ram >> sulabh >> ajay; if (ram < sulabh && ram < ajay) cout << "Ram is the youngest." << endl; else if (sulabh < ram && sulabh < ajay) cout << "Sulabh is the youngest." << endl; else cout << "Ajay is the youngest." << endl; return 0; }
#include <iostream> using namespace std; int main() { int angle1, angle2, angle3; cout << "Enter the three angles of a triangle: "; cin >> angle1 >> angle2 >> angle3; if (angle1 + angle2 + angle3 == 180) cout << "The triangle is valid." << endl; else cout << "The triangle is not valid." << endl; return 0; }
#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." << endl; else cout << year << " is not a leap year." << endl; return 0; }
#include <iostream> using namespace std; int main() { float basic, hra, da, gross; cout << "Enter basic salary: "; cin >> basic; if (basic < 1500) { hra = 0.1 * basic; da = 0.9 * basic; } else { hra = 500; da = 0.98 * basic; } gross = basic + hra + da; cout << "Gross salary: " << gross << endl; return 0; }
#include <iostream> using namespace std; int main() { int calls; float bill = 200; cout << "Enter the number of calls: "; cin >> calls; if (calls > 100) { if (calls <= 150) bill += (calls - 100) * 0.60; else if (calls <= 200) bill += 50 * 0.60 + (calls - 150) * 0.50; else bill += 50 * 0.60 + 50 * 0.50 + (calls - 200) * 0.40; } cout << "Total bill: Rs. " << bill << endl; return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, c, discriminant, root1, root2; cout << "Enter coefficients a, b, and c: "; cin >> a >> b >> c; if (a == 0) { cout << "This is not a quadratic equation." << endl; } else { discriminant = b * b - 4 * a * c; if (discriminant >= 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); cout << "Roots: " << root1 << " and " << root2 << endl; } else { cout << "No real roots exist." << endl; } } return 0; }