0% found this document useful (0 votes)
5 views

Sheet 04

Uploaded by

omar.ahmed.nes6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Sheet 04

Uploaded by

omar.ahmed.nes6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

‫جامعة عين شمس‬

‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

CSE141 Introduction to Computer Programming


Sheet 04
1. Draw the flowchart of a program that asks the user for a positive number. If the
number is negative, the program asks him again. If the number is positive, the
program prints "positive" and ends.

1/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

2. Draw the flowchart of a program to find Even number between 1 to 50.

2/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

3. Draw the flowchart of a program to find sum of series 1+3+5+…..+N, Where N


is positive odd Integer.

3/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

4. Draw the flowchart of a program that calculates 24 using a loop approach.

4/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

5. YOUR TURN Draw a flowchart to obtain to obtain the factorial of 20.

6. For the following program segment:

int i, j;
for (i = 0; i < 10; ++i)
{
for (j = 0; j < i; j++)
cout << i * j;
cout << "Good Luck";
}

a) How many times does the first call to cout execute? Answer: 45
b) How many times does the second call to cout execute? Answer: 10
c) What is the last value displayed? Answer: "Good Luck"
d) Evaluate the output of the program manually showing each step in detail.
i j (j<i) First cout Second cout
i=0 False Good Luck → 1
i=1 j=0 True i*j=0 → 1
Good Luck → 2
i=2 j=0 True i*j=0 → 2
5/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

j=1 True i*j=2 → 3


Good Luck → 3
i=3 j=0 True i*j=0 → 4
j=1 True i*j=3 → 5
j=2 True i*j=6 → 6
Good Luck → 4
i=4 j=0 True i*j=0 → 7
j=1 True i*j=4 → 8
j=2 True i*j=8 → 9
j=3 True i*j=12 → 10
Good Luck → 5
i=5 j=0 True i*j=0 → 11
j=1 True i*j=5 → 12
j=2 True i*j=10 → 13
j=3 True i*j=15 → 14
j=4 True i*j=20 → 15
Good Luck → 6
i=6 j=0 True i*j=0 → 16
j=1 True i*j=6 → 17
j=2 True i*j=12 → 18
j=3 True i*j=18 → 19
j=4 True i*j=24 → 20
j=5 True i*j=30 → 21
Good Luck → 7
i=7 j=0 True i*j=0 → 22
j=1 True i*j=7 → 23
j=2 True i*j=14 → 24
j=3 True i*j=21 → 25
j=4 True i*j=28 → 26
j=5 True i*j=35 → 27
j=6 True i*j=42 → 28
Good Luck → 8
i=8 j=0 True i*j=0 → 29
j=1 True i*j=8 → 30
j=2 True i*j=16 → 31
6/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

j=3 True i*j=24 → 32


j=4 True i*j=32 → 33
j=5 True i*j=40 → 34
j=6 True i*j=48 → 35
j=7 True i*j=56 → 36
Good Luck → 9
i=9 j=0 True i*j=0 → 37
j=1 True i*j=9 → 38
j=2 True i*j=18 → 39
j=3 True i*j=27 → 40
j=4 True i*j=36 → 41
j=5 True i*j=45 → 42
j=6 True i*j=54 → 43
j=7 True i*j=63 → 44
j=8 True i*j=72 → 45
Good Luck → 10

7. Trace the following C program’s segments showing its output:

int main()
{
long num=654;
do
{
cout << (num%10);
num /= 10;
} while (num != 0);
cout << endl;
return (0(;
}

Answer: 456

8. Trace the following program’s segments showing its output:

7/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

int main()
{
float grade;
cin >> grade;
if (grade > 75)
cout << "very good\n";
else if (grade > 85)
cout << "excellent\n";

return 0;

Answer:

when grade:50, output: <nothing>


when grade:80, output: very good
when grade:95, output: very good

9. Trace the following program’s segments showing its output:


int main()
{
int sum = 0, index;
for (index = 0; index < 30; index++);
sum += index;
cout << sum;
return 0;
}

Answer: 30

10. Trace the following program’s segments showing its output:


int main()
{
int sum = 0;
int n = 10;
while (n <= 100)
sum += n * n;
return 0;
}

Answer: Infinite loop

11. Rewrite the following program segment using while() loop.


8/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

int x, i, n, count=0;
for (i=0; i<n; i++)
{
cin >> x;
if (x==i) ++count;
}

Answer:
int x, i = 0, n, count = 0;

while (i < n) {
cin >> x;
if (x == i) {
++count;
}
++i;
}

12. Write a program to calculate the following series summations:


a) 1 + 2 + 3 + 4 + .......... +n
2 2 2 2 2

1 1 1 1 1
b) 1 + + + + + ............ +
2 3 4 5 n
3 5 7 2n - 1
c) 1 + 2 + 2 + 2 + ... 2
2 3 4 n
d) k + 2 * k + 3 * k + 4 * k + ........... + N * k
1 1 1 1 1
e) + + + + ........... +
A A + B A + 2B A + 3B A + NB

13. Write a program to determine whether a given number is a prime.


(A prime number is only divisible by 1 and itself)

14. Write a program that computes and prints the sum of all integers from 1 to 100,
and divisible by 2 and 3.

15. Write a program to read the number of exam scores then read the exam scores
themselves. Your program needs to find and print the average.

16. (Extra) Draw the following pattern:

9/3
‫جامعة عين شمس‬
‫كليـة الهندسة‬
‫برامج الساعات المعتمدة‬

‫*‬
‫***‬
‫*****‬
‫*******‬

‫‪10/3‬‬

You might also like