C++ Programming Questions and Solutions - Computer Notes | Computer Notes for 11 and 12 | PLK Computer Sir

Breaking

Home Top Ad

Post Top Ad

Responsive Ads Here

C++ Programming Questions and Solutions

1. WAP to display hello world in C++.

#include<iostream.h>

int main(){          

                cout<<"Hello World! "<<"\n";

                return 0;

}


2. WAP to take 3 numbers and calculate sum and average in C++.

#include<iostream.h>

int main(){          

                int n1,n2,n3,sum,avg;

                cout<<"Enter three numbers: "<<"\n";

                cin>>n1>>n2>>n3;

                sum=n1+n2+n3;

                avg=sum/3;

                cout<<"Sum = "<<sum<<"\n"<<"Average = "<<avg<<"\n";

                return 0;

}


3. Calculate area and circumference of circle in C++. Hint area=pi*r*r and cf=2*pi*r.

#include<iostream.h>

int main(){

float pi=3.14,r,area,circum;

cout<<"Please enter the value of radius: "<<"\n";

cin>>r;

area=pi*r*r;

circum=2*pi*r;

cout<<"Area of circle is: "<<area<<"\n";

cout<<"Circumference of circle is: "<<circum<<"\n";

return 0;

}


 

4. WAP to calculate simple interest in C++. Hint. Si = PTR/100

#include<iostream.h>

int main(){          

                float si,p,t,r;

                cout<<"Enter the value of principle,time and rate "<<"\n";

                cin>>p>>t>>r;

                si=(p*t*r)/100;

                cout<<"Simple Interest= "<<si<<"\n";

                return 0;

}

5. Write a program in C++ to check whether the given number is odd or even.

#include<iostream.h>

int main(){

int num,rem;

cout<<"Enter the number: "<<"\n";

cin>>num;

rem=num%2;

if(rem==0){

cout<<"The number "<<num<<" is even"<<"\n";

}else{

cout<<"The number "<<num<<" is odd"<<"\n";

}

return 0;

}

6. Write a C++ program to input a number and check whether it is positive, negative or zero.

#include<iostream.h>

int main(){

int num;

cout<<"Enter the number: "<<"\n";

cin>>num;

if(num==0){

cout<<"The number "<<num<<" is Zero"<<"\n";

}else if(num>0){

cout<<"The number "<<num<<" is Positive"<<"\n";

}else{

cout<<"The number "<<num<<" is Negative"<<"\n";

}

return 0;

}

7. Write a C++ program that stores cost price(cp) and selling price(sp) and determines whether there is loss or gain.

#include<iostream.h>

int main(){

int cp,sp;

cout<<"Enter the cost price and selling price: "<<"\n";

cin>>cp>>sp;

if(cp==sp){

cout<<"There is no loss or no gain"<<"\n";

}else if(cp>sp){

cout<<"There is loss by "<<cp-sp<<" amount"<<"\n";

}else{

cout<<"There is gain by "<<sp-cp<<" amount"<<"\n";

}

return 0;

}

8. Write a C++ program that stores three different numbers and display the largest among them.

#include<iostream.h>

int main(){

int num1, num2, num3;

cout<<"Enter three numbers: "<<"\n";

cin>>num1>>num2>>num3;

cout<< "Amount three numbers "<<num1<<","<<num2<<","<<num3<<"\n";

if(num1>num2&&num1>num3){

cout<<"Number "<<num1<<" is greater."<<"\n";

}else if(num2>num3){

cout<<"Number  "<<num2<<" is greater."<<"\n";

}else{

cout<<"Number "<<num3<<" is greater."<<"\n";

}

return 0;

}

9. Write a C++ program that stores three different numbers and display the smallest among them.

#include<iostream.h>

int main(){

int num1, num2, num3;

cout<<"Enter three numbers: "<<"\n";

cin>>num1>>num2>>num3;

cout<< "Amount three numbers "<<num1<<","<<num2<<","<<num3<<"\n";

if(num1<num2&&num1<num3){

cout<<"Number "<<num1<<" is smaller."<<"\n";

}else if(num2<num3){

cout<<"Number  "<<num2<<" is smaller."<<"\n";

}else{

cout<<"Number "<<num3<<" is smaller."<<"\n";

}

return 0;

}

