Junaid program assignment

15
Problem Set for Sequential Structure Programming Assignment Junaid Ahmed (BS-SE) 1st semester Roll No: F14-030 Convert the following mathematical expressions into C++ Expressions. Algebraic Expression C++ Expression (a*b)-(c*d) (m+n)*(a+b) 3*x*x+2(x)+5 (a+b+c)/(d+e) [(2*b*y)/(d+1)-(x)/3(z+y)] Which of the following are invalid variable names and why? BASICSALARY valid variable. #MEAN is wrong because “#” is not allowed for variable. 2015_DDay invalid variable. group. Is variable. basic-hra - is not allowed in variable. _basic is variable. 422 numbers are not variables. Plot # 3 invalid statement “#” is not allowed in variable. hELLO is variable. population in 2006 spaces are not allowed in variable. over time is again wrong due space in variable. mindovermatter is variable. queue. . is not is variable. team’svictory ‘ is not allowed in variable. FLOAT is variable.

Transcript of Junaid program assignment

Page 1: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

Convert the following mathematical expressions into C++ Expressions.

Algebraic Expression C++ Expression

(a*b)-(c*d)

(m+n)*(a+b)

3*x*x+2(x)+5

(a+b+c)/(d+e)

[(2*b*y)/(d+1)-(x)/3(z+y)]

Which of the following are invalid variable names and why?

BASICSALARY valid variable. #MEAN is wrong because “#” is not allowed for variable. 2015_DDay invalid variable. group. Is variable. basic-hra - is not allowed in variable. _basic is variable. 422 numbers are not variables. Plot # 3 invalid statement “#” is not allowed in variable. hELLO is variable. population in 2006 spaces are not allowed in variable. over time is again wrong due space in variable. mindovermatter is variable. queue. . is not is variable. team’svictory ‘ is not allowed in variable. FLOAT is variable.

Page 2: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

Evaluate the following expressions according to their precedence.

Expression Evaluate C++ Expression

(A)g = big / 2 + big * 4 / big - big + abc / 3 ;

(abc = 2.5, big = 2, assume g to be a float)

(B) on = ink * act / 2 + 3 / 2 * act + 2 + tig ;

(ink = 4, act = 1, tig = 3.2, assume on to be an int)

(C) s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ; (qui = 4, add = 2, god = 2, assume s to be an int)

(D) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ; (a = 4, g = 3, assume s to be an int)

float g=(big/2)+((big*4)/big)-((big+abc)/3);

int on=((ink*act)/2)+(3/2*act)+2+tig;

int s=((qui*add)/4)-(6/2)+((2/3)*(6/god));

int s=((1/3)*(a/4))-(6/2)+((2/3)*(6/g));

(A) # include<iostream> # include<stdio.h> # include<conio.h>

using namespace std;

int main()

{

float g, big, abc;

big=2;

abc=5;

g = big /( 2 + big * 4) /( big - big + abc )/ 3 ;

cout<<g;

}

(B) # include<iostream> # include<stdio.h> # include<conio.h>

using namespace std;

int main()

Page 3: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

{

int ink, act;

ink=4;

act=1;

float tig, on;

tig=3.2;

on = (ink * act )/( 2 + 3) / (2 * act + 2 + tig);

cout<<on;

}

(C)

# include<iostream> # include<stdio.h> # include<conio.h>

using namespace std;

int main()

{

int qui,add,god,s;

qui=4; add=2; god=2;

s = (qui * add )/ (4 - 6 )/( 2 + 2) / (3 * 6)/ god ;

cout<<s;

}

(D)

# include<iostream> # include<stdio.h>

# include<conio.h> using namespace std; int main()

{ int a, g, s;

a=4; g=3; s = 1 / (3 * a )/ (4 - 6 )/ (2 + 2)/ (3 * 6)/ g ; cout<<s;

getch(); }

Page 4: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

Find the errors, if any in the following C++ statements.

Errors C++ statements

(a) int = 314.562 * 150 ;

(b) name = ‘Ajay’ ;

(c) varchar = ‘3’ ;

(d) 3.14 * r * r * h = vol_of_cyl ;

