0% found this document useful (0 votes)
12 views15 pages

CPPPrograms (2018 2023)

C++ Programming Codes

Uploaded by

doreamon1nobita0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

CPPPrograms (2018 2023)

C++ Programming Codes

Uploaded by

doreamon1nobita0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ Programs (2018-2023)

1. Write a C++ function to find surface area of a sphere. (Hint : Surface area of
sphere = A=4πr² )

#include <iostream>

using namespace std;

float areaOfSphere(float radius)


{

float areaOfSphere = 4 * 3.14 * radius * radius;


return areaOfSphere;
}

int main()
{
float r;
cout << "Enter the radius of sphere:";
cin >> r;
float area = areaOfSphere(r);
cout << "Area of sphere is:" << area;

return 0;
}

2. Write a program in C++ using OOP techniques to compute the circumference


of a circle.()

#include <iostream>

C++ Programs (2018-2023) 1


// circumference of circle=2*3.14*radius
using namespace std;

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.

C++ Programs (2018-2023) 2


#include <iostream>

using namespace std;

int main()
{
int arr[10];

cout << "Enter the elements of array:";


for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}

// 10 20 30 40 50 60 70 80 90 100

int sum = 0;

for (int i = 0; i < 10; i++)


{
sum = sum + arr[i];
}

cout << "Sum of elements of array is:" << sum << endl;

float avg = sum / 10;

cout << "Average of elements of array is:" << avg;


return 0;
}

4. Write a C+ program to find factorial of a number during execution by


using constructor function.

C++ Programs (2018-2023) 3


#include <iostream>

using namespace std;

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;
}

5. Write C++ program to find a factorial of integers from 1. to 5.

// Write C++ program to find a factorial of integers from 1. to

#include <iostream>

C++ Programs (2018-2023) 4


using namespace std;

int factorial(int num)


{

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;
}

6. Write a C+ program which finds a fibonacci series of n terms.

#include <iostream>

using namespace std;

int main()
{
int n;
cout << "Enter the number of terms:";
cin >> n;

C++ Programs (2018-2023) 5


int f1 = 0;
int f2 = 1;
int next;

cout << f1 << " " << f2 << " ";

for (int i = 3; i <= n; i++)


{
next = f1 + f2;
f1 = f2;
f2 = next;

cout << next << " ";


}
return 0;
}

7. Write C++ program to generate and print first 15 terms of fibonacci series (1, 1,
2, 3, 5……)

#include <iostream>

using namespace std;

int main()
{
int f1 = 0;
int f2 = 1;
int next;

for (int i = 1; i < 13; ++i)


{
next = f1 + f2;

C++ Programs (2018-2023) 6


f1 = f2;
f2 = next;
cout << next << endl;
}

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>

using namespace std;

int main()
{
int num;

cout << "Enter the number:";


cin >> num;
int isPrime = 1;

for (int i = 2; i < num; i++)


{
if (num % i == 0)
{
isPrime = 0;
break;
}
}

C++ Programs (2018-2023) 7


if (isPrime == 1)
{
cout << "The number is prime";
}
else
{
cout << "The number is not prime";
}

return 0;
}

10. Implement a class average that accepts value of three float variables another
function print average of three numbers.

#include <iostream>

using namespace std;

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;

C++ Programs (2018-2023) 8


cout << "Average :" << avg;
}
};

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>

using namespace std;

int main()
{
int num = 125;
int sum = 0;
int digit;

while (num > 0)

while (num > 0)


{
digit = num % 10; // 2
sum = sum + digit; // sum=7
num = num / 10; // 1
}

cout << "Sum of digits is:" << sum;

C++ Programs (2018-2023) 9


return 0;
}

12. Write class based C + + program to accept two integers and find it’s G.C.D.
(Greatest Common Divisor)

#include <iostream>

using namespace std;

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;
}

C++ Programs (2018-2023) 10


}

cout << "GCD is:" << 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>

using namespace std;

int main()
{
int num = 153;
int digit = 0;
int sum = 0;
int temp = num;

while (temp > 0)


{
digit = temp % 10;
sum = sum + (digit * digit * digit);
temp = temp / 10;
}

C++ Programs (2018-2023) 11


cout << "Sum of digits is:" << sum << endl;
cout << "The number is:" << num << endl;

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>

using namespace std;

int main()
{
char str[97];
int count = 0;

cout << "Enter the string:";


cin.getline(str, 97);

for (int i = 0; i < 97; i++)


{
if (str[i] == 'M')
{

C++ Programs (2018-2023) 12


count++;
}
}

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>

using namespace std;

int main()
{
char str[80];
int count = 1;

cout << "Enter the string:";


cin.getline(str, 80);

for (int i = 0; i < 80; i++)


{
if (str[i] == ' ')
{
count++;
}
}

cout << "The number of words in the string is:" << count;

C++ Programs (2018-2023) 13


return 0;
}

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>

using namespace std;

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()
{

int smallest = min(1, 2, 3, 4);

C++ Programs (2018-2023) 14


cout << "Smallest Number is :" << smallest << endl;
return 0;
}

C++ Programs (2018-2023) 15

You might also like