شيت البرمجة النسخة النهائية بعد التعديل

Post on 27-Apr-2017

224 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1) Write a single C++ statement to accomplish each of the following:a) Declare the variables c, q76354 and number to be of type int.b) Read an integer from the user at the keyboard and store the

value entered in integer variable age.c) Print the message "This is a C++ program" on one line.d) Print the message "This is a C++ program" on two lines, in

which the first line ends with C++.e) Print the message "This is a C++ program" with each word of

the message on a separate line;f) Print the message "This is a C++ program" with each word

separated from the next by a tab.

Answera)int c, q76354, number;b)cin>>age;c)cout<<" This is a C++ program "<<endl;d)cout<<" This is a C++ \nprogram\n";e)cout<<" This\nis\na\nC++\nprogram\n";f)cout<<" This\tis\na\tC++\tprogram\t";

2State whether each of the following is true or false

T F 1.Comments cause the computer to print the text after // on the screen when the program is executed.

T F 2.All variables must be declared before they are

used.

T F 3.All variables must be given a type when they

are declared.

T F 4.The arithmetic operators *, /, %, + and – all

have the same level of precedence.

T F 5.Declarations can appear almost anywhere in the

body of C++ function

T F 6.C++ considers the variables number and

NuMbEr to be identical.

3) State the order of evaluation of the operators in the following C++ statements and show the value of after each statement is performed.

C++ statement value of i) X=15

ii) X=8iii) X=32iv) X=324

4)Find the C++ expression for the following mathematical expressions

Math C++X= (a+b+c+d+e)/5;

X=pow(a,4)*b-sqrt(c*d);

X=sqrt(a*b-c);

X=log(a*a+b);.

5)Write a program that asks the user to enter two numbers, obtain two numbers from the user and prints the sum, product, difference, quotient and reminder of the two numbers. Answer

#include<iostream.h>void main(){int a b;cin>>a>>b;cout<<"sum ="<<a+b<<endl;cout<<"product ="<<a*b<<endl;cout<<"difference ="<<a-b<<endl;cout<<"quotient ="<<a/b<<endl;cout<<" reminder ="<<a%b<<endl;}

6)Write a program that asks the user to enter two numbers, obtain two numbers from the user, then prints the larger number followed by the words "is larger". If the numbers are equal, print the message "These numbers are equal".Answer

#include<iostream.h>void main(){ int x, y;

cout<<"enter x= "; cin>>x; cout<<endl; cout<<"enter y= "; cin>>y; cout<<endl; if(x>y) cout<<"x is larger"<<endl; if(y>x) cout<<"y is larger"<<endl; if(x==y) cout<<"these numbers are equal"<<endl; }

7)Write a program that reads in the radius of a circle and prints the circle's diameter, circumference and area. Use the constant value 3.14159 for . Do these calculations in output statements.

Answer

#include<iostream.h>void main(){Const float Pi=3.14159;int r; cout<<"enter radius = ";cin>>r;cout<<"\ndiameter = "<<2 * r <<endl;cout<<" circumference ="<<2 * pi * r <<endl;cout<<"area ="<< pi * r * r <<endl;}

8)What is output of the following program:-

# include<iostream.h>void main(){int i,j;for(i=1;i<=6;i++) { for(j=0;j<=i;j++) cout<<"*"; cout<<endl; }}

Ans** ** * ** * * ** * * * ** * * * * *

10)Write the C++ program to find the sum and average of a set of n integer numbers.

Answer

#include<iostream.h>void main(){int n, i, x sum=0, average;cout<<"enter n = ";cin>>n;

for(i=0;i<=n;i++){cout<<"enter x= ";cin>>x;sum += x;}average = sum/n;cout<<"sum = "<<sum<<endl;cout<<"average ="<<average<<endl;

9)

Write the C++ program to find the sum of odd integers between 1 and 99 using a for structure.

Answer

#include<iostream.h>void main() { int sum=0; for(i=0;i<=99;i=i+2) sum +=i; cout<<" sum of odd integers between 1 and 99 ="<<sum; }

11) Write the C++ program to find the factorial of any integer number n.

