CHAPTER # 3
write a beginner level c++ program that reads four integers,prints their
sum,product and average
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, num4;
cout << "Enter four integers: ";
cin >> num1 >> num2 >> num3 >> num4;
int sum = num1 + num2 + num3 + num4;
cout << "Sum = " << sum << endl;
int product = num1 * num2 * num3 * num4;
cout << "Product = " << product << endl;
double average = sum / 4.0;
cout << "Average = " << average << endl;
return 0;
}
write a beginner level c++ program that reads length and breadth of a reactangle
and print its area
#include <iostream>
using namespace std;
int main()
{
double length, breadth, area;
cout << "Enter length of rectangle: ";
cin >> length;
cout << "Enter breadth of rectangle: ";
cin >> breadth;
area = length * breadth;
cout << "Area of rectangle = " << area << endl;
return 0;
}
write a beginner level c++ program that reads temperature in Fahrenheit and
prints its equivalent temmperature in celsius using formula c=f/9(f-32)
#include <iostream>
using namespace std;
int main()
{
double fahrenheit, celsius;
cout << "Enter temperature in Fahrenheit: ";
cin >> fahrenheit;
celsius = ((fahrenheit - 32) * 5) / 9;
cout << "Temperature in Celsius: " << celsius << endl;
return 0;
}
CHAPTEr 4;
write a program that reads an integer and prints its square if the number is
greater than 10 otherwise prints its cube
#include <iostream>
using namespace std;
int main()
{
int num, result;
// Prompt the user to enter an integer
cout << "Enter an integer: ";
cin >> num;
// Check if the number is greater than 10
if (num > 10) {
result = num * num; // Calculate the square
} else {
result = num * num * num; // Calculate the cube
}
// Output the result
cout << "Result: " << result << endl;
return 0;
}
Write a program that tells whether the alphabet is uppercase or lowercase
#include <iostream>
using namespace std;
int main()
{
char letter;
// Prompt the user to enter a letter
cout << "Enter a letter: ";
cin >> letter;
// Check if the letter is lowercase or uppercase
if (letter >= 'a' && letter <= 'z') {
cout << "The letter is lowercase." << endl;
} else if (letter >= 'A' && letter <= 'Z') {
cout << "The letter is uppercase." << endl;
} else {
cout << "Invalid input." << endl;
}
Write a program using for loop that prints the sum of series
#include <iostream>
using namespace std;
int main()
{
int sum = 0; // variable to hold the sum of the series
// loop through the series, incrementing by 3 from 30 to 60
for (int i = 30; i <= 60; i += 3) {
sum += i; // add current value of i to the sum
}
// print the sum of the series
cout << "The sum of the series is: " << sum << endl;
return 0;
}
(Above program using while loop)
#include <iostream>
int main() {
int number = 30;
int sum = 0;
while (number <= 60) {
sum += number;
number += 3;
}
std::cout << "The sum of the series is: " << sum <<
std::endl;
return 0;
}
Wirte a program prints multiplicative table upto 20
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
std::cout << "Multiplication table for " << number <<
":\n";
for (int i = 1; i <= 20; ++i) {
int result = number * i;
std::cout << number << " * " << i << " = " <<
result << std::endl;
}
return 0;
}
Write a program that prints all positive odd numbers upto 50 apart from those who are
divisible by 5
#include <iostream>
int main() {
for (int number = 1; number <= 50; number += 2) {
if (number % 5 == 0) {
continue;
}
std::cout << number << " ";
}
std::cout << std::endl;
return 0;
}
Write a program that reads an integer and prints its factorial
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
int factorial = 1;
for (int i = 1; i <= number; ++i) {
factorial *= i;
}
std::cout << "The factorial of " << number << " is: "
<< factorial << std::endl;
return 0;
}
program that reads ten numbers into an array and prints them in reverse order:
#include<iostream>
using namespace std;
int main() {
const int size = 10;
int numbers[size];
cout << "Enter ten numbers:\n";
for (int i = 0; i < size; ++i) {
cout << "Number " << i+1 << ": ";
cin >> numbers[i];
}
cout << "Numbers in reverse order:\n";
for (int i = size - 1; i >= 0; --i) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
Write a program that reads 10 numbers and print the smallest
along its index
#include <iostream>
int main() {
const int size = 10;
int numbers[size];
std::cout << "Enter ten numbers:\n";
for (int i = 0; i < size; ++i) {
std::cout << "Number " << i + 1 << ": ";
std::cin >> numbers[i];
}
int smallest = numbers[0];
int smallestIndex = 0;
for (int i = 1; i < size; ++i) {
if (numbers[i] < smallest) {
smallest = numbers[i];
smallestIndex = i;
}
}
std::cout << "Smallest number: " << smallest <<
std::endl;
std::cout << "Index: " << smallestIndex << std::endl;
return 0;
}
program that calculates the sum of positive numbers in the given 2D array:
#include <iostream>
int main() {
int arr[3][4] = {
{4, 18, 16, 11},
{-5, 10, -2, 12},
{15, -3, 17, 18}
};
int sum = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
if (arr[i][j] > 0) {
sum += arr[i][j];
}
}
}
std::cout << "The sum of positive numbers in the array
is: " << sum << std::endl;
return 0;
}
C++ program that reads a string, copies it into another string, and then prints
both strings:
#include <iostream>
#include <string>
int main() {
std::string originalString;
std::string copiedString;
std::cout << "Enter a string: ";
std::getline(std::cin, originalString);
copiedString = originalString;
std::cout << "Original string: " << originalString <<
std::endl;
std::cout << "Copied string: " << copiedString <<
std::endl;
return 0;
}
write a beginner C++ program that reads 2 strings of size 20
and print both with their length
#include <iostream>
#include <string>
int main() {
const int maxSize = 20;
std::string firstString;
std::string secondString;
std::cout << "Enter the first string (up to " << maxSize
<< " characters): ";
std::getline(std::cin, firstString);
std::cout << "Enter the second string (up to " <<
maxSize << " characters): ";
std::getline(std::cin, secondString);
std::cout << "First string: " << firstString << ",
length: " << firstString.length() << std::endl;
std::cout << "Second string: " << secondString << ",
length: " << secondString.length() << std::endl;
return 0;
}
write a beginner C++ program that reads 2 strings of size 20 and concatenate the
second string onto the end of first string and print it
#include <iostream>
#include <string>
int main() {
const int maxSize = 20;
std::string firstString;
std::string secondString;
std::cout << "Enter the first string (up to " << maxSize
<< " characters): ";
std::getline(std::cin, firstString);
std::cout << "Enter the second string (up to " <<
maxSize << " characters): ";
std::getline(std::cin, secondString);
if (firstString.length() + secondString.length() <=
maxSize) {
firstString += secondString;
std::cout << "Concatenated string: " << firstString
<< std::endl;
} else {
std::cout << "Concatenation not possible due to
exceeding maximum size." << std::endl;
}
return 0;
}
Write a C++ program that reads 3 strings and finds out the
smallest
#include <iostream>
#include <string>
int main() {
std::string firstString;
std::string secondString;
std::string thirdString;
std::cout << "Enter the first string: ";
std::getline(std::cin, firstString);
std::cout << "Enter the second string: ";
std::getline(std::cin, secondString);
std::cout << "Enter the third string: ";
std::getline(std::cin, thirdString);
std::string smallestString = firstString;
if (secondString < smallestString) {
smallestString = secondString;
}
if (thirdString < smallestString) {
smallestString = thirdString;
}
std::cout << "The smallest string is: " <<
smallestString << std::endl;
return 0;
}
write a beginner c++ program with a function that takes two
integer parameters, adds them and returns their sum:
#include <iostream>
int addTwoIntegers(int a, int b) {
int sum = a + b;
return sum;
}
int main() {
int num1, num2;
std::cout << "Enter the first integer: ";
std::cin >> num1;
std::cout << "Enter the second integer: ";
std::cin >> num2;
int result = addTwoIntegers(num1, num2);
std::cout << "The sum of the two integers is: " <<
result << std::endl;
return 0;
}
Write a program with function name “mean” to read in three
integers from keyboard and find their arithmetic mean
#include <iostream>
double mean(int num1, int num2, int num3) {
double sum = num1 + num2 + num3;
double mean = sum / 3.0;
return mean;
}
int main() {
int num1, num2, num3;
std::cout << "Enter the first integer: ";
std::cin >> num1;
std::cout << "Enter the second integer: ";
std::cin >> num2;
std::cout << "Enter the third integer: ";
std::cin >> num3;
double result = mean(num1, num2, num3);
std::cout << "The arithmetic mean is: " << result <<
std::endl;
return 0;
}
c++ program that has a function name rectangle to read the length and width of
the of rectangle from keyboard and find area of the rectangle. Result should
return to main program for displaying on screen
#include <iostream>
double rectangle(double length, double width) {
double area = length * width;
return area;
}
int main() {
double length, width;
std::cout << "Enter the length of the rectangle: ";
std::cin >> length;
std::cout << "Enter the width of the rectangle: ";
std::cin >> width;
double area = rectangle(length, width);
std::cout << "The area of the rectangle is: " << area <<
std::endl;
return 0;
}
write a beginner C++ program having two functions named area
and perimeter to find area and permieter of a square
#include <iostream>
double area(double sideLength) {
double area = sideLength * sideLength;
return area;
}
double perimeter(double sideLength) {
double perimeter = 4 * sideLength;
return perimeter;
}
int main() {
double sideLength;
std::cout << "Enter the length of the side of the
square: ";
std::cin >> sideLength;
double squareArea = area(sideLength);
double squarePerimeter = perimeter(sideLength);
std::cout << "The area of the square is: " << squareArea
<< std::endl;
std::cout << "The perimeter of the square is: " <<
squarePerimeter << std::endl;
return 0;
}
C++ program that reads a number from the keyboard and then passes it to a
function to determine whether it is prime or composite:
#include <iostream>
bool isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i * i <= number; ++i) {
if (number % i == 0) {
return false;
}
}
return true;
}
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (isPrime(number)) {
std::cout << number << " is a prime number." <<
std::endl;
} else {
std::cout << number << " is a composite number." <<
std::endl;
}
return 0;
}
c++ program to get an integer number from keyboard in main progress and pass
it as an argument to a function where it calculate and display the table
#include <iostream>
void displayMultiplicationTable(int number) {
for (int i = 1; i <= 10; ++i) {
std::cout << number << " x " << i << " = " <<
(number * i) << std::endl;
}
}
int main() {
int number;
std::cout << "Enter an integer number: ";
std::cin >> number;
displayMultiplicationTable(number);
return 0;
}