Assignment II Solution

advertisement
‫المملكة العربية السعودية‬
Kingdom of Saudi Arabia
Ministry of Higher Education
Majmaah University
Computer Science and Information Technology College
‫وزارة التعليم العالي‬
‫جامعة المجمعة‬
‫كلية علوم الحاسب وتقنية المعلومات‬
Assignment II
Due by 21 Mon October 2013
Question 1.
Write a program that asks for a number and reports the summation from 1 to the
number
# include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int num;
sum=0 ;
cout << "enter the number,please" << endl;
cin>>num;
for( int count=1;count<=num;count++)
sum=sum+count;
cout<<"The sum of the number is :"<<sum;
getch();
return 0 ;
}
Question 2.
Correct the errors in the following programs:
#include<iostream>
using namespace std;
int main_
{
char a;
int x,y;
cin<<x<<y;
a="$";
cout<<x<<y<<a;
return_ ;
}
Answer
#include<iostream>
using namespace std;
int main()
{
char a;
‫المملكة العربية السعودية‬
Kingdom of Saudi Arabia
Ministry of Higher Education
Majmaah University
Computer Science and Information Technology College
‫وزارة التعليم العالي‬
‫جامعة المجمعة‬
‫كلية علوم الحاسب وتقنية المعلومات‬
int x,y;
cin>>x>>y;
a='$';
cout<<x<<y<<a;
return 0 ;
}
Question 3.
State the order of the operators in each of the following C++ statement and show the
value of x after each is performed:
 X= 7 % 3 + 8 / 4 % 2 -1;
X= 1+8/4%2-1;
X=1+2%2-1;
X=1+0-1;
X=1-1;
X=0;
 x=6 + 2 < 10 && 6/2 ==3 || 10!=10;
x=6+2 <10 && 3==3||10 !=10;
x=8<10 &&3==3 || 10 !=10;
x=1 && 3==3 ||10 !=10;
x=1&& 1 ||10 !=10;
x=1&& 1 ||0;
x= 1||0;
x=1;
‫المملكة العربية السعودية‬
Kingdom of Saudi Arabia
Ministry of Higher Education
Majmaah University
Computer Science and Information Technology College
‫وزارة التعليم العالي‬
‫جامعة المجمعة‬
‫كلية علوم الحاسب وتقنية المعلومات‬
Question 4.
Write c++ condition for each of the following statements:
1.
Testing a number x is odd.
X%2!=0;
2.
Y is even and z equal 5.
)Y %2==0) &&( z==5(;
3.
Testing a string st is the not the same of "Nothing."
St != "Nothing";
4.
x+y is not greater than z and is smaller than 3.
(X+Y) < Z && (X+Y) <3; OR !(X+Y >Z) && X+Y < 3;
5.
Testing a character c is equal to first letter of your name;
c == 'H';
Question 5.
Write a complete c++ program that reads a number then print the number before and
after.
The output should be as following:
Enter a number: 8
The number before is (7) , and the after is (9)
Answer:
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
‫المملكة العربية السعودية‬
Kingdom of Saudi Arabia
Ministry of Higher Education
Majmaah University
Computer Science and Information Technology College
‫وزارة التعليم العالي‬
‫جامعة المجمعة‬
‫كلية علوم الحاسب وتقنية المعلومات‬
// Declare variables
int num,num_after,num_befor;
// read number from user
cout<< "Enter a number: ";
cin>> num;
num_after=num+1;
num_befor=num-1;
cout<< "The number before is ("<<num_befor<< ") and the after
is ("<<num_after<<")";
getch();
}
Question 6.
Write program that print an arrow as follows:
*
***
*****
*
*
*
*
Answer:
#include<iostream>
#include <conio.h>
using namespace std;
void main()
{
cout<<"
*\n";
cout<<"
***\n";
cout<<"
*****\n";
cout<<"
*\n";
cout<<"
*\n";
cout<<"
*\n";
cout<<"
*\n";
getch();
}
Download