Вы находитесь на странице: 1из 20

1. Write a program that give the sum of the digits of a three digit number.

int main()
{
int n,sum=0;
cout<<" enter a three digit number"<<endl;
cin>>n;
while(n!=0)
{
sum=sum+n%10;
n=n/10;

}
cout<<sum<<" is the sum of digit of the number"<<endl;
return 0;
}
2. Write a program for swapping where a=5 and b=10.
int main()
{
int a=5,b=10,temp;
cout<<" Before Swapping "<<endl;
cout<<" a = "<<a<<" and b ="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<" After Swapping "<<endl;
cout<<" a = "<<a<<" and b ="<<b<<endl;
}
3. Write a program to separate the digits of a three digit number and find sum of cubes of these digits.
int main()
{
int a,b,c,s,n;
cout<<" Enter values of three-digit number n "<<endl;
cin>>n;
a=n/100;
b=((n%100)/10);
c=n%10;
s=a*a*a+b*b*b+c*c*c;
cout<< "sum =" <<s<<endl;
}
4. Write a program that will take number from user and check that number is palindrome or not.
int main()
{
int a,n,digit,rev=0;
cout<<" enter the number to check"<<endl;
cin>>a;
n=a;
while(a!=0)
{
digit=a%10;
rev=(rev*10)+digit;
a/=10;
}
if(n==rev)
{
cout<<" the number is pelindrome "<<endl;
}
else
cout<<" the number is not pelindrome "<<endl;
}
5. Write a program that takes a number form user and check it is even or odd.
int main()
{
int a;
cout<<" enter the number to check"<<endl;
cin>>a;
if(a%2==0)
{
cout<< " the number is even "<<endl;
}
else
cout<<" the number is odd "<<endl;
}

6. Write a program that take character from user and check it is vowel or consonant.
int main()
{
char x;
cout<<" enter the character to check"<<endl;
cin>>x;
if(x=='a' || x=='e' || x=='i' || x=='o' || x=='u' && x=='A' || x=='E' || x=='I' || x=='O' || x=='U')
{
cout<< " the character is vowel "<<endl;
}
else
cout<<" the charcter is consonant "<<endl;
}
7. Write a program to check leap year.
int main()
{
int year;
cout<<" enter a year "<<endl;
cin>>year;
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
{
cout<<"its a leap year "<<endl;
}
else
cout<<" its not a leap year "<<endl;
}
else
cout<<" its a leap year"<<endl;
}
else
cout<<" its not a leap year "<<endl;
}
8. Write a program that takes input from user and this program will prompt you that number you enters is a
number or character.
int main()
{
char x;
cout<<" enter to check wheter its number or character "<<endl;
cin>>x;
if(x>='a' && x<='z' || x>='A' && x<='Z')
{
cout<<" its a character "<<endl;
}
else
cout<<" its a number "<<endl;
}
9. Write a program that takes three number and find largest number using nested if statement.
int main()
{
int a,b,c;
cout<<" enter the values of a,b and c respectively "<<endl;
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
cout<<a<<" is the largest number "<<endl;
}
else
cout<<c<<" is the largest number "<<endl;
}
else
if(b>c)
{
cout<<b<<" is the largest number "<<endl;
}
else
cout<<c<<" is the largest number "<<endl;
}
10. Write a program that will take your age as input and your program will tell whether you are eligible for
voting or not.
int main()
{
int age;
cout<<" enter your age "<<endl;
cin>>age;
if(age>=18)
{
cout<<" you can cast vote "<<endl;
}
else
cout<<" you can't cast vote "<<endl;
}
11. Write a program that will check character input is uppercase or lowercase.
int main()
{
char str[50] = "C++ Programming is awesome";
cout << "String Length = " << strlen(str);
return 0;
}
12. Write a program that will take 3 coefficients of quadratic equation and will tell roots are imaginary or not.
int main()
{
int a,b,c,disc;
cout<<" enter values of b,a and c respectively "<<endl;
cin>>b>>a>>c;
disc=pow(b,2)-4*a*c;
if(disc>=0)
{
cout<<" roots are real"<<endl;
}
if(disc<0)
{
cout<<" roots are imaginary"<<endl;
}
}
13. Write a program that input electricity unit charger and calculate bill.
#include <iostream>

