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

C++ Programs

The document contains a series of C++ programs designed for educational purposes, covering various topics such as input/output operations, mathematical calculations, data structures, and object-oriented programming. Each program includes a brief description of its functionality, followed by the corresponding C++ code. The programs range from simple tasks like converting digits to words and finding the sum of natural numbers to more complex operations like calculating roots of quadratic equations and implementing classes and structures.

Uploaded by

notin.1404
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)
10 views16 pages

C++ Programs

The document contains a series of C++ programs designed for educational purposes, covering various topics such as input/output operations, mathematical calculations, data structures, and object-oriented programming. Each program includes a brief description of its functionality, followed by the corresponding C++ code. The programs range from simple tasks like converting digits to words and finding the sum of natural numbers to more complex operations like calculating roots of quadratic equations and implementing classes and structures.

Uploaded by

notin.1404
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

.

C++ Programs for 2025-2026

1. Input a digit and display the same in word using switch


statement. (The output should be any of these-Zero for 0, One for
1,...., Nine for 9)
Program: Digital to word using switch

#include <iostream>
using namespace std;

int main()

int digit;

cout<< "Enter a digit (0-9) : ";

cin>> digit;

switch(digit)
{

case 0: cout<< "Zero"; break;

case 1: cout<< "One"; break;

case 2: cout<< "Two"; break;

case 3: cout<< "Three"; break;

case 4: cout<< "Four"; break;

case 5: cout<< "Five"; break;

case 6: cout<< "Six"; break;


case 7: cout<< "Seven"; break;
case 8: cout<< "Eight"; break;
case 9: cout<< "Nine"; break;

default: cout<< "Invalid input";

return 0;
}

[Link] the roots of a quadratic equation by inputting the three


coefficients.
Program: Roots of quadratic equation

#include <iostream>

#include <cmath>

using namespace std;

int main()

float a, b, c, d, root1, root2;

cout<< "Enter coefficients a, b and c : ";


cin>> a >> b >> c;

d = b*b - 4*a*c;

if(d > 0)

root1 = (-b + sqrt(d)) / (2*a);

root2 = (-b - sqrt(d)) / (2*a);

cout<< "Roots are real and distinct: " << root1 << " , " << root2;

}
else if(d == 0)
{

root1 =root2= -b / (2*a);

cout<< "Roots are real and equal: " << root1<<”,”<<root2;

}
else

cout<< "Roots are imaginary";

return 0;

3. Input a day number of a week and display the corresponding


name of the day.
Program: Day number to day name.
#include <iostream>

using namespace std;

int main()

{
int day;

cout<< “Enter day number (1-7): “;

cin>> day;

switch(day)

case 1: cout<< “Sunday”; break;

case 2: cout<< “Monday”; break;

case 3: cout<< “Tuesday”; break;


case 4: cout<< “Wednesday”; break;
case 5: cout<< “Thursday”; break;

case 6: cout<< “Friday”; break;

case 7: cout<< “Saturday”; break;

default: cout<< “Invalid input”;


}

return 0;

4. Find the sum of the first N natural numbers without using any
formula.
Program: Sum of first N Natural numbers ( no formula)

#include <iostream>

using namespace std;

int main()

int n, sum = 0;

cout<< “Enter the value of N : “;


cin>> n;

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


{

sum = sum + i;

cout<< “Sum = “ << sum;

return 0;

}
5. Input an integer number and find the sum of its digits.
Program: Sum of digits

#include <iostream>

using namespace std;

int main()

