single and multiple cout statements.

Q20. Write a program that displays on screen

  1. the character ‘z’
  2. the name ‘Mohan’
  3. the number 1977

Using

  1. single cout statement
  2. multiple cout statements.

Ans 1.

#include<iostream.h>

#include<conio.h>   //for clrscr()

void main()

{

clrscr();    //for clear screen

cout<<“\n z \n Mohan \n 1977”;

}

Output:

z

Mohan

1977

Ans 2.

#include<iostream.h>

#include<conio.h>   //for clrscr()

void main()

{

clrscr();  // for clear screen

cout<<“\n z”;

cout<<“\n Mohan”;

cout<<“\n 1977”;

}

Output:

z

Mohan

1977

Program Display Of Value P.

Q18. Write a program to read values of w, x, y and z and display the value of P, where

P= w+x / y-z

Ans.

#include<iostream.h>

#include<conio.h>   //for clrscr()

void main()

{

int w,x,y,z,P;

clrscr();          //for clear screen

cout<<“\n Enter the value of w: “;

cin>>w;

cout<<“\n Enter the value of x: “;

cin>>x;

cout<<“\n Enter the value of y: “;

cin>>y;

cout<<“\n Enter the value of z: “;

cin>>z;

P=(w+x)/(y-z);

cout<<“\n The value of P is: “<<P;

}

Output

 Enter the value of w: 5

Enter the value of x: 5

Enter the value of y: 20

Enter the value of z: 10

The value of P is: 1

Function Main().

Q32. Why is function main() special? What would happen if main() is not present in the program?

Ans. Every C++ program must have a function named main(). The program execution begins at main(). So, if function main() is missing then there shall be no execution of the statements.

Write a C++ program to accept percentage and print division accordingly.

Q22. Write a C++ program to accept percentage and print division accordingly.

Solution:

#include<iostream.h> //standard input/output header file

#include<conio.h> //for clrscr()

 

int main()

{

int per;

 

clrscr(); //for clearing the screen

 

cout<<“\n Enter the percentage: “;

cin>>per;

 

per>=60? cout<<“\n I Division.” : per<60&&per>=45? cout<<“\n II Division.” :per>=36? cout<<“\n III Division.” : cout<<“\n Fail.”;

 

getch(); //holds the screen until any key is pressed

return 0; //makes main() finish

}

 

OUTPUT:

CASE 1:

Enter the percentage: 78

 

I Division.

 

CASE 2:

Enter the percentage: 57

 

II Division.

 

CASE 3:

Enter the percentage: 33

 

Write a C++ program to accept a number and if it is less than 50 and greater than 20 than print its square value.

Q21. Write a C++ program to accept a number and if it is less than 50 and greater than 20 than print its square value.

Solution:

#include<iostream.h> //standard input/output header file

#include<conio.h> //for clrscr()

 

int main()

{

int a;

 

clrscr(); //for clearing the screen

 

cout<<“\n Enter a number: “;

cin>>a;

 

a<50&&a>20? cout<<“\n The square of number is “<<a*a<<“.” : cout<<“\n The number is “<<a<<“.”;

 

getch(); //holds the screen until any key is pressed

return 0; //makes main() finish

}

 

 

OUTPUT:

CASE 1:

Enter a number: 45

 

The square of number is 2025.

 

CASE 2:

Enter a number: 78

 

The number is 78.

Write a C++ program to accept a number and if it is less than 10 then print its square value.

Q20. Write a C++ program to accept a number and if it is less than 10 then print its square value.

Solution:

#include<iostream.h> //standard input/output header file

#include<conio.h> //for clrscr()

 

int main()

{

int a;

 

clrscr(); //for clearing the screen

 

cout<<“\n Enter a number: “;

cin>>a;

 

a<10? cout<<“\n The square of number is “<<a*a<<“.” : cout<<“\n The number is “<<a<<“.”;

 

getch(); //holds the screen until any key is pressed

return 0; //makes main() finish

}

 

OUTPUT:

CASE 1:

 

Enter a number: 9

 

The square of number is 81.

 

CASE 2:

Enter a number: 11

 

The number is 11.

 

Write a C++ program to accept a number and determine whether it is even or odd.

Q19. Write a C++ program to accept a number and determine whether it is even or odd.

Solution:

#include<iostream.h> //standard input/output header file

#include<conio.h> //for clrscr()

 

int main()

{

int a;

 

clrscr(); //for clearing the screen

 

cout<<“\n Enter a number: “;

cin>>a;

 

a%2==0? cout<<“\n It is an even number.” : cout<<“\n It is an odd number.”;

 

getch(); //holds the screen until any key is pressed

return 0; //makes main() finish

}

 

OUTPUT:

CASE 1:

Enter a number: 7

 

It is an odd number.

 

CASE 2:

Enter a number: 6

 

It is an even number.

Write a C++ program to accept a character and determine whether its number, alphabet or symbol.

Q18. Write a C++ program to accept a character and determine whether its number, alphabet or symbol.

Solution:

#include<iostream.h> //standard input/output header file

#include<conio.h> //for clrscr()

 

int main()

{

char a;

 

clrscr(); //for clearing the screen

 

cout<<“\n Enter any character: “;

cin>>a;

 

a>=65&&a<=90||a>=97&&a<=122? cout<<“\n It is an alphabet.” : a>=48&&a<=57? cout<<“\n It is a number.” : cout<<“\n It is a symbol.”;

 

getch(); //holds the screen until any key is pressed

return 0; //makes main() finish

}

 

OUTPUT:

CASE 1:

Enter any character: d

 

It is a alphabet.

 

 

CASE 2:

Enter any character: 3

 

It is a number.

 

CASE 3:

Enter any character: $

 

It is a symbol.

Write a C++ program to accept a character and determine whether it’s upper case or lower case.

Q17. Write a C++ program to accept a character and determine whether it’s upper case or lower case.

Solution:

#include<iostream.h> //standard input/output header file

#include<conio.h> //for clrscr()

 

int main()

{

char a;

 

clrscr(); //for clearing the screen

 

cout<<“\n Enter any character: “;

cin>>a;

 

a>=90?cout<<“\n It is a lower case letter.” : cout<<“\n It is an upper case letter.”;

 

getch(); //holds the screen until any key is pressed

return 0; //makes main() finish

}

 

OUTPUT:

CASE 1:

Enter any character: r

 

It is a lower case letter.

 

CASE 2:

Enter any character: R

 

It is an upper case letter.

Write a C++ program to input users age and display a message you can vote or cannot vote.

Q16. Write a C++ program to input users age and display a message you can vote or cannot vote.

Solution:

#include<iostream.h> //standard input/output header file

#include<conio.h> //for clrscr()

 

int main()

{

int age;

 

clrscr(); //for clearing the screen

 

cout<<“\n Enter your age: “;

cin>>age;

 

age>=18?cout<<“\n You can vote.” : cout<<“\n You cannot vote.”;

 

getch(); //holds the screen until any key is pressed

return 0; //makes main() finish

}

 

OUTPUT:

CASE 1:

Enter your age: 7

 

You cannot vote.

 

CASE 2:

Enter your age: 28

 

You can vote.