

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Write the following code, execute it and compare the output
Program-
#include <stdio.h> #include <conio.h> #include <iostream.h> main() {
int x,y;
cin>>x; cin>>y;
if(x>y) cout<<”X is greater than Y”); getch(); }
Exercise Modify the program so that it should execute as given below
Program Execution
Enter value for X: 23 Enter value for Y: 65 Y=65 is greater than X= Press any key to terminate the program..
Read this : The % is called the mod operator, it returns the remainder of a division. e.g.
13%5 = 13 / 5 = 2 (quotient) + 3 (remainder) = 3 17 %5= 17 / 5 = 3 (quotient) + 2 (remainder) = 2 56 % 13 = 4 (quotient) + 4 (remainder) = 4
We can check if a number is even or odd by using % (mod) operator if a number is even the remainder with 2 will be zero for odd number it will be a non zero number
3 %2 = 1 (3 is odd) 7 %2 = 1 (7 is odd) 6% 2 = 0 (6 is even)
(have a look at the following program)
Program-
#include <stdio.h> #include <conio.h> #include <iostream.h> main(void) {
int x; cout<<”Enter value for x:”;
cin>>x;
if( (x%2)==0) cout<<”X is an even integer”<<endl; else cout<<”X is an odd integer”<<endl;
getch(); }
Program-
#include <stdio.h> #include <conio.h> #include <iostream.h> main() {
int length, width;
cout<<”Enter the length of the diagram”<<endl; cin>>length; cout<<”Enter the width of the diagram”<<endl;
if(length==width) cout<<”The diagram is a square”<<endl; else cout<<”The diagram is a rectangle”;
getch(); }
Exercise Extend the program, get length and width of two diagram from the user and check 1) the two diagrams are of same length and same width 2) which of the two diagram is a square and which one is a rectangle
Hint: Do it on paper for two diagrams , develop a technique to compare diagrams and then write the program
Program-04 (character input)
#include <stdio.h> #include <conio.h> #include <iostream.h> main() {
char ans;
cout<<”Is today your birthday? (Y/N) : “; cin>>ans;
if(ans==’y’ || ans == ‘Y’) cout<<” Happy Birthday To You “<<endl ; else
Docsity.com
Exercise Extend the program-2, get two numbers X and Y from the user, check that X completely divides Y or not. If X does not divides Y print the remainder. The output of the program should be as follows
Output Enter value for X: 3 Enter value for Y: 10 X does not divides Y Remainder is = 1 Press any key to terminate the program...
cout<<”Have a nice day”<<endl; getch(); }
Exercises
1- Write a program that compares three integers and print the smallest of the three
2- Write a program that ask users to input three numbers and print how many of them are even, how many of them are odd and print the numbers in ascending order
Docsity.com