10. Write a C++ program that stores three different numbers and display the middle number among them. [Hint: 30,5,10=> 10 is the middle number].

#include<iostream.h>

int main(){

int num1, num2, num3;

cout<<"Enter three numbers: "<<"\n";

cin>>num1>>num2>>num3;

cout<< "Amount three numbers "<<num1<<","<<num2<<","<<num3<<"\n";

if(num2>num1&&num1>num3||num3>num1&&num1>num2){

cout<<"Number "<<num1<<" is middle."<<"\n";

}else if(num1>num2&&num2>num3||num3>num2&&num2>num1){

cout<<"Number  "<<num2<<" is middle."<<"\n";

}else{

cout<<"Number "<<num3<<" is middle."<<"\n";

}

return 0;

}

WAP to find out whether the input number is even or odd?

#include<iostream.h>

int main(){

int num;

cout<<"Enter any number: "<<"\n";

cin>>num;

if(num%2==0){

cout<<"The number "<<num<<" is even."<<"\n";

}else{

cout<<"The number "<<num<<" is odd."<<"\n";

}

return 0;

}

WAP that checks whether the number entered by user is exactly divisible by 5 but not by 11?

#include<iostream.h>

int main(){

int num;

cout<<"Enter any number: "<<"\n";

cin>>num;

if(num%5==0&&num%11!=0){

cout<<"The number "<<num<<" is exactly divisible by 5 but not by 11."<<"\n";

}else{

cout<<"Invalid! You entered number "<<num<<" does not provide the required result of this program."<<"\n";

}

return 0;

}

WAP in c++ to display day of weeks while entering between 1 to 7.

#include<iostream.h>

int main(){

int n;

cout<<"Please enter between 1 to 7: ";

cin>>n;

switch(n){

case 1:

cout<<"Sunday";

cout<<"\n";

break;

case 2:

cout<<"Monday";

cout<<"\n";

break;

case 3:

cout<<"Tuesday";

cout<<"\n";

break;

case 4:

cout<<"Wednesday";

cout<<"\n";

break;

case 5:

cout<<"Thursday";

cout<<"\n";

break;

case 6:

cout<<"Friday";

cout<<"\n";

break;

case 7:

cout<<"Saturday";

cout<<"\n";

break;

default:

cout<<"Invalid! Please enter between 1 to 7";

cout<<"\n";

break;

}

return 0;

}

WAP in c++ to display 1,2,3,5 using continue statement
#include<iostream.h>
int main(){
int n;
for(n=1;n<=5;n++){
if(n==4){
continue;
}
cout<<n<<"\t";
}
cout<<"\n";
return 0;
}

WAP in c++ to display following pattern

/*

*****

****

***

**

*

*/


#include<iostream.h>

int main(){

int i,j;

for(i=1;i<=5;i++){

for(j=i;j<=5;j++){

cout<<"*";

}

cout<<"\n";

}

return 0;

}

WAP in c++ to display following pattern

/*

*

**

***

****

*/


#include<iostream.h>

int main(){

int i,j;

for(i=1;i<=5;i++){

for(j=1;j<i;j++){

cout<<"*";

}

cout<<"\n";

}

return 0;

}

WAP in c++ to display following pattern

/*

       *

     **

   ***

 ****

*/

#include<iostream.h>

int main(){

int i,j,k,m=1,n;

cout<<"Please enter a number of rows: ";

cin>>n;

for(i=n;i>=1;i--){

for(j=1;j<=i-1;j++){

cout<<" ";

}

for(k=1;k<=m;k++){

cout<<"*";

}

cout<<"\n";

m++;

}

return 0;

}

//WAP in c++ to display following pattern

/*

 *****

   ****

     ***

       **

         *

*/

#include<iostream.h>

int main(){

int i,j,k,m,n;

cout<<"Please enter a number of rows: ";

cin>>n;

m=n;

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

for(j=1;j<i;j++){

cout<<" ";

}

for(k=1;k<=m;k++){

cout<<"*";

}

m--;

cout<<"\n";

}

