Some counting
Use your newly acquired knowledge of the for loop to complete the following tasks. Print all values to console in each case.
• Write a program that counts up from 0 to 50 in increments of 1.
• Write a program that counts down from 50 to 0 in decrements of 1.
• Write a program that counts up from 30 to 50 in increments of 1.
• Write a program that counts down from 50 to 10 in decrements of 2.
• Write a program that counts up from 100 to 200 in increments of 5.
#include<iostream>
using namespace std;
int main(){
for(int i=0; i<=50; i++){
cout<<i<<" ";
}
}
#include<iostream>
using namespace std;
int main(){
for(int i=50; i>=0; i--){
cout<<i<<" ";
}
}
#include<iostream>
using namespace std;
int main(){
for(int i=30; i<=50; i++){
cout<<i<<" ";
}
}
#include<iostream>
using namespace std;
int main(){
int i=50;
while(i>=10){
cout<<i<<" ";
i -= 2;
}
}
#include<iostream>
using namespace std;
int main(){
int i=100;
while(i<=200){
cout<<i<<" ";
i += 5;
}
}
Comments
Leave a comment