using namespace std;

int main()
{
int a;
cout<< "Enter units consumed";
cin>>a;
cout<<"Your bill is:";
if(a>0&&a<=50)
{
a=a*2;
cout<<a;
}
else if(a>50&&a<=100)
{
a=a*4;
cout<<a;
}
else if(a>100)
{
a=a*6;
cout<<a;
}
else
cout<< "Invalid comand";

return 0;
}
14. Write a program using for loop to print your name for 10 times.
int main()
{
int i;
for(i=0;i<=9;i++)
cout<<"ZEESHAN \n";
return 0;
}
15. Write a program to print out on the screen. Following pattern
*
**
***
*****
int main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=0;j<=i;j++)
cout<< "*";
cout<<endl;
}
return 0;
}
16. Write a program to print following:
****
***
**
*
int main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=i;j<=3;j++)
cout<< "*";
cout<<endl;
}
return 0;
}
17. Write a program to print following on screen.
*
* * *
* * * * * * *
* * *
*

int main()
{
int i,j,r;
cout << " Input number of rows (half of the diamond):";
cin >> r;
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";
cout<<endl;
}
for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";
cout<<endl;;
}
return 0;
}
18. Write a program that will printout the elements of array and the sum of these elements . the array will
contain 5 elements.
#include <iostream>
using namespace std;
int main()
{
int i[5],j=0,r;
cout<< "Enter the numbers";
for(r=0;r<=4;r++)
cin>>i[r];
cout<< "Elements are:";
for(r=0;r<=4;r++)
cout<<i[r];
cout<<endl;
for(r=0;r<=4;r++)
j=j+i[r];
cout<< "And their sum is"<<j<<endl;
return 0;
}

19. Write a program that will take 5 values from user in integer array and your program will find largest number
of all.
int main()
{
int a[5],i,max;
cout<<"enter the values"<<endl;;
for(i=0;i<=4;i++)
{
cin>>a[i];
}

max=a[0];
for(i=0;i<=4;i++)
{
if(max<=a[i])
max=a[i];
}
cout<<"max value="<<max<<endl;
}
20. Write a program that takes 5 values form user in a integer array and when user enters a value to search in
an array your program will show location of that number in array if it found in array.
int main()
{
int a[5],n,i,p;
cout<<" enter the elements of array "<<endl;
for(i=0;i<=4;i++)
{
cin>>a[i];
}
p=0;
cout<<" enter the integer to find "<<endl;
cin>>n;
for(i=0;i<=4;i++)
{
if(n==a[i])
{
p=i+1;
break;
}
}
if(p==0)
{
cout<<" number not found "<<endl;
}
else
cout<<" the number found at location "<<p<<endl;
}
21. Write a program that will take 10 number from user in an array . your program will store only even numbers
in array.
22. Write a program that take 4 values from user in array and program will show table of the entered number.
int main()
{
int a[4],i;
cout<<" enter the elements of array "<<endl;
for(i=1;i<=4;i++)
{
cin>>a[i];
}
cout<<" the tabular form of the elements is "<<endl;
for(i=1;i<=4;i++)
{
cout<<a[i]<<endl;
}

23. Write a program that will take values form user and calculate the average of these values but if the user
enters 0 than program should average.
24. Write a program that will display the entered number in reverse order the number can be of any
length.
int main()
{
int a[5], i;
cout<<" enter the elements of array "<<endl;
for(i=1;i<=5;i++)
{
cin>>a[i];
}
cout<<" the elements in the reverse order is "<<endl;
for(i=5;i>=1;i--)
{
cout<<a[i]<<endl;
}
}
25. Write a program that will calculate standard deviation of 5 numbers.
{
float a,b,c,d,e,variance,deviation,exponent,base;
cout<<" enter the value of a,b,c,d and e "<<endl;
cin>>a>>b>>c>>d>>e;
variance=(a*a+b*b+c*c+d*d+e*e)/5;
base=variance;
exponent=0.5;
deviation=pow(base,exponent);
cout<<" the deviation is "<<deviation<<endl;
}

26. Write a program that takes 10 values in 2 arrays and sum of both arrays and show the result in 3 rd array.
int main()
{
int i,a[5],b[5],c[5];
cout<<" Enter elements of first array"<<endl;
for(i=0;i<=4;i++)
{
cin>>a[i];
}
cout<<" Enter elements of 2nd array"<<endl;
for(i=0;i<=4;i++)
{
cin>>b[i];
}
cout<< " 1st + 2nd = sum"<<endl;
for(i=0;i<=4;i++)
{
c[i]=a[i]+b[i];
}
for(i=0;i<=4;i++)
cout<<a[i]<<" + "<<b[i]<<" = "<<c[i]<<endl;
}
27. Write a program that takes value from user and program will find Factors of that number using do while
loop.
int main()
{
int number,temp;
temp=1;
cout << "Enter the number to determine its factors : " << endl;
cin >> number;
do
{
if (not(number % temp))
cout << "The factors of " << number << " are : " <<temp<<endl;
temp++;
}
while (temp <= number);
}
28. Write a program that take 10 values form user and your program will display array in reverse order.
int main()
{
int a[5], i;
cout<<" enter the elements of array "<<endl;
for(i=1;i<=5;i++)
{
cin>>a[i];
}
cout<<" the elements in the reverse order is "<<endl;
for(i=5;i>=1;i--)
{
cout<<a[i]<<endl;
}
}

29. Write a program that will sort the array in descending order by using bubble sorting. The number should get
from user.
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";

for(i=0;i<n;++i)
cin>>a[i];

for(i=0;i<n;++i)
{
for(j=i+1;j<n;++j)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

cout<<"Array after bubble sort:";


for(i=0;i<n;++i)
cout<<" "<<a[i];

return 0;
}
30. Write a program that takes values in 2D array and your program will find maximum number.
int main()
{
int a[2][3],i,j,max;
cout<<"enter the values\n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
cin>>a[i][j];
}
}
max=a[0][0];
for(i=0;i<=1;i++)
for(j=0;j<=2;j++)
{
if(max<=a[i][j])
max=a[i][j];
}
cout<<"max value="<<max<<endl;

