Question:5
C++ program to take a number check and print whether the number is even , odd or zero?
Sol:
#include<iostream> using namespace std; int main() { int a; cout<<"Enter any no "; cin>>a; if(a==0) cout<<"Number is zero"; else if(a%2==0) cout<<"Number is even"; else cout<<"Number is odd"; return(0); }
/* Output */ Enter any no 63 Number is odd Enter any no 10 Number is even Enter any no 0 Number is zero
Question:6
C++ program to take input for three number check and print largest number?
Sol:
Method:1
#include<iostream> using namespace std; int main() { int a,b,c; cout<<"Enter 3 nos "; cin>>a>>b>>c; if(a>b && a>c) cout<<"Max no = "<<a; else if(b>c) cout<<"Max no = "<<b; else cout<<"Max no = "<<c; return(0); }
Enter 3 nos 25 96 3 Max no = 96
Method:2
#include<iostream> using namespace std; int main() { int a,b,c,m; cout<<"Enter 3 nos "; cin>>a>>b>>c; if(a>b && a>c) m=a; else if(b>c) m=b; else m=c; cout<<"Max no = "<<m; return(0); }
Method:3
#include<iostream> using namespace std; int main() { int a,b,c,m; cout<<"Enter 3 nos "; cin>>a>>b>>c; m=a; if(b>m) m=b; if(c>m) m=c; cout<<"Max no = "<<m; return(0); }
Method:4
#include<iostream> using namespace std; int main() { int a,b,c,m; cout<<"Enter 3 nos "; cin>>a>>b>>c; m=(a>b && a>c)?a:(b>c)?b:c; cout<<"Max no = "<<m; return(0); }