Muhammad Magdi
for ( INITIALIZATION; CONDITION; ASSIGNMENT ) {
BODY
}
INITIALIZATION: What to do before starting the loop?
CONDITION: What is the condition to continue looping?
ASSIGNMENT: What to do after each iteration?
1
2
3
4
#include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 10; i++){
cout << "Hello" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 10; i++){
cout << i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++){
cout << i << endl;
}
return 0;
}
Given x print all the odd numbers from 1 to x -including x-
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
for (int i = 1; i <= x; i++) {
if (i%2 != 0) {
cout << i << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
for (int i = 1; i <= x; i = i + 2) {
cout << i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
if (x%2 == 0 && x > 0) {
cout << "EVEN POSITIVE" << endl;
} else if (x%2 == 0 && x < 0) {
cout << "EVEN NEGATIVE" << endl;
} else if (x%2 != 0 && x > 0) {
cout << "ODD POSITIVE" << endl;
} else if (x%2 != 0 && x < 0) {
cout << "ODD NEGATIVE" << endl;
} else {
cout << "NULL" << endl;
}
}
return 0;
}