Escolar Documentos
Profissional Documentos
Cultura Documentos
#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
#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
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
#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
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
# 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
#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
#include<iostream>
using namespace std;
int add(int n);
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
Output
#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
#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
#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
#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
#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
#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
#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
#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
#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
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;
#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;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element a" << i+1 << j+1 << " : ";
cin >> a[i][j];
}
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];
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
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
#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;
Entered Matrix:
1 2 9
0 4 7
Transpose of Matrix:
1 0
2 4
9 7