FEU-IT
CCS Department Computer Programming 1 – Machine Problem
Name: Date Performed: __________________
Course/Yr: Date Submitted: __________________
MP # 5
Using different repetition control structures
1. Using while loop, write a program that will ask the user for a positive integer number. Display all numbers from
1 to the user’s input. Consider the sample output.
Enter a number: 10
1 2 3 4 5 6 7 8 9 10
#include <iostream>
using namespace std;
main (){
int num, i;
cout<<"Enter a number:";
cin>>num;
i=1;
while (num>=i){
cout<<i<<"\n";
i++;
}
return 0;
}
FEU-IT
CCS Department Computer Programming 1 – Machine Problem
2. Using do-while loop, create a program that will display the following output
Output:
5 10 15 20 25 30 35 40 45 50
#include <iostream>
using namespace std;
int main()
int x = 5;
do{
cout <<x<<endl;
x = x + 5;
}while(x<55);
}
FEU-IT
CCS Department Computer Programming 1 – Machine Problem
3. Using, for loop, write a program that will ask the user for a positive integer number. Display the
sum of all numbers from 1 to the user’s input. Consider the sample output.
Enter a number: 10
Sum of all numbers from 1 to 10: 55
#include <iostream>
using namespace std;
int main()
{
int x, sum = 0;
cout << "Enter a number: ";
cin >> n;
for (int x = 1; x <= n; ++x) {
sum += x;
}
cout<< "Sum of all numbers : "<<sum;
return 0;
}