1. write a program in C++ to accept two number and find there sum and average using input function.
#include <iostream>
int main() {
double num1, num2;
// Accept two numbers from the user
std::cout << "Enter the first number: ";
std::cin >> num1;
std::cout << "Enter the second number: ";
std::cin >> num2;
// Calculate the sum
double sum = num1 + num2;
// Calculate the average
double average = sum / 2;
// Display the results
std::cout << "Sum: " << sum << std::endl;
std::cout << "Average: " << average << std::endl;
return 0;
}
2. write a C++ program to print car structure that havaa carName, carData, carNumber, carPrice and carDealerName using input function.
#include <iostream>
#include <string>
struct Car {
std::string carName;
std::string carData;
std::string carNumber;
double carPrice;
std::string carDealerName;
};
int main() {
Car car;
// Input car details from the user
std::cout << "Enter Car Name: ";
std::cin >> car.carName;
std::cout << "Enter Car Data: ";
std::cin >> car.carData;
std::cout << "Enter Car Number: ";
std::cin >> car.carNumber;
std::cout << "Enter Car Price: ";
std::cin >> car.carPrice;
std::cout << "Enter Car Dealer Name: ";
std::cin >> car.carDealerName;
// Print the car details
std::cout << "\nCar Details:\n";
std::cout << "Car Name: " << car.carName << std::endl;
std::cout << "Car Data: " << car.carData << std::endl;
std::cout << "Car Number: " << car.carNumber << std::endl;
std::cout << "Car Price: $" << car.carPrice << std::endl;
std::cout << "Car Dealer Name: " << car.carDealerName << std::endl;
return 0;
}
3. write a code in C++ to print any name and age using input function
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
// Input name and age from the user
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
// Print the name and age
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << " years" << std::endl;
return 0;
}
4. write a program in C++ input basic Pay,Tax,Da,Ma,Hra and find the total pay, using input function.
#include <iostream>
int main() {
double basicPay, tax, da, ma, hra, totalPay;
// Input basic pay, tax, DA, MA, and HRA from the user
std::cout << "Enter Basic Pay: ";
std::cin >> basicPay;
std::cout << "Enter Tax: ";
std::cin >> tax;
std::cout << "Enter DA (Dearness Allowance): ";
std::cin >> da;
std::cout << "Enter MA (Medical Allowance): ";
std::cin >> ma;
std::cout << "Enter HRA (House Rent Allowance): ";
std::cin >> hra;
// Calculate total pay
totalPay = basicPay - tax + da + ma + hra;
// Display the total pay
std::cout << "Total Pay: " << totalPay << std::endl;
return 0;
}
5. Write a program that print Hello World.
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
6. Write a C++ program that takes two numbers as input from the user and calculates their sum.
#include <iostream>
int main() {
double num1, num2, sum;
std::cout << "Enter the first number: ";
std::cin >> num1;
std::cout << "Enter the second number: ";
std::cin >> num2;
sum = num1 + num2;
std::cout << "Sum: " << sum << std::endl;
return 0;
}
7. Write a C++ program that calculates the area of a rectangle given its length and width.
#include <iostream>
int main() {
double length, width, area;
std::cout << "Enter the length of the rectangle: ";
std::cin >> length;
std::cout << "Enter the width of the rectangle: ";
std::cin >> width;
area = length * width;
std::cout << "Area: " << area << std::endl;
return 0;
}
8. Write a C++ program a that determines the largest number among three given numbers.
#include <iostream>
int main() {
double num1, num2, num3, largest;
std::cout << "Enter three numbers: ";
std::cin >> num1 >> num2 >> num3;
largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
std::cout << "The largest number is: " << largest << std::endl;
return 0;
}
9. Write a C++ program that checks if a number is even or odd.
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
if (number % 2 == 0) {
std::cout << number << " is even." << std::endl;
} else {
std::cout << number << " is odd." << std::endl;
}
return 0;
}
10. Write a C++ program that calculates the factorial of a positive integer.
#include <iostream>
int main() {
int number, factorial = 1;
std::cout << "Enter a positive integer: ";
std::cin >> number;
if (number < 0) {
std::cout << "Factorial is not defined for negative numbers." << std::endl;
} else {
for (int i = 1; i <= number; i++) {
factorial *= i;
}
std::cout << "Factorial of " << number << " is " << factorial << std::endl;
}
return 0;
}
11. Write a C++ program that acts as a simple calculator. It takes two numbers and an operator (+, -, *, /) as input and performs the corresponding operation.
#include <iostream>
int main() {
double num1, num2, result;
char operation;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << "Enter an operator (+, -, *, /): ";
std::cin >> operation;
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
std::cout << "Division by zero is not allowed." << std::endl;
return 1;
}
break;
default:
std::cout << "Invalid operator." << std::endl;
return 1;
}
std::cout << "Result: " << result << std::endl;
return 0;
}
12. Write a C++ program that calculates the average of five numbers entered by the user: C++ program that calculates the average of five numbers entered by the user:
#include <iostream>
int main() {
double num1, num2, num3, num4, num5, average;
std::cout << "Enter five numbers: ";
std::cin >> num1 >> num2 >> num3 >> num4 >> num5;
average = (num1 + num2 + num3 + num4 + num5) / 5.0;
std::cout << "Average: " << average << std::endl;
return 0;
}
Comments
Post a Comment