31. Write a program to find transpose of a matrix. Matrix should be entered by the user.
int main()
{
int r,c,i,j;
int a[50][50],b[50][50];
cout<< "Enter the number of rows of Matrix: ";
cin>>r;
cout<<endl;
cout<< "Enter the number of columns of Matrix: ";
cin>>c;
cout<<endl;
cout<< "Enter the entries of matrix\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
}
cout<< "The Transpose of Entered Matrix is: \n";
for(j=0;j<r;j++)
{
for(i=0;i<c;i++)
{
cout<< b[j][i];
}
}
return 0;
}

32. Write a program to input data into two different arrays and your program first check whether they
can be multiplied. If yes your program will multiple them and display the result, otherwise your
program will show that multiplication not possible
int main ()
{
int r1, c1, r2, c2, i, j, k;
int A[5][5], B[5][5], C[5][5];
cout << "Enter number of rows and columns of matrix A"<<endl;
cin >> r1 >> c1;
cout << "Enter number of rows and columns of matrix B"<<endl;
cin >> r2 >> c2;
if (c1 != r2)
{
cout << "Matrices cannot be multiplied!";
exit(0);
}
cout << "Enter elements of matrix A"<<endl;
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
cin >> A[i][j];
cout << "Enter elements of matrix B"<<endl;
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
cin >> B[i][j];
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
C[i][j] = 0;
for (k = 0; k < r2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Product of matrices\n";
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
cout << C[i][j] << " ";

}
return 0;
}

33. Write a program for addition of 2 matrices. Matrices should be entered by the user.
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

cout << "Enter the number of rows and columns of matrix "<<endl;
cin >> m >> n;
cout << "Enter the elements of first matrix\n";

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


for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];

cout << "Enter the elements of second matrix\n";

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


for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];

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


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

cout << "Sum of entered matrices:-\n";

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


{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";

cout << endl;


}

