Você está na página 1de 26

/* 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;
do
{
digit = num%10;
rev = (rev*10) + digit;
num = num/10;
}while (num!=0);
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;
}
Output

Enter a positive number: 12321

The reverse of the number is: 12321


The number is a palindrome

Enter a positive number: 12331


The reverse of the number is: 13321
The number is not a palindrome

/* C++ program to check whether a number entered by user is Armstrong or not. */

#include <iostream>
using namespace std;
int main()
{
int n, n1, rem, num=0;
cout << "Enter a positive integer: ";
cin >> n;
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
cout << n << " is an Armstrong number.";
else
cout << n << " is not an Armstrong number.";
return 0;
}

Output

Enter a positive integer: 371


371 is an Armstrong number.

/* C++ program to check Prime number. */


#include <iostream>
using namespace std;
int check_prime(int n);
int main()
{
int n, temp=0;
cout << "Enter a positive integer: ";
cin >> n;
temp=check_prime(n);

if(temp==0)
cout << n << " is a prime number.";
else
cout << n << " is not a prime number.";
return 0;
}
int check_prime(int n)
{
int i, flag=0;
for(i=2;i<=n/2;++i)
{
if(n%i==0)

{
flag=1;
break;
}
}
return flag;
}

Output

Enter a positive integer: 371


371 is an Armstrong number.

Prime Numbers Between two Intervals by Making User-defined Function

#include<iostream>
using namespace std;
int check_prime(int num);
int main(){
int n1,n2,i,flag;
cout << "Enter two numbers(intervals): ";
cin >> n1 >> n2;
cout << "Prime numbers between " << n1 << " and " << n2 << " are: " << endl;
for(i=n1+1;i<n2;++i)
{
flag=check_prime(i);
if(flag==0)
cout << i << endl;
}
return 0;

}
int check_prime(int num) /* User-defined function to check prime number*/
{
int j,flag=0;
for(j=2;j<=num/2;++j){
if(num%j==0){
flag=1;
break;
}
}
return flag;
}
Output

Enter two numbers(intervals): 10 30


Prime numbers between 10 and 30 are: 11 13 17 19 23 29

This program takes a positive integer from user and checks whether that number
can be expressed as the sum of two prime numbers. If that number can be
expressed as sum of two prime numbers then, that number is expressed as sum of
two prime numbers in output. To perform this task, a user-defined function is
created to check prime number.

#include <iostream>
using namespace std;
int check_prime(int n);
int main()
{
int n, i, flag=0;
cout << "Enter a positive integer: ";

cin >> n;
for(i=2; i<=n/2; ++i)
{
if (check_prime(i)==0)
{
if ( check_prime(n-i)==0)
{
cout << n << " = " << i << " + " << n-i << endl;
flag=1;
}

}
}
if (flag==0)
cout << n << " can't be expressed as sum of two prime numbers.";
return 0;
}
int check_prime(int n) /*check prime number*/
{
int i, flag=0;
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
return flag;
}

Output

Enter a positive integer: 34


34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17

Source Code to Make Simple Calculator in C++ programming

/* Source code to create a simple calculator for addition, subtraction, multiplication


and division using switch...case statement in C++ programming. */

# include <iostream>
using namespace std;
int main()
{
char o;
float num1,num2;
cout << "Enter operator either + or - or * or /: ";
cin >> o;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(o) {
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;

break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output

Enter operator either + or - or * or divide : Enter two operands:


3.4
8.4
3.4 - 8.4 = -5.0

Source code to Calculate H.C.F using recursion

/* Example to calculate GCD or HCF using recursive function. */

#include <iostream>
using namespace std;
int hcf(int n1, int n2);

int main()
{
int n1, n2;
cout << "Enter two positive integers: ";
cin >> n1 >> n2;
cout << "H.C.F of " << n1 << " & " << n2 << " is: " << hcf(n1, n2);
return 0;
}
int hcf(int n1, int n2)
{
if (n2!=0)
return hcf(n2, n1%n2);
else
return n1;
}
Output

Enter two positive integers: 366 60


H.C.F of 366 and 60 is: 6

Source Code to Calculated Sum using Recursion

#include<iostream>
using namespace std;
int add(int n);
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;

cout << "Sum = " << add(n);


return 0;
}
int add(int n)
{
if(n!=0)
return n+add(n-1); /* recursive call */
}

Output

Enter an positive integer: 10


Sum = 55

Source Code to Calculate Factorial Using Recursion

/* Source code to find factorial of a number. */

#include<iostream>
using namespace std;
int factorial(int n);
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}