(e) k=(a*b) (c+(2.5a+b)(d+e);

(f) m_inst = rate of interest *

amount in rs ;

(g) si = principal * rateofinterest

* numberofyears / 100 ;

(h) area = 3.14 * r ** 2 ;

(i) volume = 3.14 * r ^ 2 * h ;

(j) k = (( a*b )+ c ) (2.5 * a + b ) ;

(k) a = b = 3 = 4 ;

(l) count = count + 1 ;

(m) date = '2 Mar 04' ;

Float=314.562*150;

String=’Ajay’

Varchar=(3);

Vol_of_cyl=3.14*r*r*h;

is wrong because missing some opratus

m_inst = rate_of_interest*

amount_in rs;

int si=principal*rateofinterest*numberofyears/100 ;

float area=3.14*r*2;

volume=3.14*r*r*h;

k=((a*b)+c)*(2.5*a+b);

a=3,b=4;

count=count+1; is correct or we can write it as count++;

string date=”2March04”;

Convert the following equations into corresponding C++ statements.

Equations C++ statements

Z = 8.8(a+b)2/c-0.5+2a/(q+r)

(a+b)*(1/m)

X = -b+(b*b)+24ac

2a

R = 2v+6.22(c+d)

g+v

A = 7.7b(xy+a)/c-0.8+2b

(x+a) (1/y)

Z = 8.8(a+b)2/(c)-0.5+2(a)/(q+r)/(a+b)*(1/m)

X = -(b)+(b*b)+2(4)(a*c)/2(a)

R = 2(v)+6.22(c+d)/(g+v)

A = 7.7(b)[(x*y)+a]/c-0.8+2(b)/(x+a)*(1/y)

Page 5: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

/*Trace out the output (A)*/ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { int i=2,j=3,k,l; float a,b; k=i/j*j; l=j/i*i; a=i/j*j; b=j/i*i; cout<<k<<l<<a<<b; getch(); return 0; }

/*Trace out the output (B)*/

# include<iostream> # include<stdio.h> # include<conio.h> using namespace std;

int main() { int a,b; a=(-3)-(-3); b=(-3)+(-3); cout<<"a="<<a<<"b="<<b; getch(); return 0; }

Page 6: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

/*Trace out the output (c)*/

# include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { cout<<"nn\n\n nn\n"; printf( "nn/n/nnn/n"); getch(); return 0; }

/*Trace out the output (D)*/ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { int p,q; cout<<"Enter Values of p and q"; cin>>p>>q; cout<<"p="<<p<<"\tq="<<q; getch(); return 0; }

Write a C++ program for the following

/* (a) Rameez's basic salary is input through the keyboard. His dearness allowance is 40% of basic salaryand house rent allowance is 20% of basic salary.Write a program to calculate his gross salary.*/ # include<iostream>

# include<stdio.h>

Page 7: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

# include<conio.h> using namespace std; int main() { float bs=0,da=0,hr=0,gs=0; cout<<" Enter Rameez's basic salary : "; cin>>bs; da=bs*40/100; hr=bs*20/100; gs=da; /*cout<<"\n Basic salary : "; cout<<bs; cout<<"\n Deaness allowance : "; cout<<da; cout<<"\n House rent allowance : "; cout<<hr;*/ cout<<"\n Gross salary : "; cout<<gs; getch(); return 0; }

/*(b) The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters. */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float km=0,m=0,f=0,i=0,cm=0; cout<<" Enter Kilometers : "; cin>>km; m=km*1000; f=km*3280.83989501; i=km*39370.1; cm=km*100000; cout<<"\n Meters : "; cout<<m; cout<<"\n Feets : "; cout<<f; cout<<"\n Inches : ";

Page 8: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

cout<<i; cout<<"\n Centimeters : ";

cout<<cm; getch(); return 0; }

/*(c) If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student.Assume that the maximum marks that can be obtained by a student in each subject is 100. */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float che,phy,bio,eng,com,total=500,sum,agg=0,per=0; cout<<"\n Chemistry : "; cin>>che; cout<<"\n Physics : "; cin>>phy; cout<<"\n Biology : "; cin>>bio; cout<<"\n English : "; cin>>eng; cout<<"\n Computer : "; cin>>com; sum=che+phy+bio+eng+com; agg=(sum/total)*100; cout<<"\n Aggregate Marks : "; cout<<agg; per=(sum*100)/total; cout<<"\n Percentage : "; cout<<per; getch(); return 0;

Page 9: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

}

/*(d) Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees. Tc = (5/9)*(Tf-32); */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main()

{ float tf=0,tc=0,Div; cout<<"Enter Fahrehrit degrees : "; cin>>tf; Div=0.5555555555555556; tc=Div*(tf-32); cout<<"Centigrade degrees: "; cout<<tc; getch(); return 0; }