Answer#include<iostream.h>void main(){int i, n, fact=1;cout<<"enter n = ";cin>>n;for(i=1;i<=n;i++) fact *=i;cout<<"the fact of "<<n<<" ="<<fact;}

12)Write the C++ program to find the square and square root of 10 integer numbers1-10 using while loop.

Answer

#include<iostream.h>#include<math.h>#include<iomanip.h>void main(){int i;cout<<" i i*i sqrt(i)"<<endl;cout<<"--------------------------------------------------------"<<endl;i=1;while(i<=10){Cout<<setw(4)<<i<<setw(4)<<i*i<<setw(4)<<sqrt(i)<<endl;i+=1;}}

13)What does this program print#include<iostream.h>void main(){int y, x=1, total=0;while(x<=10){ y = x * x;cout<<y<<endl;total += y;++x;} Cout<<"total is "<<total<<endl;}

Answer

14)

Find the errors in each of the following code segments

and explain how to correct them (answer only eight)

(a) cout >>”The result=<<area;

No Error Correction1 >> <<2 "is not found "

(b) if (d=1) cout <<”Sat"<<endl;else; cout <<"not Sat"<<endl;

No Error Correction1 = ==2 Else; Else

(c)

for (int i=1;i<=100;i+) Cout<<i<<andl;

No Error Correction1 I+ I++2 Andl Endl

(d)

#include <iostream> void maim ()

No Error Correction1 <iostream> <iostream.h>2 Maim main

(e)

void main();{ cout<<"Welcome to Major exam 2"<<end;}

No Error Correction1 Void main(); Void main()2 End Endl

(f)

int a[3]=(12,6,9);

for(int i=0; i<3; i++) cout <<a;

No Error Correction1 int a[3]=(12,6,9); int a[3]={12,6,9};2 A a[i] (i)

double x[4]= {1.1 , 2.3 , 4.4 , 5.8 ,6.9};

cout <<a[1]<<" "<<a(3);

No Error Correction1 x[4]= {1.1 , 2.3 , 4.4 ,

5.8 ,6.9};x[5]= {1.1 , 2.3 , 4.4 , 5.8 ,6.9};

2 a(3) X[3]

15) What is the output of the following programs:

(a)

*******WELCOME*******

d=8

sum=21

********THANK YOU********

(b) use any input you want

please enter height=3

Please enter width=5

The result=15

(c)

1

2

3

4

5

6

7

8

9

10

(d)

the sum =55

(e) #include <iostream.h> void main ( ) { int x=6; cout << " x = " << x << endl; cout << " x = " << ++x << endl; cout << " x = " << x << endl; cout << " x = " << x++ << endl; cout << " x = " << x << endl; }

X=6X=7X=7X=7X=8

16)

Write a program to print Multiplication table from 1 to 5 as following output:

#include<iostream.h>Void main(){Int I,j,p;For(i=1;i<=5;j++){For(j=1;j<=5;j++){P=j*I;Cout<<p;Cout<<"_____________";}Cout<<endl;}}

17) Write a program to compute the operations for two int numbers (sum, subtract, multiplication, divide), using switch statement as following output:

#include<iostream.h>Void main(){Int x,y;Float r;Char op;Cout<<"enter x then y then any operation(+,-,*,/)"<<endl;Cin>>x>>y>>op;Switch (op){Case'+': r=x+y; break;Case'-': r=x-y; break;Case'*': r=x*y; break;Case'/': r=x/y; break;Default: cout<<"bad operation";}Cout<<"r="<<r<<endl;}

18)Write a C++ program to read array a of n real elements and find their sum and average.

#include<iostream.h>Void main(){Const int n=5;Int I,n,Float a[n],sum, average;Sum=0;For(i=0;i<a;i++){Cin>>a[i];Sum+=a[i];}Average=sum/n;Cout<<"sum="<<sum<<endl;Cout<<"average="<<average<<endl;}

19)Write a C++ program to find the smallest element of an array of n real elements.

#include<iostream.h>Void main(){Const int n=5Int I;Float a[n],small;For(i=0;i<n;i++)Cin>>a[i];Small=a[0];For(i=1;i<n;i++)If(a[i]<small)Small=a[i];Cout<<"small="<<small<<endl;}

top related