{
int n, d,sum = 0;

cout<< “Enter a number : “;

cin>> n;

while(n > 0)

d = n%10;

sum = sum + d;
n = n/10;

cout<< “Sum of digits = “ << sum;

return 0;

6. Display the first N terms of Fibonacci series.


Program: Fibonacci series

#include <iostream>

using namespace std;


int main()
{

Int n, a=0, b=1, c;

cout<< “Enter No. of terms : “;

cin>> n;
cout<< “Fibonacci Series : “;

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

Cout<< a << “ “;

c = a + b;

a = b;

b = c;

}
return 0;

7. Input a number and check whether it is palindrome or not.


Program: Palindrome check

#include <iostream>
using namespace std;
int main()
{
int n, [Link]=0, temp;
cout<< “Enter a number : “;
cin>> n;
temp = n;
while(temp > 0)
{
d = temp%10;
rev = rev*10 + d;
temp = temp/10;
}
If(rev == n)
cout<< “Palindrome”;
else
cout<< “Not Palindrome”;
return 0;
}

8. Input an integer number and check whether it is a prime or not.


Program: Prime check

#include <iostream>

using namespace std;

int main()
{

int n, flag=0;

cout<< “Enter a number : “;

cin>> n;

for(int i=2; i<=n/2; i++)

if (n % i == 0)

{
flag=1;

break;

if(n==1)

cout<< “Not prime”;


else if (flag==0)
cout<< “Prime”;

else

cout<< “Not prime”;

return 0;
}

9. Create an array of N numbers into an array and find their sum


and average.
Program: Array sum and average

#include <iostream>
using namespace std;
int main()
{
int n;
int arr[50], sum=0;
cout<< “Enter the value of N : “;
cin>> n;
for(int i=0; i<n; i++)
{
cin>>arr[i];
sum = sum + arr[i];
}
cout<< “Sum = “ << sum << “& Average = “ << (float)sum/n;
return 0;
}

10. Read the heights of some students into an array and sort
the values in ascending order.
Program: Sort Array ( Heights)

#include <iostream>
using namespace std;
int main()

int n;

cout<< “Enter number of students: “;


cin>> n;

int h[ n ];

cout<<”Enter “<<n<<” heights :”;

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

cin>> h[ i ];

for(int i=0; i<n-1; i++)

for(int j=0; j<n-i-1; j++)


{

If (h[ j ] > h[ j+1 ])

int temp = h[ j ];

h[ j ] = h[ j+1 ];

h[ j+1 ] = temp;

}
}

cout<< “Sorted heights : “;

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

cout<< h[ i ] << “ “;

return 0;

}
11. Find the length of a string without using strlen() function.
Program: String length ( Without strlen() function)

#include <iostream>

using namespace std;

int main()

{
char str[100];

cout<< “Enter a string : “;

[Link](str, 100);

int len = 0;

while(str[ len ] != „\0‟)

len++;

cout<< “Length = “ <<len;


return 0;

12. Find the factorial of a number with the help of a user


defined function.
Program:Factorial ( User defined function)

#include <iostream>

using namespace std;


long int fact ( int n )

if (n <= 1)
return 1;
else

return n * fact (n-1);

int main()
{

int n;

cout<< “Enter a number : “;

cin>> n;

if (n < 0)

cout<<” Factorial is not defined for negative numbers “;

}
else

cout<< “Factorial = “ << fact(n);

return 0;

13. Define a structure to represent the details of employees


which consist of employee code, name of employee, basic pay,
DA and PF.
declare a variable using this structure and input employee
code, name and basic pay of an employee. Calculate DA as 29%
of basic
pay, PF as 10% of basic pay and Net Salary as basic pay +
DA-PF. Display the name of employee and Net Salary.
Program: Employee Structure
#include <iostream>

using namespace std;

struct Employee
{

int code;

char name[50];

float basic, DA, PF, net;

};

int main()

{
Employee e;

cout<< “Enter employee code, name and basic pay : “;

cin>>[Link]>>[Link]>>[Link];

[Link] = 0.29 * [Link];

[Link] = 0.10 * [Link];

[Link] = ([Link] + [Link]) – [Link];

cout<< “Employee : “ <<[Link]<< “\nNet Salary: “ << [Link];


return 0;

14. Create a structure to represent admission number, name


and marks given for CE, PE and TE of a subject. Input the details
of a student
and display admission number, name and total marks
obtained.
Program: Student Structure
#include <iostream>

using namespace std;

struct Student
{

Int admno;

char name[50];

int CE, PE, TE;

};

int main()

Student s;
cout<< “Enter admission no, name, CE, PE, TE marks: “;

cin>>[Link]>>[Link]>>[Link]>>[Link]>>[Link];

int total = [Link] + [Link] + [Link];

cout<< “Admission No: “ <<[Link]<< “\nName: “ <<[Link]<<


“\nTotal Marks: “ << total;

return 0;

15. Define a function using pointer arguments to swap the


values of two variables. Call this function to demonstrate its
purpose.
Program: Swap using Pointers

#include <iostream>
using namespace std;
void swap(int *a, int *b)

int temp = *a;

*a = *b;
*b = temp;

int main()

int x, y;

cout<< “Enter two numbers : “;

cin>> x >> y;

swap(&x, &y);
cout<< “After swap : x = “ << x << “, y = “ << y;

return 0;

16. Create a class to represent admission number, name and


marks given for CE, PE and TE of a subject. Input the details of a
student and display

admission number, name and total marks obtained.

Program: Student class example

#include <iostream>

using namespace std;


class Student

{
Int admno;
char name[50];

int CE, PE, TE;

Public:

void input ( )
{

cout<< “Enter admission number : “;

cin>>admno;

cout<< “Enter name : “;

cin>> name;

cout<< “Enter marks for CE, PE, TE : “;

cin>> CE >> PE >> TE;

void display( )

int total = CE + PE + TE;

cout<< “\nAdmission Number : “ <<admno;

cout<< “\nName: “ << name;

cout<< “\nTotal Marks : “ << total <<endl;


}

};

int main ( )

Student s;

[Link]();
[Link]();
return 0;

You might also like