Q3: Write a C++ program to read five numbers, find their sum, and print the numbers in reverse order.
#include <iostream>
using namespace std;
int main()
{
int sum=0;
int number [5];
cout<<"enter the five number : "<<endl;
for (int i = 0; i < 5; i++)
{
cout<<" number "<<i+1<<" : ";
cin>>number[i];
sum += number[i];
}
cout<<" the sum "<<sum<<endl;
cout<<" number in reverse : "<<endl;
for (int i = 4; i>=0; i--)
{
cout<< number[i]<<", ";
}
return 0;
}
❤1👍1
Q4: Write a C++ program to find the sum and average of six numbers.
#include <iostream>
using namespace std;
int main()
{
float sum=0;
float average;
float number [6];
cout<< " enter the six number : "<<endl;
for (int i = 0; i < 6; i++)
{
cout<<" number "<<i+1<<" : ";
cin>>number[i];
sum += number[i];
average = sum/6;
}
cout<<" the sum "<<sum<<endl;
cout<<" the average "<<average<<endl;
return 0;
}
👍1
Q5: Write a C++ program to print the sequence of numbers (10, 11, 12 … 30). Use while loop statement.
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
while ( a <= 30)
{
cout<<a<<", ";
a++;
}
return 0;
}
👍1
Q6: Write a C++ program to print the sequence of odd numbers from (11 - 29). Use while loop statement.
#include <iostream>
using namespace std;
int main ()
{
int a = 11;
while ( a <= 29)
{
cout<<a<<", ";
a += 2;
}
return 0;
}
👍1
Q7: Write a C++ program to print the sequence of even numbers from (10 - 30). Use while loop statement.
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
while ( a <= 30)
{
cout<<a<<", ";
a += 2;
}
return 0;
}
👍1
Q8: Write a C++ program to print the sequence of numbers (15, 16, 17 … 30). Use do while loop statement.
#include <iostream>
using namespace std;
int main ()
{
int a = 15;
do
{
cout<<a<<", ";
a++;
}
while ( a < 30);
cout<<a<<", ";
return 0;
}
👍1
Q9: Write a C++ program to print the sequence of odd numbers from (11 - 29). Use do while loop statement.
#include <iostream>
using namespace std;
int main ()
{
int a = 11;
do
{
cout<<a<<", ";
a += 2;
}
while ( a < 29);
cout<<a<<", ";
return 0;
}
👍1
Q10: Write a C++ program to print the sequence of even numbers from (10 - 30). Use do while loop statement.
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
do
{
cout<<a<<", ";
a += 2;
}
while ( a < 30);
cout<<a<<", ";
return 0;
}
👍1
Q11: Write a C++ program that takes two integers as input and checks if they are equal, greater than, or less than each other
#include <iostream>
using namespace std;
int main ()
{
int a, b;
cout<<"enter the value of a : ";
cin>>a;
cout<<"enter the value of b : ";
cin>>b;
if ( a == b )
{
cout<<" a equal b "<<endl;
}
else if ( a > b)
{
cout<<" a is greater than b "<<endl;
}
else if ( a < b )
{
cout<<" a is less than b "<<endl;
}
return 0;
}
👍1
Q12: Create a C++ program that determines whether a given year is a leap year or not. Provide appropriate messages based on the result.
#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;
}
👍1
Q13: Implement a C++ program that takes a student's percentage as input and assigns a grade according to the following criteria:
90% and above: A
80-89%: B
70-79%: C
Below 70%: F
90% and above: A
80-89%: B
70-79%: C
Below 70%: F
#include <iostream>
using namespace std;
int main()
{
int grade;
cout<<" enter your grade ";
cin>>grade;
if ( grade > 90 )
{
cout<<" your grade is : A "<<endl;
}
else if ( grade > 80 )
{
cout<<" your grade is : B "<<endl;
}
else if ( grade > 70 )
{
cout<<" your grade is : C "<<endl;
}
else
{
cout<<" your grade is : F "<<endl;
}
return 0;
}
👍1
Q14: Write a C++ program to determine if a given number is positive, negative, or zero. Use appropriate conditional statements.
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<" enter the number ";
cin>>num;
if ( num > 0 )
{
cout<<" the number is positive "<<endl;
}
else if ( num < 0)
{
cout<<" the number is negative "<<endl;
}
else
{
cout<<" the number is zero "<<endl;
}
return 0;
}
👍1
Q15: Develop a simple C++ calculator program with a menu that performs addition, subtraction, multiplication, and division based on the user's choice.
#include <iostream>
using namespace std;
int main()
{
float num1, num2, result;
char op;
cout<<" enter the number1 : ";
cin>>num1;
cout<<" enter operator (+, -, *, /) : ";
cin>>op;
cout<<" enter the number2 : ";
cin>>num2;
switch (op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if ( num2 != 0)
{
result = num1 / num2;
}
else
{
cout<<" error "<<endl;
return 1;
}
break;
default:
cout<<" error "<<endl;
return 1;
}
cout<<result<<endl;
return 0;
}
👍1
Q16: Create a C++ program that takes a character as input and determines whether it is a vowel, consonant.
#include <iostream>
using namespace std;
int main()
{
char character;
cout << "Enter a character: ";
cin >> character;
if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'))
{
switch (character)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << character << " is a vowel." << endl;
break;
default:
cout << character << " is a consonant." << endl;
}
}
else
{
cout << "Invalid input. Please enter a valid alphabet character." << endl;
}
return 0;
}
👍1
Q17: Write a C++ program that classifies a person into different age groups: child (0-12), teenager (13-19), adult (20-59), or senior (60 and above).
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<" enter your age : ";
cin>>age;
if( age <= 12 )
{
cout<<"your a child"<<endl;
}
else if ( age <=19 )
{
cout<<"your a teenager"<<endl;
}
else if ( age <= 59 )
{
cout<<"your a adult"<<endl;
}
else if ( age >= 60 )
{
cout<<"your a senior"<<endl;
}
return 0;
}
👍1
Q18: Implement a C++ program that converts temperatures between Celsius and Fahrenheit. Ask the user for the temperature and the unit, then perform the conversion based on the input. Hint:
(Temperature in degrees Fahrenheit (°F) = (Temperature in degrees Celsius (°C) * 9/5) + 32)
(Temperature in degrees Fahrenheit (°F) = (Temperature in degrees Celsius (°C) * 9/5) + 32)
#include <iostream>
using namespace std;
int main() {
float c, f;
cout << " value ";
cin >> c;
f = ( c * 9/5 ) + 32;
cout << " in F the temp is :" << f <<endl;
return 0;
}
👍1
Q19: Create a C++ program that takes two integers as input and checks whether the first number is divisible by the second one. Provide appropriate messages based on the result.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
if (num2 != 0)
{
if (num1 % num2 == 0)
{
cout << num1 << " is divisible by " << num2 << endl;
}
else
{
cout << num1 << " is not divisible by " << num2 << endl;
}
}
else
{
cout << "Error: Division by zero is not allowed." << endl;
}
return 0;
}
👍1
Q20: Write a C++ program that takes three numbers as input and determines the largest among them using conditional statements
#include <iostream>
using namespace std;
int main() {
double num1, num2, num3;
cout << "Enter num1 : ";
cin >> num1;
cout << "Enter num2 : ";
cin >> num2;
cout << "Enter num3 : ";
cin >> num3;
if (num1 >= num2 && num1 >= num3) {
cout << "The largest number is : " << num1 << endl;
}
else if (num2 >= num1 && num2 >= num3)
{
cout << "The largest number is : " << num2 << endl;
}
else
{
cout << "The largest number is : " << num3 << endl;
}
return 0;
}
👍2
Q21: Create a C++ program that takes three angles as input and classifies the triangle based on the angle values (acute, obtuse, or right-angled). determines the largest among them using conditional statements
#include <iostream>
using namespace std;
int main()
{
int angle1, angle2, angle3;
cout << "Enter the first angle: ";
cin >> angle1;
cout << "Enter the second angle: ";
cin >> angle2;
cout << "Enter the third angle: ";
cin >> angle3;
if (angle1 + angle2 + angle3 == 180)
{
if (angle1 < 90 && angle2 < 90 && angle3 < 90)
{
cout << "The triangle is acute-angled." << endl;
}
else if (angle1 == 90 || angle2 == 90 || angle3 == 90)
{
cout << "The triangle is right-angled." << endl;
}
else
{
cout << "The triangle is obtuse-angled." << endl;
}
}
else
{
cout << "Invalid input: The angles do not form a triangle." << endl;
}
return 0;
}
👍1
Q22: Write a C++ program that calculates the total cost after applying a discount based on the purchase amount. For example, provide a 10% discount if the purchase amount is above $100.
#include <iostream>
using namespace std;
int main()
{
int purchaseamount, discount, totalcost;
cout<<" enter the purchaseamount: ";
cin>>purchaseamount;
if ( purchaseamount > 100)
{
discount = 0.10 * purchaseamount;
}
else
{
discount = 0;
}
totalcost = purchaseamount - discount;
cout<< "Discount applied: $"<<discount<<endl;
cout<< "Total cost after discount: $"<<totalcost<<endl;
return 0;
}
👍1