NED UNIVERSITY OF ENGG. AND TECH.
SPRING 2019
COMPUTER AND PROGRAMING (CP)
EE-163
Prepared by:
Isfahan Mukhtiar (EE-002)
Kashif Ali (EE-003)
Muhammad Ahmed Khan (EE-017)
Ramish Uddin (EE-040)
Hunain Raza Khan (EE-001)
QUESTION NO:1
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++" << endl;
return 0;
}
QUESTION NO:2
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome \n To \n C++ \n"<<endl;
return 0;
}
QUESTION NO:3
#include <iostream>
using namespace std;
int main()
{
int a,b,sum;
cout << "Pick first number"<<endl;
cin >> a;
cout << "Pick second number"<<endl;
cin >> b;
sum = a+b;
cout << "Hence the sum is "<<sum<<endl;
return 0;
}
QUESTION NO:4
C++ uses operators to do arithmetic. It provides operators for five
basic arithmetic calculations: addition, subtraction, multiplication, division, and taking the
modulus.
In order to properly evaluate an expression such as 4 + 2 * 3, we must understand both what
the operators do, and the correct order to apply them. The order in which operators are
evaluated in a compound expression is called operator precedence. Using normal mathematical
precedence rules (which state that multiplication is resolved before addition), we know that the
above expression should evaluate as 4 + (2 * 3) to produce the value 10.
In C++, all operators are assigned a level of precedence. Those with the highest precedence are
evaluated first. You can see in the table below that multiplication and division have a higher
precedence than addition and subtraction. The compiler uses these levels to determine how to
evaluate expressions it encounters.
Thus, 4 + 2 * 3 evaluates as 4 + (2 * 3) because multiplication has a higher level of precedence
than addition.
If two operators with the same precedence level are adjacent to each other in an expression,
the associativity rules tell the compiler whether to evaluate the operators from left to right or
from right to left.
For example, in the expression 3 * 4 / 2, the multiplication and division operators are both
precedence level 5. Level 5 has an associativity of left to right, so the expression is resolved
from left to right: (3 * 4) / 2 = 6.
QUESTION NO:5
Step 1
Find and download a free or commercial compiler. Some free compilers include Dev
C/C++ and Cfree. For this tutorial, use Microsoft's Visual C++. It is
a commercial product that is easy to use. Install and launch the compiler of your choice.
Step 2
Click on "File" in the upper left-hand corner. In the pop-up window, under the "File" tab,
click on "C++ Source File" and then click "OK." Paste the code into the text box that
opens up and save the file.
Step 3
Compile the file by clicking on "Build" and then click on "Compile example.cpp" (where "
example.cpp" is the file that you just saved). Click "Yes" when you are prompted to
create a project workspace.
Step 4
Build the file by clicking on "Build" and then "Build example.cpp." You may just tap "F7"
instead.
Open the directory in which you saved your .cpp file. Locate and open the folder labeled
"Debug." You will find your new .exe file here and can now execute it.
Tip
The source code for your .cpp file has to be in C or C++.
QUESTION NO:6
#include <iostream>
using namespace std;
int main()
{
int a,b,result;
char op;
cout << "Pick first number"<<endl;
cin >> a;
cout << "Pick second number"<<endl;
cin >> b;
cout << "Select operator"<<endl;
cin >> op;
if (op=='+')
{
result=a+b;
}
else if (op=='-')
{
result = a-b;
}
else if (op=='*')
{
result = a*b;
}
else if (op=='/')
{
result = a/b;
}
else
{
cout << "Invalid input"<<endl;
}
cout << "The result is "<<result<<endl;
return 0;
}
QUESTION NO:7
#include <iostream>
using namespace std;
int main()
int a,b;
cout << "Pick a number"<<endl;
cin>> a;
cout << "Pick a second number"<<endl;
cin >> b;
if (a>b)
cout << "N1 is larger than N2"<<endl;
else if (a<b)
cout << "N1 is smaller than N2"<<endl;
else if (a=b)
cout << "These numbers are equal"<<endl;
else
{
cout << "Invalid input "<<endl;
return 0;
}
QUESTION NO:8
#include <iostream>
using namespace std;
int main()
int a,b,c,s,p,Av,G,S;
cout<<"Enter three integers:"<<endl;
cin>>a>>b>>c;
s=a+b+c;
p=a*b*c;
Av=(a+b+c)/3;
cout<<"SUM is:"<<s<<endl;
cout<<"Product is:"<<p<<endl;
cout<<"Average is:"<<Av<<endl;
if(a<b && a<c){
cout<<"Smallest is:"<<a<<endl;
else if(b<a && b<c){
cout<<"Smallest is:"<<b<<endl;
else if(c<a && c<b){
cout<<"Smallest is:"<<c<<endl;
}
else if(a>b && a>c){
cout<<"Largest is:"<<a<<endl;
else if(b>a && b>c){
cout<<"Largest is:"<<b<<endl;
else if (c>a && c>a)
cout<<"Largest is:"<<c<<endl;
else
cout << "Invalid input"<<endl;
return 0;
}
QUESTION NO:9
#include <iostream>
using namespace std;
int main()
int num,ans;
cout <<"Enter an integer"<<endl;
cin >> num;
ans=num %2;
if (ans==0)
cout << "Even"<<endl;
else
cout << "Odd"<<endl;
return 0;
}
QUESTION NO:10
#include <iostream>
using namespace std;
int main()
int num, a,b,c,d,e;
cout << "Enter 5 integers"<<endl;
cin >> num;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
d=num%10;
num=num/10;
e=num%10;
num=num/10;
cout << e <<" "<< d <<" "<< c <<" "<< b <<" "<< a <<endl;
return 0;
}
QUESTION NO:11
#include <iostream>
using namespace std;
int main()
{
int hours,hourly_rate;
cout << "Enter hours worked"<<endl;
cin >> hours;
cout << "Enter hourly rate of the employee"<<endl;
cin >> hourly_rate;
if (hours<=40)
{
cout << "Salary is "<<hours*hourly_rate;
}
else
{
cout << "Salary is "<<(40*hourly_rate)+(hours-40)*(1.5)*hourly_rate;
}
return 0;
}
QUESTION NO:12
#include <iostream>
using namespace std;
int main()
int num,f=1;
cout << "Enter a number"<<endl;
cin >> num;
for (int x=1;x<=num;x++)
f=f*x;
cout << "The factorial is "<<f<<endl;
return 0;
}
QUESTION NO:13
#include <iostream>
using namespace std;
int main()
int n;
cout << " Enter number of elements in fabioncci sequence: ";
cin >> n;
int first=0;
int second=1;
int next;
for(int i=0; i<n; i++)
if(i<=1)
next=i;
}else
next=first+second;
first=second;
second=next;
cout << next << " ";
return 0;
}
QUESTION NO:14
#include <iostream>
using namespace std;
int main()
double a,b,c;\
cout << "Enter length of three sides"<<endl;
cin >> a>>b>>c;
if((a+b)==c&& (b+c)==a&& (a+c)==b)
cout << "These are the sides of the triangle"<<endl;
else
cout << "These are not the sides of the triangle"<<endl;
return 0;
}
QUESTION NO:15
#include <iostream>
using namespace std;
int main()
double H,B,P;
cout << "Enter a hypotenuse"<<endl;
cin >> H;
cout << "Enter a base "<<endl;
cin >> B;
cout << "Enter a perpendicular "<<endl;
cin >> P;
if (H=(B*B)+(P*P))
cout << "These are the sides of the triangle"<<endl;
else
cout << "These are not the sides of the triangle"<<endl;
return 0;
}
QUESTION NO:16
#include <iostream>
#include <cmath>
using namespace std;
int main()
double x;
int n;
int fact=1;
double ex =1.0;
cout << "Enter number whose exponential needs to be evaluated"<<endl;
cin >> x;
cout << "Enter number of terms "<<endl;
cin >> n;
for (int i =1;i<=n;i++)
fact=fact*i;
ex=ex + (pow(x,i))/fact ;
cout << "The exponential of "<<x<< " is "<<ex;
return 0;
}
QUESTION NO:17
#include <iostream>
#include <cmath>
using namespace std;
int main()
double x,a,b;
int n;
int fact =1;
double sinx=0.0;
cout << "Enter value for sine evaluation"<<endl;
cin >> x;
cout << "Enter number of terms in the simulation"<<endl;
cin >> n;
for (int i=1;i<=n;i++)
fact =fact +(2*i)+1;
a=pow(-1,i);
b=pow(x,(2*i)+1);
sinx = sinx + (a*b)/fact;
cout << "The result is "<< sinx;
return 0;
}
QUESTION NO:18
#include <iostream>
using namespace std;
int main()
{
for (int y = 1; y <= 10; y++)
{
for(int s = 10-y; s>0; s--)
{
cout<<" ";
}
for(int x = 1; x <= y; x++)
{ cout<< "*" <<" ";
}
cout << endl;
}
cout << endl;
for(int i = 1; i <= 10; ++i)
{
for(int j = 1; j <= i; ++j)
{ cout << "* ";
} cout << "\n" ;
}
cout << endl;
for (int a = 10; a > 0; a--)
{
for (int sp = 0; sp < 10 - a; sp++)
{
cout<<" ";
}
for(int b = 1; b <= a; b++)
{
cout<<"*"<<" ";
}
cout<<endl;
}
cout << endl;
for(int i = 10; i >= 1; --i)
{ for(int j = 1; j <= i; ++j)
{ cout << "* ";
}
cout << endl;
}
cout << endl;
}
QUESTION NO:19 & 20
#include <iostream>
using namespace std;
int main()
{
int rows, space, star;
int totalrows;
cout << " Enter no, of rows in diamond: ";
cin >> totalrows;
for(rows=1; rows<=totalrows; rows++)
{
for( space=totalrows-rows; space>=1; space--)
{
cout << " ";
}
for( star=1; star<=(2*rows)-1; star++)
{
cout << "*";
}cout << endl;
}
for( rows=totalrows-1; rows>=1; rows--)
{
for(space=1;space<=totalrows-rows ; space++)
{
cout << " ";
}
for(star=1; star<=(2*rows)-1; star++)
{
cout << "*";
} cout << endl;
}
return 0;
}
QUESTION NO:21
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
for(a=1; a<=500; a++)
{
for(b=1; b<=500; b++)
{
for(c=1; c<=500; c++)
{
if((a*a)+(b*b)==(c*c)&& c<=500)
{
cout << a << " + " << b << " = " << c << endl;
}
}
}
}
return 0;
}