return 0;
}
34. Write a Program to find determinant of 2x2 Matrix entered by the user.
int main()
{
int a[2][2],i,j,k,l,m;
cout<< "Enter the Matrix entries:\n";
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>a[i][j];
k=a[0][0]*a[1][1];
l=a[0][1]*a[1][0];
m=k-l;
cout<< "The determinent is:"<<m;
return 0;
}

35. Write a program which perform the task of “Strcpy” without using built in function.
#include <string.h>
using namespace std;
int main()
{
char s1[100], s2[100];
cout << "Enter string s1: ";
cin.getline(s1, 100);
strcpy(s2, s1);
cout << "s1 = "<< s1 << endl;
cout << "s2 = "<< s2;
return 0;
}
36. Write a program which perform the task of “Strlen” without using built in function.
int main()
{
char str[50] = "C++ Programming is awesome";
cout << "String Length = " << strlen(str);
return 0;
}
37. Write a program which perform the task of “Strcmp” without using built in function.
#include<string.h>
using namespace std;

int main()
{

char str1[100], str2[100];


cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
if(strcmp(str1, str2)==0)
{
cout<<"Both the strings are equal";
}
else
{
cout<<"Both the strings are not equal";
}
}

38. Write a program which perform the task of “Strcat” without using built in function.
#include<string.h>
using namespace std;
int main()
{
char dest[50] = "This is an";
char src[50] = " example";
strcat(dest, src);
cout << dest;
return 0;
}
39. Write a program to check frequency of letter in a string. The string should be entered by the user and the
character whose frequency is to check is also entered by the user.
int main()
{
string str = "C++ Programming is awesome";
char checkCharacter = 'm';
int count = 0;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == checkCharacter)
{
++ count;
}
}
cout << "Number of " << checkCharacter << " = " << count;
return 0;
}
40. Write a program to reverse a String the string should be set using getline() function.
void reverse(const string& a);
int main()
{
string str;
cout << " Please enter a string " << endl;
getline(cin, str);
reverse(str);
return 0;
}
void reverse(const string& str)
{
size_t numOfChars = str.size();
if(numOfChars == 1)
cout << str << endl;
else
{
cout << str[numOfChars - 1];
reverse(str.substr(0, numOfChars - 1));
}
}
41. Write a program using function to print table of a number.
int main()

{
void tab(int);
int n;
cout<<" enter any number"<<endl;
cin>>n;
tab(n);
}
void tab(int x)
{
int c;
for(c=1;c<=10;c++)
{
cout<<c<<" * "<<x<<" = "<<c*x<<endl;
}
}
42. Write a program in which we pass 2 values and sum is display in main body.
int main()

{
void sum(int,int);
int a,b;
cout<<" enter values of a and b"<<endl;
cin>>a>>b;
sum(a,b);
}
void sum(int x,int y)
{
int s;
s=x+y;
cout<<x<<" + "<<y<<" = "<<s<<endl;
}

43. Write a function that finds the area of the rectangle on providing length and width. Get Length &
width from user in main () Call the function area () Calculate the length and return the area Display
the result in main ().
int main()

