BAHRIA UNIVERSITY ISLAMABAD CAMPUS
DEPARTMENT OF EARTH AND ENVIRONMENTAL SCIENCES
MID TERM TAKE HOME ASSIGNMENT (SPRING 2020)
Subject: Programming Fundamentals Class: BB Geo4A
Instructor’s Name:Joddat Fatima Submission date 31-05-2020
Student Name: Inayat ur rehman Max Marks:15
Enrollment No: 01-165182-009
Q.1. You have two fractions, a/b and b/c their sum can be obtained:
Write a program that encourages the user to enter two fractions and calculate its sum in fractions.
Your output screen should be.
Enter first fraction: 1/4
Enter first fraction: 2/3
Sum = 11/12
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d, ans, ans2;
cout <<"Enter first friction";
cin>>a>>b;
cout<<a<<"/"<<b<<endl;
cout <<"Enter second friction";
cin>>c>>d;
cout<<c<<"/"<<d<<endl;
cout<<"``````````````````````````````````````"<<endl;
cout<<"a/b+c/d = " <<a<<"/"<<b<<"+"<<c<<"/"<<d<<endl;
ans= (a*d)+(b*c);
ans2= (b*d);
cout<<"Sum = "<<ans<<"/"<<ans2<<endl;
return 0;
}
Q.2. Write a C++ code which uses the nested loop and display the Multiplication Table? Your
output screen should look like the following Figure?
#include <iostream>
using namespace std;
int main()
{
for (int i=1 ; i<=10 ; i++)
{
for (int j=1 ; j<=10 ; j++)
{
cout<<i*j<<" ";
}
cout<<endl;
}
return 0;
}
Q.3. Write a C++ code that sum the digits entered by the user? For example, number= 245
sum of digits 2+4+5= 11.
#include<iostream>
using namespace std;
int main()
{
int x, n, sum = 0;
cout << "Enter the number : ";
cin >> n;
x=n;
while (x != 0)
{
sum = sum + x % 10;
x = x / 10;
}
cout << "sum of the digits "<<n<<" is "<<sum<<endl;
return 0;
}
Part B: Short Questions?
a. What is the output of code?
25
4
1
0
b. Correct the syntax/format of the loop and show the output of the corrected loop?
int main()
while (n>0){
n /= 2;
cout << n*n <<endl;
}
Add in {} after loop condition;
a. What will be the output if user enters the integer values that are 0, 15 and 7?
Enter 0; Less than 10.
Enter 15; Greater than 5
Enter 7; Less than 10
c. What values for n will cause the output to be "not interesting"?
No run else function, all accepted; symbols, char, word, and all integers + or -
Rewrite the code and use do-while instead of while loop?
int main()
{
int n;
cout <<"Enter a non-negative integer"<<endl;
cin>>n;
if (n<0)
{
do{
cout << "The integer you entered is negative"<<endl;
cout <<"Enter a non-negative integer"<<endl;
cin>>n;
}while(n<0);
}
return 0;
}
Write the code to produce following output using SWITCH statement?
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Number"<<endl;
cin >> n;
switch(n%2)
{
case 0:
cout <<"NUMBER IS EVEN"<<endl;
break;
case 1:
cout <<"NUMBER IS ODD"<<endl;
break;
}
return 0;
}
Write the code for following output screen and explain the concept of difference in output?
If a user enters the “welcome to edureka” so Programs will print in output just “Welcome”.
Use the string in c++; as a word, not print the sentence. If we will print the sentence so use the
getline command;