0% found this document useful (0 votes)
11 views16 pages

C++Programs

The document contains a series of C++ programming exercises from 2018 to 2024, covering various topics such as calculating the surface area of a sphere, finding the circumference of a circle, and implementing object-oriented programming techniques. It includes code examples for tasks like calculating sums, averages, factorials, Fibonacci series, checking for prime numbers, and more. Each exercise is accompanied by code snippets demonstrating the implementation of the described functionality.
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)
11 views16 pages

C++Programs

The document contains a series of C++ programming exercises from 2018 to 2024, covering various topics such as calculating the surface area of a sphere, finding the circumference of a circle, and implementing object-oriented programming techniques. It includes code examples for tasks like calculating sums, averages, factorials, Fibonacci series, checking for prime numbers, and more. Each exercise is accompanied by code snippets demonstrating the implementation of the described functionality.
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/ 16

C++ Programs (2018-2024)

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

C++ Programs (2018-2024) 1


cout << "Area of sphere is:" << area;

return 0;
}

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


of a circle.(2πr)

#include <iostream>

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

class Circle
{
float radius;

public:

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

C++ Programs (2018-2024) 2


return 0;
}

3. Write a C++ program to accept 10 integers in an array and find its sum and
average.

#include<iostream>
using namespace std;

int main(){
int arr[10];
int sum=0;

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


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

for(int i=0;i<5;i++){
sum=sum+arr[i];
}

cout<<"Sum="<<sum<<endl;

cout<<"Average="<<sum/10;

return 0;
}

4. Write a program in C+ to accept 10 integers in an array from. keyboard


and find largest element of array.

C++ Programs (2018-2024) 3


#include<iostream>
using namespace std;

int main(){

int arr[10];
cout<<"Enter the elements of array:"<<endl;
for(int i=0;i<10;i++){
cin>>arr[i];
}

int largest=arr[0];

for(int i=1;i<10;i++){
if (largest<arr[i]){
largest=arr[i];
}
}

cout<<"Largest element in the array is:"<<largest;

return 0;
}

5. Write a program in C+ to accept 10 integers in an array from. keyboard


and find largest element of array.

#include<iostream>
using namespace std;

int main(){

C++ Programs (2018-2024) 4


int arr[10];
cout<<"Enter the elements of array:"<<endl;
for(int i=0;i<10;i++){
cin>>arr[i];
}

int smallest=arr[0];

for(int i=1;i<10;i++){
if (smallest>arr[i]){
smallest=arr[i];
}
}

cout<<"Smallest element in the array is:"<<smallest;

return 0;
}

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


using constructor function.

#include<iostream>
using namespace std;

class Factorial{

public:
int num;
Factorial(){
cout<<"Enter the number to find factorial:"<<endl;

C++ Programs (2018-2024) 5


cin>>num;
int fact=1;

for(int i=1;i<=num;i++){
fact=fact*i;
}

cout<<"Factorial of "<<num<<" is:"<<fact;


}
};

int main(){
Factorial obj;

return 0;
}

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

#include<iostream>
using namespace std;

void factorial(int num){


int fact=1;

for(int i=1;i<=num;i++){
fact=fact*i;
}

cout<<"Factorial of "<<num<<" is:"<<fact<<endl;


}

C++ Programs (2018-2024) 6


int main(){

for(int i=1;i<=5;i++){
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;
int n1=0,n2=1;
int next=0;

cout<<"Enter the number of terms:"<<endl;


cin>>n;
cout<<"Fibonacci series:"<<endl;
cout<<n1<<endl;
cout<<n2<<endl;

for(int i=3;i<=n;i++){
next=n1+n2;
cout<<next<<endl;
n1=n2;

C++ Programs (2018-2024) 7


n2=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 n=15;
int n1=0,n2=1;
int next=0;

cout<<"Fibonacci series:"<<endl;
cout<<n1<<endl;
cout<<n2<<endl;

for(int i=3;i<=n;i++){
next=n1+n2;
cout<<next<<endl;
n1=n2;
n2=next;
}

C++ Programs (2018-2024) 8


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

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

C++ Programs (2018-2024) 9


{
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:
void getNumbers()
{
cout<<"Enter three numbers"<<endl;
cin>>a>>b>>c;
}

void calculateAvg()
{
float avg = (a + b + c) / 3;
cout << "Average :" << avg;
}
};

int main()
{

C++ Programs (2018-2024) 10


Average obj;
obj.getNumbers();
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;

class MyClass{

public:
int num;
void sumOfDigits(){
cout<<"Enter a number:";
cin>>num;//17
int sum=0;

while(num>0){//0>0
int digit=num%10;//digit=1%10=1
sum=sum+digit;//sum=7+1=8
num=num/10;//num=1/10=0
}
cout<<"Sum of digits is:"<<sum;
}

};

int main(){

C++ Programs (2018-2024) 11


MyClass obj;
obj.sumOfDigits();
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{

public:
int num1,num2;

void getGCDNumber(){
cout<<"Enter two numbers:";
cin>>num1>>num2;

while(num1!=num2){
if(num1>num2){
num1=num1-num2;
}else{
num2=num2-num1;
}
}

cout<<"GCD is:"<<num1;
}
};

C++ Programs (2018-2024) 12


int main(){

GCD obj;
obj.getGCDNumber();

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=125;
int sum=0;
int temp=num;

while(temp>0){ //0>0=false
int digit=temp%10; //digit=1%10=1
sum=sum+(digit*digit*digit); //sum=152+(1*1*1)=153
temp=temp/10;//temp=1/10=0
}

if(sum==num){
cout<<"Armstrong number";
}
else{
cout<<"Not an Armstrong number";
}

C++ Programs (2018-2024) 13


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>
// #include<string.h>
using namespace std;

int main(){
char str[80];
int wordCount=0;
cout<<"Enter a string:";
cin.getline(str,80);
int length=strlen(str);

for(int i=0;i<length;i++){
if(str[i]==' '){
wordCount++;
}
}

return 0;
}

15. Write a C++ program to accept sentence of 80 characters and count number
of words in a sentence.

#include <iostream>
// #include<string.h>

C++ Programs (2018-2024) 14


using namespace std;

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

cout << "Enter the string:";


cin.getline(str, 80);
int length=strlen(str);
for (int i = 0; i < length; i++)
{
if (str[i] == ' ')
{
count++;
}
}

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

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,num3=3,num4
{
int smallest = num1;

C++ Programs (2018-2024) 15


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


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

1. Write a program in C+ to create a class test having member functions


getmarks () to read marks of two subjects and showsum () to display the
total marks.

C++ Programs (2018-2024) 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy