0% found this document useful (0 votes)
0 views10 pages

Program List

The document contains a list of C++ programs demonstrating various programming concepts, including swapping numbers, finding roots of quadratic equations, displaying Fibonacci series, reversing numbers, and checking for palindromes. Each program is accompanied by code snippets and sample outputs for clarity. Additionally, there are exercises for users to write their own C++ programs to solve specific problems.

Uploaded by

Purushotham C
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)
0 views10 pages

Program List

The document contains a list of C++ programs demonstrating various programming concepts, including swapping numbers, finding roots of quadratic equations, displaying Fibonacci series, reversing numbers, and checking for palindromes. Each program is accompanied by code snippets and sample outputs for clarity. Additionally, there are exercises for users to write their own C++ programs to solve specific problems.

Uploaded by

Purushotham C
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/ 10

// Example Program List

1. C++ Program to Swap Two Numbers

#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}

2. C++ Program to Find All Roots of a Quadratic Equation

#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}

else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}

return 0;
}
3. C++ Program to Display Fibonacci Series

#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
return 0;
}

4. C++ Program to Reverse a Number

#include <iostream>
using namespace std;
int main() {
int n, reversed_number = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0) {
remainder = n % 10;
reversed_number = reversed_number * 10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversed_number;
return 0;
}

5. C++ Program to Calculate Power of a Number

#include <iostream>
using namespace std;
int main()
{
int exponent;
float base, result = 1;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
cout << base << "^" << exponent << " = ";
while (exponent != 0) {
result = results * base;
--exponent;
}
cout << result;
return 0;
}
6. C++ Program to Check Whether a Number is Palindrome or Not

#include <iostream>
using namespace std;

int main()
{
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
while(num!=0)
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
}
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}

7. C++ Program to Display Factors of a Number

#include <iostream>
using namespace std;
int main() {
int n, i;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: ";
for(i = 1; i <= n; ++i) {
if(n % i == 0)
cout << i << " ";
}
return 0;
}

8. C++ Program to Check Whether a Number is Prime or Not

#include <iostream>
using namespace std;
int main() {
int i, n,f=0;
cout << "Enter a positive integer: ";
cin >> n;
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
f=1;
break;
}
}
if (f==0)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";
return 0;
}

Solution Link:https://www.programiz.com/cpp-programming/examples

1. Write a program in C++ to find the first 10 natural numbers.


Sample output:
The natural numbers are:
1 2 3 4 5 6 7 8 9 10

#include <iostream>
using namespace std;
int main()
{
int i;
cout << "\n\n Find the first 10 natural numbers:\n";
cout << "---------------------------------------\n";
cout << " The natural numbers are: \n";
for (i = 1; i <= 10; i++)
{
cout << i << " ";
}
return 0;
}

2. Write a program in C++ to find the sum of first 10 natural numbers.


Sample Output:
Find the first 10 natural numbers:
---------------------------------------
The natural numbers are:
1 2 3 4 5 6 7 8 9 10
The sum of first 10 natural numbers: 55

#include <iostream>
using namespace std;
int main()
{
int i,sum=0;
cout << "\n\n Find the first 10 natural numbers:\n";
cout << "---------------------------------------\n";
cout << " The natural numbers are: \n";
for (i = 1; i <= 10; i++)
{
cout << i << " ";
sum=sum+i;
}
cout << "\n The sum of first 10 natural numbers: "<<sum << endl;
return 0;
}

3. Write a program in C++ to find the sum of digits of a given number.


Sample Output:
Input a number: 1234
The sum of digits of 1234 is: 10

#include <iostream>
using namespace std;
int main()
{
int num1, num2, r, sum=0;
cout << "\n\n Find the sum of digits of a given number:\n";
cout << " Input a number: ";
cin >> num1;
num2 = num1;
while (num1 > 0)
{
r = num1 % 10;
num1 = num1 / 10;
sum = sum + r;
}
cout << " The sum of digits of " << num2 << " is: " << sum << endl;
return 0;
}

4. Write a program in C++ to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) +
(5*5) + ... + (n*n). Go to the editor
Sample Output:
Input the value for nth term: 5
1*1 = 1
2*2 = 4
3*3 = 9
4*4 = 16
5*5 = 25
The sum of the above series is: 55

#include <iostream>
using namespace std;
int main()
{
int i, n, sum = 0;
cout << "\n\n Find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... +
(n*n):\n";
cout << " Input the value for nth term: ";
cin >> n;
for (i = 1; i <= n; i++)
{
sum += i * i;
cout << i << "*" << i << " = " << i * i << endl;
}
cout << " The sum of the above series is: " << sum << endl;
return 0;
}

5. Write a program in C++ to print all ASCII character with their values. Go to the editor
Sample Output:
Input the starting value for ASCII characters: 65
Input the ending value for ASCII characters: 75
The ASCII characters:
65 --> A
66 --> B
67 --> C
68 --> D
69 --> E
70 --> F
71 --> G
72 --> H
73 --> I
74 --> J
75 --> K