return 0;

}

WAP in c++ to display following pattern

/*

            *

          * *

        * * * 

      * * * *

   * * *  *  *

*/

#include<iostream.h>

int main(){

int i,j,k,m,n;

cout<<"Please enter a number of rows: ";

cin>>n;

m=n;

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

for(j=1;j<=m-1;j++){

cout<<" ";

}

for(k=1;k<=2*i-1;k++){

cout<<"*";

}

m--;

cout<<"\n";

}

return 0;

}

WAP in c++ to display following pattern

/*

     * * * * *

      * * * *

       * * *

        * *

         *

*/

#include<iostream.h>

int main(){

int i,j,k,m,n;

cout<<"Please enter a number of rows: ";

cin>>n;

m=n;

for(i=n;i>=1;i--){

for(j=1;j<m;j++){

cout<<" ";

}

for(k=1;k<=2*i-1;k++){

cout<<"*";

}

m++;

cout<<"\n";

}

return 0;

}

WAP in c++ to display following pattern

/*

*

* *

*   *

*     *

*       *

*******

*/

#include<iostream.h>

int main(){

int i,j,n;

cout<<"Please enter a number of rows: ";

cin>>n;

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

for(j=1;j<=i;j++){

if(j==1||i==j||i==n){

cout<<"*";

}else{

cout<<" ";

}

}

cout<<"\n";

}

return 0;

}

WAP in c++ to display even numbers between 1 to 100 using while.
#include<iostream.h>
int main(){
int n=1;
while(n<=100){
if(n%2==0){
cout<<n<<"\t";
}
n++;
}
cout<<"\n";
return 0;
}

WAP in c++ to display odd numbers between 1 to 100 using do..while.
#include<iostream.h>
int main(){
int n=1;
do{
if(n%2!=0){
cout<<n<<"\t";
}
n++;
}while(n<=100);
cout<<"\n";
return 0;
}

Functions

WAP in c++ to display greatest numbers among three numbers entered by user using function int greatest(int,int,int).

#include<iostream.h>

int greatest(int,int,int);

int main(){

int n1,n2,n3,grt;

cout<<"Enter three numbers to check greatest: ";

cin>>n1>>n2>>n3;

grt=greatest(n1,n2,n3);

cout<<"Greatest number is: "<<grt<<"\n";

return 0;

}

int greatest(int num1,int num2, int num3){

if(num1>num2&&num1>num3){

return num1;

}

else if(num2>num1&&num2>num3){

return num2;

}

else{

return num3;

}

}

WAP to find out whether the input number is even or odd using function?
#include<iostream.h>
void evenodd(int);
int main(){
int num;
cout<<"Enter any number: "<<"\n";
cin>>num;
evenodd(num);
return 0;
}
void evenodd(int n){
if(n%2==0){
cout<<"The number "<<n<<" is even."<<"\n";
}else{
cout<<"The number "<<n<<" is odd."<<"\n";
}
}

WAP that checks whether the number entered by user is exactly divisible by 5 but not by 11 using function?

#include<iostream.h>

void checknum(void);

int main(){

checknum();

return 0;

}

void checknum(void){

int num;

cout<<"Enter any number: "<<"\n";

cin>>num;

if(num%5==0&&num%11!=0){

cout<<"The number "<<num<<" is exactly divisible by 5 but not by 11."<<"\n";

}else{

cout<<"Invalid! You entered number "<<num<<" does not provide the required result of this program."<<"\n";

}

}

WAP that enter SP and CP and determine whether there is profit of loss using function?

#include<iostream.h>

void profit(int);

void loss(int);

int main(){

int sp,cp,p,l;

cout<<"Enter cost price and selling price : "<<"\n";

cin>>cp>>sp;

p=sp-cp;

l=cp-sp;

if((sp-cp)>0){

profit(p);

}else{

loss(l);

}

return 0;

}

void profit(int p){

cout<<"Profit is : "<<p<<"\n";

}

void loss(int l){

cout<<"Loss is : "<<l<<"\n";

}







No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here

Pages