You are on page 1of 19

Question 1

Write a program to find the sum of all the odd integers in the range of 1 to 100.

Pseudocode
• Using for loop, list the odd numbers between the range of 1 to 100
• Set the limit, i <=99 and the increment, i++
• Find the sum of all odd numbers by assigning i%2!=0 and sum=sum+1
• Display the result

Flowchart

Start

False
Int i = 1

i <=99

True
if (i%2!=0)

Sum=sum + i

i++

Display “The sum of odd integer


between 1 and 100 is “

End
Source Input

#include<iostream>

using namespace std;

int main ()

int sum=0;

for(int i= 1; i <= 99; i++)

if (i%2!=0)

cout<<i<<" ";

sum=sum+i;

cout<<endl;

cout<<"The sum of odd integers between 1 and 100 is " <<sum<<endl;

system ("pause");

return 0;

}
Output
Question 2

Write a program that asks the user to enter a list of integers. The program is to determine the
largest value entered and the number of times it was entered. For example, if the following
series is entered

5 2 15 3 7 15 8 9 5 2 15

it would output that the largest value is 15n and that it was entered 3 times

Pseudocode

 Using while loop, enter 10 integers.


 Using if and else statement, the largest value of the integers and its occurrences can be
determined.
 Print the result.

Flowchart

Start

i=0

max=0

j=1

i < 10

num >= max


Yes
Display the
max == num largest value

j++

No
Yes
Display the
max = num largest value

j=1

No
Yes Display the
max = max largest value

i++

Display the largest


number’s occurrence

End

Source Input

#include<iostream>

using namespace std;

int main()

int i = 0;

int max = 0;
int num;

int j = 1;

cout << "Please enter 10 digits\n";

while(i < 10) {

cin >> num;

if ( num >= max ) {

if ( max == num ) {

j++;

else {

j = 1;

max = num;

} else {

max = max;

i++;

cout << "\nThe largest integer entered is: " ;

cout << max << " The number of occurance is " << j << " times.";

cout << endl;

system ("pause");

return 0;

}
Output

Question 3

Write a program that prints the following patterns separately on below the other. Use for
loops to generate the patterns. All asterisks should be printed by a cout statement in the form
of cout<<”*”
a) Pseudocode

 Using nested loop, for the outer loop, set the value of i = 0
 Set the limit i < 11 and its increment, ++i
 For the inner loop, set the value j = 1
 Set the limit j <= 1 and its increment, ++j
 Print the result in the form of “*”
Flowchart

Start

i=1

i<11

++i

j=1

j<=i

++j

Display the pattern in the


form of “*”.

End
Source Input

#include<iostream>

using namespace std;

int main ()

int i,j;

for(i=1; i<11; ++i)

cout<<"\n";

for (j=1; j<=i; ++j)

cout<<"*";

cout<<endl;

system ("pause");

return 0;

Output
b) Pseudocode

• Using nested loop, for the outer loop, set the value of i = 10
• Set the limit i >=1 and its increment, --i
• For the inner loop, set the value j = 1
• Set the limit j <= 1 and its increment, ++j
• Print the result in the form of “*”

Flowchart

Start

i=10

i >=1

--i

j=1

j<=i

++j

Display the pattern in


the form of “*”.

End
Source Input

#include<iostream>

using namespace std;

int main ()

int i,j;

for(i=10; i>=1; --i)

cout<<"\n";

for (j=1; j<=i; ++j)

cout<<"*";

cout<<endl;

system ("pause");

return 0;

}
Output

c) Pseudocode

 Using nested loop, for the outer loop, set the value of i = 10
 Set the limit i >0 and its increment, i--
 For the inner loop, set the value j = 0
 Set the limit j > 1 and its increment, j--
 Print the result in the form of “(j <= i ? "*" : " " )”
Flowchart

Start

i = 10

i>0

i --

j=0

j >0

j--

Display the pattern in the


form of (j <= i ? "*" : " " )

End
Source Input

#include<iostream>

using namespace std;

int main ()

int i,j;

for(i=10; i>0; i--)

cout<<" \n";

for (j=10; j>0; j--)

cout<<(j <= i ? "*" : " " );

cout<<endl;

system ("pause");

return 0;

}
Output

d) Pseudocode

 Using nested loop, for the outer loop, set the value of i = 1
 Set the limit i <= 10 and its increment, i++
 For the inner loop, set the value j = 10
 Set the limit j >= 1 and its increment, j--
 Print the result in the form of “(j <= i ? "*" : " " )”
Flowchart

Start

i=1

i < = 10

i ++

j = 10

j >= 1

j--

Display the pattern in the form


of (j <= i ? "*" : " " )

End
Source Input

#include<iostream>

using namespace std;

int main ()

int i,j;

for(i=1; i<=10; i++)

cout<<"\n";

for (j=10; j>=1; j--)

cout<<(j <= i ? "*" : " " );

cout<<endl;

system ("pause");

return 0;

}
Output

You might also like