{
void area(int,int);
int l,w;
cout<<" enter the length and width of triangle"<<endl;
cin>>l>>w;
area(l,w);
}
void area(int x,int y)
{
int a;
a=x*y;
cout<<a<<" sq units is the area of the reactangle with length "<<x<<" and width
"<<y<<endl;
}
44. write a program for checking the entered number is prime or not.
int main()
{
int n;
cout<<" enter the number to check "<<endl;
cin>>n;
if(n%2==1)
{
cout<<" its a prime number "<<endl;
}
else
cout<<" its not a prime number "<<endl;
45. Write a program in C++ that takes a starting number and an ending number and you program will
display the primes numbers between those entered numbers using do while loop.
int main()
{
int low, high, i, flag;
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
while (low < high)
{
flag = 0;
for(i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
cout << low << " ";
++low;
}
return 0;
}

46. Write a program for multiplication of matrices.


int main ()
{
int r1, c1, r2, c2, i, j, k;
int A[5][5], B[5][5], C[5][5];
cout << "Enter number of rows and columns of matrix A"<<endl;
cin >> r1 >> c1;
cout << "Enter number of rows and columns of matrix B"<<endl;
cin >> r2 >> c2;
if (c1 != r2)
{
cout << "Matrices cannot be multiplied!";
exit(0);
}
cout << "Enter elements of matrix A"<<endl;
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
cin >> A[i][j];
cout << "Enter elements of matrix B"<<endl;
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
cin >> B[i][j];
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
C[i][j] = 0;
for (k = 0; k < r2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Product of matrices\n";
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
cout << C[i][j] << " ";

}
return 0;
}
47. Write a program that count no of words, spaces in a sentence which is entered by user in string variable or
character array.
#include<string.h>
using namespace std;
int main ()
{
char str[50];
int count = 0, i;
cout << "Enter a string : ";
gets(str);
for (i = 0; str[i] != '\0';i++)
{
if (str[i] == ' ')
count++;
}
cout << "Number of words in the string are: " << count + 1<<endl;
cout<<" Number of spaces in the string are: "<<count;
return 0;
}
48. Write a program to check the number entered by the user is Armstrong number or not.
int main()
{
int n,num,rem,sum=0;
cout<<" enter the number to check "<<endl;
cin>>num;
n=num;
while(n!=0)
{
rem=n%10;
sum+=rem*rem*rem;
n/=10;
}
if(sum==num)
{
cout<<num<<" is an armstrong number "<<endl;
}
else
cout<<num<<" is not an armstrong number "<<endl;
}
49. Wirte a program to display following menu:
1= Print table
2= Print Factorial
3= Quit
Program will ask to enter your choice if user entered number other then menu invalid should be displayed if
the number matched the corresponding function is called and perform the task. This program should be
written using functions.
50. Write a program in C++ that take a single character from the user, and tells it's a Small Letter or it's
a CAPITAL letter using nested if statement only.
int main()
{
char a;
cout<<" enter a charcter to check"<<endl;
cin>>a;
if(a>='a')
{
if(a<='z')
cout<<" the character you entered is lowercase"<<endl;
}
else
cout<<" the character you entered is uppercase"<<endl;
}
51. Write a Program that display numbers from 1-20 using for loop.
1,2,3,4,5,6,7.8.9………………..20
main()
{
int i;
for(i=1;i<=20;i++)
{
cout<<i<<",";
}
}
52. Write a program in C++ that calculate the factorial of user defined number, using for loop.
int main()
{
int n;
long int fact;
cout<<" enter a number "<<endl;
cin>>n;
fact=1;
for(;n>=1;n--)
{
fact=fact*n;

}
cout<<" the factorial is "<<fact;
}
53. Write a program in C++ to find the factorial of number using while loop. The number must be user
defined.
int main()
{
int n;
long int fact;
cout<<" enter a number "<<endl;
cin>>n;
fact=1;
while(n>=1)
{

fact=fact*n;
n--;

}
cout<<" the factorial is "<<fact;
}
54. Write a program that will display of the ASCII code of the character entered by the user if user enter “Q” the
program should terminate else it will display the ACSII as user enter the any character.
int main()
{
char alp;
cout<< "Enter the character:\t";
cin>>alp;
if(alp!='q'&&alp!='Q')
{
cout<< "ASCII Code of \t"<<alp<<" is\t";
cout<<int(alp);
}
//int(alp) prints the respective ASCII Code of that alphabat stored in Alp.
else
cout<< "Terminated";
return 0;
}

55. Write a program that will find the size of array you can initialize the array in the program with
different numbers.
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
int size = *(&arr + 1) - arr;
cout << "Number of elements in arr[] is "
<< size;
return 0;
}
56. Write a program that will find the GCD of the two numbers that are entered by the user.
int main()
{
int num1, num2, gcd,i;
cout << "Find the Greatest Common Divisor of two numbers:\n";
cout << " Input the first number: ";
cin >> num1;
cout << " Input the second number: ";
cin >> num2;

for (i = 1; i <= num1 && i <= num2; i++)


{
if (num1 % i == 0 && num2 % i == 0)
{
gcd = i;
}
}
cout << " The Greatest Common Divisor is: " << gcd << endl;
return 0;
}

Вам также может понравиться