int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}
Output

Enter an positive integer: 6


Factorial of 6 = 720

Source code to calculate power using recursion

/* Source Code to calculate power using recursive function */

#include <iostream>
using namespace std;
int power(int n1,int n2);
int main()
{
int base, exp;
cout << "Enter base number: ";
cin >> base;
cout << "Enter power number(positive integer): ";
cin >> exp;
cout << base << " ^ " << exp << " = " << power(base, exp);
return 0;
}
int power(int base,int exp)
{
if ( exp!=1 )

return (base*power(base,exp-1));
}
Output

Enter base number: 3


Enter power number(positive integer): 3
3^3 = 27

Source Code to Display Largest Element of an array

#include <iostream>
using namespace std;
int main(){
int i,n;
float arr[100];
cout << "Enter total number of elements: ";
cin >> n;
cout << endl;
while (n>100 || n<=0)
{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}
for(i=0;i<n;++i) /* Stores number entered by user. */
{
cout << "Enter Number " << i+1 << " : ";
cin >> arr[i];
}
for(i=1;i<n;++i) /* Loop to store largest number to arr[0] */

{
if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
arr[0]=arr[i];
}
cout << "Largest element = " << arr[0];
return 0;
}
Output

Enter total number of elements: 8

Enter Number 1: 23.4


Enter Number 2: -34.5
Enter Number 3: 50
Enter Number 4: 33.5
Enter Number 5: 55.5
Enter Number 6: 43.7
Enter Number 7: 5.7
Enter Number 8: -66.5

Largest element = 55.5


Source Code to Calculate Standard Deviation by Passing it to Function

/* Source code to calculate standard deviation. */

#include <iostream>
#include <cmath>
using namespace std;
float standard_deviation(float data[], int n);

int main()
{
int n, i;
float data[100];
cout << "Enter number of data: ";
cin >> n;
while (n>100 || n<=0)
{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number of data again: ";
cin >> n;
}
cout << "Enter elements: " << endl;
for(i=0; i<n; ++i)
cin >> data[i];
cout << endl;
cout << "Standard Deviation = " << standard_deviation(data,n);
return 0;
}
float standard_deviation(float data[], int n)
{
float mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);

return sqrt(sum_deviation/n);
}
Output

Enter number of datas: 4


Enter elements:
1
2
3
4

Standard Deviation = 1.11803

Source Code to Calculate Average of Numbers Using Arrays

#include <iostream>
using namespace std;
int main(){
int n, i;
float num[100], sum=0.0, average;
cout << "Enter the numbers of data: ";
cin >> n;
while (n>100 || n<=0)
{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}
for(i=0; i<n; ++i)

{
cout << i+1 << ". Enter number: ";
cin >> num[i];
sum+=num[i];
}
average=sum/n;
cout << "Average = " << average;
return 0;
}
Output

Enter the numbers of data: 6


1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

Source Code to Find the Frequency of Characters

#include <iostream>
#include <cstring>
using namespace std;
int main(){
char c[1000],ch;
int i,count=0;
cout << "Enter a string: ";

cin.getline(c, 1000);
cout << "Enter a character to find frequency: ";
cin >> ch;
for(i=0;c[i]!='\0';++i)
{
if(ch==c[i])
++count;
}
cout << "Frequency of " << ch << " = " << count;
return 0;
}
Output

Enter a string: i like programming


Enter a character to find frequency: i
Frequency of e = 3
Source Code to Calculate Length of a string without using strlen() Function

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[1000],i;
cout << "Enter a string: ";
cin.getline(s, 1000);
for(i=0; s[i]!='\0'; ++i);
cout << "Length of string: " << i;
return 0;
}

