Lab02Answers

advertisement
CS110 Lab 2 Answers
1. Write her/his gender 'Male' or 'Female'.
#include <iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
//This program is identifying
int main()
{
the gender of the user.//
char g;
cout<<"What is your gender (M/F) ? \n";
cin>>g;
if(g=='F' || g=='f')
cout<<"You are Female!";
else
cout<< "You are male!";
getch();
}
2. Print (sum , product, difference and quotient)
#include <iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
//This program is This program is using 4 operations on two numbers entered by the user//
int main()
{
double num1, num2;
cout<<"Please enter num1\n";
cin>>num1;
cout<<"Please enter num2\n";
cin>>num2;
cout<<"the sum of num1 and num2 is "<<num1+num2<<"\n";
cout<<"the product of num1 and num2 is "<<num1*num2<<"\n";
cout<<"the difference of num1 and num2 is "<<num1-num2<<"\n";
cout<<"the quotient of num1 and num2 is "<<num1/num2<<"\n";
getch();
}
3. find the largest values among the 6 numbers
#include <iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
//This program is comparing between six numbers entered by the user and then prints
number//
int main()
{
double num1,num2,num3,num4,num5,num6;
double max=1;
cout<<"Please enter SIX numbers\n";
cout<<"Please enter num1 \n";
the largest
cin>>num1;
cout<<"Please enter num2
cin>>num2;
cout<<"Please enter num3
cin>>num3;
cout<<"Please enter num4
cin>>num4;
cout<<"Please enter num5
cin>>num5;
cout<<"Please enter num6
cin>>num6;
if (num1>=num2)
max=num1;
else
max=num2;
if (num3>=max)
max=num3;
if (num4>=max)
max=num4;
if (num5>=max)
max=num5;
if (num6>=max)
max=num6;
\n";
\n";
\n";
\n";
\n";
cout<<"the largest number is "<<max<<"\n";
getch();
}
4. find the two largest values among the 6 numbers
#include <iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
//This program is comparing between six numbers entered by the user and then prints
largest numbers//
int main()
{
double num1,num2,num3,num4,num5,num6;
double max1=1,max2=1;
cout<<"Please enter SIX numbers\n";
cout<<"Please enter num1 \n";
cin>>num1;
cout<<"Please enter num2 \n";
cin>>num2;
cout<<"Please enter num3 \n";
cin>>num3;
cout<<"Please enter num4 \n";
cin>>num4;
cout<<"Please enter num5 \n";
cin>>num5;
cout<<"Please enter num6 \n";
cin>>num6;
if (num1>num2)
{
max1=num1;
max2=num2;
}
else if(num1!=num2)
{
the
two
max1=num2;
max2=num1;
}
else max1=num1;
if (num3>max1)
{
max2=max1;
max1=num3;
}
if (num4>max1)
{
max2=max1;
max1=num4;
}
if (num5>max1)
{
max2=max1;
max1=num5;
}
if (num6>max1)
{
max2=max1;
max1=num6;
}
if(num6>max2&&num6!=max1)
max2=num6;
cout<<"the two largest number is "<<max1<<" and "<<max2<<"\n";
getch();
}
BEST WISHES ☺☺
NISREEN ALZAHRANI
Download