Simplified Sequence Problems in C++
Looping Problems
1. Print Numbers 1 to 10
Use a loop to print numbers from 1 to 10.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
2. Sum of First N Numbers
Ask the user to enter a number N and find the sum of the first N natural numbers.
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum is: " << sum << endl;
return 0;
}
3. Multiplication Table
Ask the user for a number and print its multiplication table up to 10.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
Simplified Sequence Problems in C++
for (int i = 1; i <= 10; i++) {
cout << num << " x " << i << " = " << num * i << endl;
}
return 0;
}
4. Even Numbers from 1 to N
Ask for a number N, then print all even numbers from 1 to N.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
for (int i = 2; i <= n; i += 2) {
cout << i << " ";
}
cout << endl;
return 0;
}
5. Factorial of a Number
Ask the user for a number and compute its factorial using a loop.
#include <iostream>
using namespace std;
int main() {
int n, fact = 1;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
fact *= i;
}
cout << "Factorial is: " << fact << endl;
return 0;
}
Conditional Problems
1. Check Even or Odd
Simplified Sequence Problems in C++
Ask the user for a number and tell whether it's even or odd.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << "Even" << endl;
else
cout << "Odd" << endl;
return 0;
}
2. Find the Greatest of Two Numbers
Ask for two numbers and display the greater one.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
if (a > b)
cout << a << " is greater." << endl;
else if (b > a)
cout << b << " is greater." << endl;
else
cout << "Both are equal." << endl;
return 0;
}
3. Grade Checker
Input a student's score and display the grade (A, B, C, D, or F).
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Enter score: ";
Simplified Sequence Problems in C++
cin >> score;
if (score >= 90)
cout << "Grade A" << endl;
else if (score >= 80)
cout << "Grade B" << endl;
else if (score >= 70)
cout << "Grade C" << endl;
else if (score >= 60)
cout << "Grade D" << endl;
else
cout << "Grade F" << endl;
return 0;
}
4. Check for Positive, Negative, or Zero
Ask the user for a number and determine if it's positive, negative, or zero.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0)
cout << "Positive" << endl;
else if (num < 0)
cout << "Negative" << endl;
else
cout << "Zero" << endl;
return 0;
}
5. Check for Leap Year
Ask for a year and determine if it's a leap year.
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
Simplified Sequence Problems in C++
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
cout << "Leap year" << endl;
else
cout << "Not a leap year" << endl;
return 0;
}