#include<iostream> #include <iomanip> using namespace std; int main () { int i,o
ID: 3805892 • Letter: #
Question
#include<iostream>
#include <iomanip>
using namespace std;
int main () {
int i,odd,count=0,n,sum=0; /initialize variables
cout<<"Even numbers between 2 to 40 ";
for(i=2;i<=40;i++){ //loop from 2 to 4
if(i%2 == 0){ //check if even ,true then print
count++;
sum++;
std::cout.width(5); std::cout << std::right <<i;//right alignment and width is set to 5
}
if(count==10){
count=0;//each ine 10 after 10,pint in next line
cout<<" ";
}
}
cout<<"Number of Even numbers is "<<sum<<" Odd numbers from 99 to 3 ";
count=0,sum=0;
for(i=99;i>=3;i--){
if(i%3==0){//check if multiple of 3
count++;
sum++;
std::cout.width(5); std::cout << std::right <<i; //allign to right
}
if(count==10){ //each ine 10 after 10,pint in next line
count=0;
cout<<" ";}
}
cout<<" Number of odd numbers is "<<sum<<" Number of powers of 2 in the range of 2^1 to 2^20 ";
long int p=2;
int x=0,c=0;
cout << std::setw(5);
do{
x++;
p=2*p;
std::cout.width(10); std::cout << std::right << p;
if(x==8){ //print in ext line after 8 values
x=0;
cout<<" ";
}
c++;
}while(p<=1048576);
cout<<" Number of numbers is "<<c<<" Numbers from 2^20 down to 2 ";
p=1048576,c=0,x=0;
while(p>=2){
std::cout.width(10); std::cout << std::right << p;
p=p/2;
x++;
if(x==8){ //after 8 values print in next line
x=0;
cout<<" ";
}
c++;
}
int num;
cout<<" Loop until user enters valid integer i.e not a negative ";
cout<<"Enter an integer : ";
cin>>num;
while(num<0){
cout<<"Invalid entry please re-enter a number :";
cin>>num;
}
cout<<"You have entered "<<num<<" ";
return 0;
}
how to write psedue code?
Explanation / Answer
You dont have to use Programming conventions [Do not declare variables, Do not use { , } , ; etc..Use Indentations etc ]
You can write Like thus
Algorithm MyProgram()
print "Even numbers between 2 to 40 ";
for i = 2 to 40 //loop from 2 to 4
if i%2 == 0 //check if even ,true then print
count++;
sum++;
print i
if count==10
count = 0 //each ine 10 after 10,pint in next line
print "Number of odd numbers is " , sum
print "Number of powers of 2 in the range of 2^1 to 2^20 "
p := 2
x=0
c=0;
do
x++;
p=2*p;
print p
if x==8 //print in ext line after 8 values
x=0;
while p< = 1048576
print "Number of numbers is ", c
print "Numbers from 2^20 down to 2 "
p=1048576
c=0
x=0
while p>=2
print p
p=p/2
x++
if x==8 //after 8 values print in next line
x=0;
c++
print Loop until user enters valid integer i.e not a negative "
print Enter an integer : "
read (num)
while num<0
print "Invalid entry please re-enter a number :"
read (num)
print "You have entered ", num
End
No need to write Main etc ..It gives us the High Level that can be converted into code using any language..
Pseudo Code is LangUage INDEPENDENT
This is the pseudo code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.