CPPPrograms (2018 2023)
CPPPrograms (2018 2023)
1. Write a C++ function to find surface area of a sphere. (Hint : Surface area of
sphere = A=4πr² )
#include <iostream>
int main()
{
float r;
cout << "Enter the radius of sphere:";
cin >> r;
float area = areaOfSphere(r);
cout << "Area of sphere is:" << area;
return 0;
}
#include <iostream>
class Circle
{
float radius;
public:
// Circle()
// {
// radius = 10;
// }
void circumferenceOfCircle()
{
cout << "Enter the radius of circle:";
cin >> radius;
float circumference = 2 * 3.14 * radius;
cout << "Circumference of circle is:" << circumference;
}
};
int main()
{
Circle c1;
c1.circumferenceOfCircle();
return 0;
}
3. Write a C++ program to accept 10 integers in an array and find its sum and
average.
int main()
{
int arr[10];
// 10 20 30 40 50 60 70 80 90 100
int sum = 0;
cout << "Sum of elements of array is:" << sum << endl;
class Factorial
{
int a;
public:
Factorial()
{
cout << "Enter the number:";
cin >> a;
int fact = 1;
for (int i = 1; i <= a; i++)
{
fact = fact * i;
}
cout << "Factorial of " << a << " is:" << fact;
}
};
int main()
{
Factorial fact;
return 0;
}
#include <iostream>
int fact = 1;
for (int i = 1; i <= num; i++)
{
fact = fact * i;
}
return fact;
}
int main()
{
for (int i = 1; i <= 5; i++)
{
cout << "Factorial of " << i << " is:" << factorial(i) <
}
return 0;
}
#include <iostream>
int main()
{
int n;
cout << "Enter the number of terms:";
cin >> n;
7. Write C++ program to generate and print first 15 terms of fibonacci series (1, 1,
2, 3, 5……)
#include <iostream>
int main()
{
int f1 = 0;
int f2 = 1;
int next;
return 0;
}
8. Write C++ program to read any integer and then check whether it's prime or
not prime no.
9. Write a C++ program to accept an integer number and test whether it is prime
or not.
#include <iostream>
int main()
{
int num;
return 0;
}
10. Implement a class average that accepts value of three float variables another
function print average of three numbers.
#include <iostream>
class Average
{
float a, b, c;
public:
Average(float x, float y, float z) // x=10.5, y=20.5, z=30.5
{
a = x; // a=10.5
b = y; // b=20.5
c = z; // c=30.5
}
void calculateAvg()
{
float avg = (a + b + c) / 3;
int main()
{
Average avgObj(10.5, 20.5, 30.5);
avgObj.calculateAvg();
return 0;
}
11. Write an object-oriented program in C++ to read an integer number and find
the sum of its digits. [Hint: input 125 output 8 i.e., 1+2+5=8]
#include <iostream>
int main()
{
int num = 125;
int sum = 0;
int digit;
12. Write class based C + + program to accept two integers and find it’s G.C.D.
(Greatest Common Divisor)
#include <iostream>
class GCD
{
int a, b;
public:
GCD()
{
a = 4;
b = 16;
}
void calculateGCD()
{
while (a != b)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
int main()
{
GCD objgcd;
objgcd.calculateGCD();
return 0;
}
13. Write a C++ program to find the entered number is Armstrong number OR NOT
Armstong.
#include <iostream>
int main()
{
int num = 153;
int digit = 0;
int sum = 0;
int temp = num;
if (sum == num)
{
cout << "The number is an Armstrong number";
}
else
{
cout << "The number is not an Armstrong number";
}
return 0;
}
14. Write a C++ program to count and print occurrence of the character M' ' in a
given string of maximum 97 characters.
#include <iostream>
int main()
{
char str[97];
int count = 0;
cout << "The number of M's in the string is:" << count;
return 0;
}
15. Write a C++ program to accept sentence of 80 characters and count number
of words in a sentence.
#include <iostream>
int main()
{
char str[80];
int count = 1;
cout << "The number of words in the string is:" << count;
16. Write a C++ program to find the smallest of four given integers using the
function min() that returns the smallest of four given integers. The function
prototype is int min(int, int, int, int) .
#include <iostream>
int min(int num1, int num2, int num3, int num4) // num1=1,num2=2
{
int smallest = num1;
if (smallest > num2)
{
smallest = num2;
}
if (smallest > num3)
{
smallest = num3;
}
if (smallest > num4)
{
smallest = num4;
}
return smallest;
}
int main()
{