0% found this document useful (0 votes)
5 views

conditional_statemens_and_loops_problems

The document presents the top 10 coding questions focused on conditional statements and loops, including problems such as checking if a number is even or odd, finding the largest of three numbers, and creating a simple calculator. Each problem is accompanied by solutions in C++, Java, and Python. The guide encourages consistent practice to enhance coding skills.

Uploaded by

Gurram Vinay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

conditional_statemens_and_loops_problems

The document presents the top 10 coding questions focused on conditional statements and loops, including problems such as checking if a number is even or odd, finding the largest of three numbers, and creating a simple calculator. Each problem is accompanied by solutions in C++, Java, and Python. The guide encourages consistent practice to enhance coding skills.

Uploaded by

Gurram Vinay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Top 10 Coding Questions on Conditional

Statements & Loops


📌 1. If-Else: Check Even or Odd
Problem: Write a program to check if a given number is even or odd using an if-else
statement.

📌 2. Nested If-Else: Find the Largest of Three Numbers


Problem: Given three numbers, find the largest using if-else or nested if-else.

📌 3. If-Else: Check Leap Year


Problem: Write a program to check whether a given year is a leap year or not.

📌 4. Switch Case: Simple Calculator


Problem: Design a calculator that takes two numbers and an operator (+, -, *, /) as input
and performs the operation.

📌 5. While Loop: Count Digits in a Number


Problem: Given a number, count the number of digits in it using a loop.

📌 6. For Loop: Sum of First N Natural Numbers


Problem: Given a number N, find the sum of the first N natural numbers using a loop.

📌 7. While Loop: Reverse a Number


Problem: Reverse a given number using a loop.

📌 8. For Loop & If-Else: Check Prime Number


Problem: Write a program to check if a number is prime.

📌 9. Loops: Print Fibonacci Series (N Terms)


Problem: Print the first N terms of the Fibonacci series

📌 10. Nested Loops: Print a Pyramid Pattern


Problem: Print a pyramid pattern for N rows.
📌 1. If-Else: Check Even or Odd
Problem: Write a program to check if a given number is even or odd using an if-else
statement.

Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int num;
cin >> num;
if (num % 2 == 0) cout << "Even";
else cout << "Odd";
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println((num % 2 == 0) ? "Even" : "Odd");
}
}

Python:
num = int(input())
print("Even" if num % 2 == 0 else "Odd")

📌 2. Nested If-Else: Find the Largest of Three Numbers


Problem: Given three numbers, find the largest using if-else or nested if-else.
Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << max(a, max(b, c));
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
System.out.println(Math.max(a, Math.max(b, c)));
}
}

Python:
a, b, c = map(int, input().split())
print(max(a, b, c))

📌 3. If-Else: Check Leap Year


Problem: Write a program to check whether a given year is a leap year or not.

Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int year;
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << "Leap Year";
else
cout << "Not a Leap Year";
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
System.out.println("Leap Year");
else
System.out.println("Not a Leap Year");
}
}

Python:
year = int(input())
print("Leap Year" if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else "Not
a Leap Year")

📌 4. Switch Case: Simple Calculator


Problem: Design a calculator that takes two numbers and an operator (+, -, *, /) as input
and performs the operation.
Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int a, b;
char op;
cin >> a >> op >> b;
switch(op) {
case '+': cout << a + b; break;
case '-': cout << a - b; break;
case '*': cout << a * b; break;
case '/': cout << (b != 0 ? a / b : 0); break;
default: cout << "Invalid Operator";
}
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
char op = sc.next().charAt(0);
int b = sc.nextInt();
switch(op) {
case '+': System.out.println(a + b); break;
case '-': System.out.println(a - b); break;
case '*': System.out.println(a * b); break;
case '/': System.out.println(b != 0 ? a / b : 0); break;
default: System.out.println("Invalid Operator");
}
}
}

Python:
a, op, b = input().split()
a, b = int(a), int(b)
print(eval(f"{a}{op}{b}"))

📌 5. While Loop: Count Digits in a Number


Problem: Given a number, count the number of digits in it using a loop.

Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int num, count = 0;
cin >> num;
while (num) {
num /= 10;
count++;
}
cout << count;
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt(), count = 0;
while (num > 0) {
num /= 10;
count++;

System.out.println(count);
}
}

Python:
num = input()
print(len(num))

📌 6. For Loop: Sum of First N Natural Numbers


Problem: Given a number N, find the sum of the first N natural numbers using a loop.

Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; i++) sum += i;
cout << sum;
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), sum = 0;
for (int i = 1; i <= n; i++) sum += i;
System.out.println(sum);
}
}

Python:
n = int(input())
print(n * (n + 1) // 2)

📌 7. While Loop: Reverse a Number


Problem: Reverse a given number using a loop.

Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int num, rev = 0;
cin >> num;
while (num) {
rev = rev * 10 + num % 10;
num /= 10;
}
cout << rev;
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt(), rev = 0;
while (num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
System.out.println(rev);
}
}

Python:
num = input()
print(num[::-1])

📌 8. For Loop & If-Else: Check Prime Number


Problem: Write a program to check if a number is prime.

Solution:
C++:
#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int main() {
int num;
cin >> num;
cout << (isPrime(num) ? "Prime" : "Not Prime");
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(isPrime(num) ? "Prime" : "Not Prime");
}
}

Python:
def is_prime(n):
if n < 2: return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0: return False
return True

num = int(input())
print("Prime" if is_prime(num) else "Not Prime")

📌 9. For Loop: Print Fibonacci Series Up to N Terms


Problem: Write a program to print the Fibonacci series up to N terms using a for loop.

Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int n, a = 0, b = 1, next;
cin >> n;
for (int i = 0; i < n; i++) {
cout << a << " ";
next = a + b;
a = b;
b = next;
}
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0, b = 1, next;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
next = a + b;
a = b;
b = next;
}
}
}

Python:
n = int(input())
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

📌 10. Nested Loops: Print a Pyramid Pattern


Problem: Print a pyramid pattern for N rows.

Solution:
C++:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) cout << " ";
for (int j = 1; j <= 2 * i - 1; j++) cout << "*";
cout << endl;
}
return 0;
}

Java:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
}
}

Python:
n = int(input())
for i in range(1, n + 1):
print(' ' * (n - i) + '*' * (2 * i - 1))

🎉 Congratulations on Completing This Guide! 🚀


You have successfully worked through some of the most important Conditional

💡
Statements & Loops problems. Keep practicing, stay consistent, and keep building
your coding skills!

For more such content, follow me on Instagram: code.abhii07 📲🔥


Happy Coding! 😊🎯

You might also like