/*(e) The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle. */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() {

Page 10: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

float len1=0,bre1=0,rad=0,rec1=0,area1=0,per1=0,area2=0,cir=0; cout<<" Enter lenght of rectangle : "; cin>>len1; cout<<" Enter breadth of rectangle : "; cin>>bre1; cout<<" Enter radius of circle : "; cin>>rad; area1=len1*bre1; per1=2*(len1+bre1); cout<<"\n Area of rectangle : "; cout<<area1; cout<<"\n Perimeter of rectangle : "; cout<<per1; area2=3.14159265*(rad*rad); cir=6.2831853*rad; cout<<"\n Area of circle : "; cout<<bre1; cout<<"\n Circumference of circle : "; cout<<cir; getch(); return 0; }

/*(f) Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D. */

# include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float loc1=0,loc2=0,x=0; cout<<" Enter location C number : "; cin>>loc1; cout<<" Enter location D number : "; cin>>loc2; x=loc1; loc1=loc2; loc2=x;

Page 11: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

cout<<"\n Value of C locatio : "; cout<<loc1; cout<<"\n Value of D Location : "; cout<<loc2; getch(); return 0; }

/*(g) If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits.*/ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float a=0,b=0,c=0,d=0,e=0,sum=0; cout<<" Enter 1st digit of a number : "; cin>>a,b,c,d,e; cout<<" Enter 2nd digit of a number : "; cin>>b; cout<<" Enter 3rd digit of a number : "; cin>>c; cout<<" Enter 4th digit of a number : "; cin>>d; cout<<" Enter 5th digit of a number : "; cin>>e; sum=a+b+c+d+e; cout<<"\n Sum : "; cout<<sum; getch();

return 0; }

/*(h) If a five-digit number is input through the keyboard, write a program to reverse the number. */

Page 12: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

# include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float a=0,b=0,c=0,d=0,e=0; cout<<" Enter 1st digit number : "; cin>>a; cout<<" Enter 2nd digit number : "; cin>>b; cout<<" Enter 3rd digit number : "; cin>>c; cout<<" Enter 4th digit number : "; cin>>d; cout<<" Enter 5th digit number : "; cin>>e; cout<<"\n 1st digit number : "; cout<<e; cout<<"\n 2nd digit number : "; cout<<d; cout<<"\n 3rd digit number : "; cout<<c; cout<<"\n 4th digit number : "; cout<<b; cout<<"\n 5th digit number : "; cout<<a; getch(); return 0; }

/*(i) If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number. */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main()

Page 13: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

{ float a=0,b=0,c=0,d=0,e=0,sum=0; cout<<" Enter 1st digit of a number : "; cin>>a; cout<<" Enter 2nd digit of a number : "; cin>>b; cout<<" Enter 3rd digit of a number : "; cin>>c; cout<<" Enter 4th digit of a number : "; cin>>d; cout<<" Enter 5th digit of a number : "; cin>>e; sum=a+e; cout<<"\n Sum : "; cout<<sum; getch(); return 0; }

/*(j) In a town, the percentage of men is 52. The percentage of total literacy is 48. If total percentage of literate men is 35 of the total population, write a program to find the total number of illiterate men and women if the population of the town is 80,000. */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float tm,im,iw,tp=80000; cout<<" Total popuation in Town : "; cout<<tp; tm=(52.0/100.0)*tp; im=tm-iw; cout<<"\n Total number of illeriate men : "; cout<<im; iw=tp-tm; cout<<"\n Total number of illeriate women : "; cout<<iw; getch();

Page 14: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

return 0; }

/*(k)A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn

isinput through the keyboard in hundreds, find the total number of currency notes of each

denomination the cashier will have to give to the withdrawer. */

# include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float a=0,b=0,c=0,x=0; cout<<" Amount : "; cin>>x; a=x/10; cout<<"\n Currency notes of 10 : "; cout<<a; b=x/50; cout<<"\n Currency notes of 50 : "; cout<<b; c=x/100; cout<<"\n Currency notes of 100 : "; cout<<c; getch(); return 0; }

/*(l) If the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a program to find the cost price of one item. */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() {

Page 15: Junaid program assignment

Problem Set for Sequential StructureProgramming Assignment

Junaid Ahmed (BS-SE)1st semester

Roll No: F14-030

float sp=0,tp=0,cp=0; cout<<" Total selling price : "; cin>>sp; cout<<"\n Total Profit Earned : ";

cin>>tp; cp=(sp+tp)/15; cout<<"\n Cost price of one item : "; cout<<cp; getch(); return 0; }

/*(m) If a five-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. */ # include<iostream> # include<stdio.h> # include<conio.h> using namespace std; int main() { float dig=0,x=0,y=0; cout<<" Enter digits : "; cin>>dig; y=11111; x=y+dig; cout<<"\n Output : "; cout<<x; getch(); return 0; }