Output

Enter a string: Programiz


Length of string: 9

Source Code to Copy String Manually

#include <iostream>
using namespace std;
int main()
{
char s1[100], s2[100];
int i;
cout << "Enter string s1: ";
cin >> s1;
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout << "String s2: " << s2;
return 0;
}
Output

Enter String s1: programiz


String s2: programiz

Source Code to Remove Characters in String Except Alphabets

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char line[150];
int i,j;
cout << "Enter a string: ";
cin.getline(line, 150);
for(i=0; line[i]!='\0'; ++i)
{
while (!((line[i]>='a'&&line[i]<='z')
|| (line[i]>='A'&&line[i]<='Z'
|| line[i]=='\0')))
{
for(j=i;line[j]!='\0';++j)
{
line[j]=line[j+1];
}
line[j]='\0';
}
}
cout << "Output String: " << line;
return 0;
}
Output

Enter a string: p2'r"o@gram84iz./


Output String: programiz

Source Code to Find Number of Vowels, Consonants, Digits and White Spaces in a
String

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
cout << "Enter a line of string: " << endl;
cin.getline(line, 150);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a'
|| line[i]=='e'
|| line[i]=='i'
|| line[i]=='o'
|| line[i]=='u'
|| line[i]=='A'
|| line[i]=='E'
|| line[i]=='I'
|| line[i]=='O'
|| line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z')
|| (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;

else if (line[i]==' ')


++s;
}
cout << " Vowels: " << v << endl;
cout << " Consonants: " << c << endl;
cout << " Digits: " << d << endl;
cout << " White Spaces: " << d << endl;
return 0;
}
Output

Enter a line of string:


programming is fun 1 234
Vowels: 5
Consonants: 11
Digits: 4
White Spaces: 4

Source Code to Add Two Matrix in C++ programming

#include <iostream>
using namespace std;
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;

/* Storing elements of first matrix entered by user. */

for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element a" << i+1 << j+1 << " : ";
cin >> a[i][j];
}

/* Storing elements of second matrix entered by user. */

cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element b" << i+1 << j+1 << " : ";
cin >> b[i][j];
}

/*Adding Two matrices */

for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];

/* Displaying the resultant sum matrix. */

cout << endl << "Sum of two matrix is: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)

{
cout << sum[i][j] << " ";
if(j==c-1)
cout << endl;
}

return 0;
}
Output

Enter number of rows (between 1 and 100): 2


Enter number of columns (between 1 and 100): 2

Enter elements of 1st matrix:


Enter element a11: -4
Enter element a12: 5
Enter element a21: 6
Enter element a22: 8

Enter elements of 2nd matrix:


Enter element b11: 3
Enter element b12: -9
Enter element b21: 7
Enter element b22: 2

Sum of two matrix is:


-1 -4
13 6

Source Code to Find Transpose of a Matrix

#include <iostream>
using namespace std;
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;

/* Storing element of matrix entered by user in array a[][]. */


cout << endl << "Enter elements of matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout << "Enter elements a" << i+1 << j+1 << ": ";
cin >> a[i][j];
}
/* Displaying the matrix a[][] */
cout << endl << "Entered Matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout << " " << a[i][j];
if(j==c-1)
cout << endl << endl;
}

/* Finding transpose of matrix a[][] and storing it in array trans[][]. */


for(i=0; i<r; ++i)

for(j=0; j<c; ++j)


{
trans[j][i]=a[i][j];
}

/* Displaying the transpose,i.e, Displaying array trans[][]. */


cout << endl << "Transpose of Matrix: " << endl;
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
cout << " " << trans[i][j];
if(j==r-1)
cout << endl << endl;
}
return 0;
}
Output

Enter rows and column of matrix: 2


3

Enter elements of matrix:


Enter elements a11: 1
Enter elements a12: 2
Enter elements a13: 9
Enter elements a21: 0
Enter elements a22: 4
Enter elements a23: 7

Entered Matrix:

1 2 9

0 4 7

Transpose of Matrix:
1 0

2 4

9 7

Você também pode gostar