#include <iostream>
using namespace std;
int main()
{
int sn, en, i, j, ctr, r;
cout << "\n\n Print ASCII character with their values:\n";
cout << " Input the starting value for ASCII characters: ";
cin >> sn;
cout << " Input the ending value for ASCII characters: ";
cin >> en;
if (sn>255 || sn<0)
sn=1;
if(en<0 || en>255)
en=255;
cout << "The ASCII characters:"<<endl ;
for (i = sn; i <=en; i++)
{
cout << i<<" --> "<<char(i)<<endl;
}
return 0;
}

6. Write a program in C++ to display the n terms of odd natural number and their sum.
Sample Output:
Input number of terms: 5
The odd numbers are: 1 3 5 7 9
The Sum of odd Natural Numbers upto 5 terms: 25

#include <iostream>
using namespace std;
int main()
{
int i, n, sum = 0;
cout << "\n\n Display n terms of odd natural number and their sum:\n";
cout << " Input number of terms: ";
cin >> n;
cout << " The odd numbers are: ";
for (i = 1; i <= n; i++)
{
cout << 2 * i - 1 << " ";
sum += 2 * i - 1;
}
cout << "\n The Sum of odd Natural Numbers upto " << n << " terms"<< sum << endl";
return 0;
}

7. Write a program in C++ to display the n terms of even natural number and their sum.
Sample Output:
Input number of terms: 5
The even numbers are: 2 4 6 8 10
The Sum of even Natural Numbers upto 5 terms: 30

#include <iostream>
using namespace std;
int main()
{
int i, n, sum = 0;
cout << "\n\n Display n terms of even natural number and their sum:\n";
cout << " Input number of terms: ";
cin >> n;
cout << "\n The even numbers are: ";
for (i = 1; i <= n; i++)
{
cout << 2 * i << " ";
sum += 2 * i ;
}
cout << "\n The Sum of even Natural Numbers upto " << n << " terms: " << sum << endl;
return 0;
}

8. Write a program in C++ to display the first n terms of Fibonacci series.


Sample Output:
Input number of terms to display: 10
Here is the Fibonacci series upto to 10 terms:
0 1 1 2 3 5 8 13 21 34

#include <iostream>
using namespace std;
int main()
{
int prv = 0, pre = 1, trm, i, n;
cout << "\n\n Display the first n terms of Fibonacci series:\n";
cout << " Input number of terms to display: ";
cin >> n;
cout << " Here is the Fibonacci series upto to " << n << " terms: "<<endl;
cout << prv << " " << pre;
for (i = 3; i <= n; i++)
{
trm = prv + pre;
cout << " " << trm;
prv = pre;
pre = trm;
}
cout << endl;
return 0;
}

9. Write a program in C++ to find the number and sum of all integer between 100 and
200 which are divisible by 9.
Sample Output:
Numbers between 100 and 200, divisible by 9:
108 117 126 135 144 153 162 171 180 189 198
The sum : 1683

#include <iostream>
using namespace std;

int main()
{
int i, sum = 0;
cout << "\n\n Find the number and sum of all integer between 100 and 200, divisible
by 9:\n";
cout << " Numbers between 100 and 200, divisible by 9: " << endl;
for (i = 101; i < 200; i++)
{
if (i % 9 == 0)
{
cout << " " << i;
sum += i;
}
}
cout << "\n The sum : " << sum << endl;
return 0;
}

10. Write a program in C++ to display the pattern like right angle triangle using an asterisk.
Go to the editor
Sample Output:
Input number of rows: 5
*
**
***
****
*****

#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << "\n\n display the pattern like right angle triangle using an asterisk:\n";
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
return 0;
}

11. Write a program in C++ to display the pattern like right angle triangle with number. Go
to the editor
Sample Output:
Input number of rows: 5
1
12
123
1234
12345

#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << "\n\n Display the pattern using number starting from 1:\n";
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<j;
cout<<endl;
}
return 0;
}

12. Write a program in C++ to make such a pattern like right angle triangle using number
which will repeat the number for that row. Go to the editor
Sample Output:
Input number of rows: 5
1
22
333
4444
55555

#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << "\n\n Display the pattern using number repeating for a row:\n";
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<i;
cout<<endl;
}
return 0;
}

13. Write a program in C++ to make such a pattern like right angle triangle with number
increased by 1. Go to the editor
Sample Output:
Input number of rows: 4
1
23
456
7 8 9 10

#include <iostream>
#include <string>
using namespace std;
int main()
{
int i,j,rows,k=1;
cout << "\n\n Display such a pattern like right angle triangle with number increased
by 1:\n";
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<k++<<" ";
cout<<endl;
}
return 0;
}

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