Posts

LOOP QUESTIONS

Image
Q1: Print numbers from 1 to 5 for(int i=1;i<=5;i++)     cout << i << " "; Outpu t: 1 2 3 4 5 Q2: Print numbers from 5 to 1 for(int i=5;i>=1;i--)     cout << i << " ";   Output: 5 4 3 2 1 Q3: Find sum of first 5 natural numbers int sum=0; for(int i=1;i<=5;i++)     sum+=i; cout << sum; Output : 15 Q4: Print even numbers between 1 and 10 for(int i=1;i<=10;i++) {     if(i%2==0)         cout << i << " "; } Output : 2 4 6 8 10 Q5: Print odd numbers between 1 and 1 0 for(int i=1;i<=10;i++) {     if(i%2!=0)         cout << i << " "; } Output: 1 3 5 7 9 Q6: Print square of numbers from 1 to 5 for(int i=1;i<=5;i++)     cout << i*i << " "; Output: 1 4 9 16 25 Q7: Print multiplication table of 2 for(int i=1;i<=10;i++)     cout << 2*i << " "; Output : 2 4 6 8 10 12 14 16 18 20 Q8: Find ...

Loops in c++

Image
Loops in C++ – Complete Guide for Beginners 1. What is a Loop? A loop is used to execute a block of code again and again until a condition becomes false. Real-life example: Brushing teeth every day until the day ends Attending classes every day until semester ends In programming, loops save time and reduce code repetition. 2. Why Loops Are Important in C++? Avoid writing same code multiple times Make programs shorter and cleaner Required for arrays, patterns, searching, sorting Used in almost every real program 3. Types of Loops in C++ C++ provides 3 main types of loops : for loop while loop do-while loop 4. for Loop in C++ Syntax: for(initialization; condition; update) { // code } How it works: Initialization runs once Condition is checked Code executes if condition is true Update runs Steps 2–4 repeat Example: for(int i = 1; i <= 5; i++) { cout << i << endl; } Output: 1 2 3 4 5 When to use for loop? When number of iterations is known 5. while Loop in C++ Synt...

Switch Case Statement in C++

Image
Introduction The switch statement is a control statement in programming (like C, C++, Java) that is used to execute one block of code out of many options based on the value of a variable or expression. Instead of writing many if–else conditions, we use a switch statement when we need to compare one variable with multiple fixed values . Why we use switch statement? Makes code cleaner and easier to read Better than long if–else ladder Useful when choices are fixed values (menu, days, options, etc.) Basic syntax (C / C++) switch(expression) {     case value1:         // code         break;     case value2:         // code         break;     case value3:         // code         break;     default:         // code if no case matches } How switch statement works The expression is evaluated once....

20 IF–ELSE Statement Questions in C++ with Answers (Beginner Friendly)

Image
  Introduction The if–else statement is one of the most important decision-making statements in C++ programming . It allows a program to execute different blocks of code based on a condition. If you are a beginner , preparing for: College exams Coding interviews Competitive programming then practicing if–else questions is very important. 👉 In this article, you will learn 20 important if–else statement questions in C++ with answers and examples . What is IF–ELSE Statement in C++? The if–else statement is used to check conditions . Syntax: if (condition) { // code if condition is true } else { // code if condition is false } 20 Important IF–ELSE Statement Questions in C++ 1. Program to check whether a number is even or odd int n = 10 ; if (n % 2 == 0 ) cout << "Even" ; else cout << "Odd" ; ✅ Output: Even 2. Check whether a number is positive, negative or zero int n = -5 ; if (n > 0 ) cout ...

If-Else Statement in C++

Image
  Introduction In real life, we take decisions every day. If it is raining, take an umbrella If marks are good, celebrate Else, study harder Similarly, in programming, we often need to make decisions . In C++, this decision-making is done using the if–else statement . The if–else statement allows a program to choose different paths of execution based on conditions. What is an If–Else Statement? An if–else statement checks a condition and executes code based on whether the condition is true or false . If the condition is true , the if block runs If the condition is false , the else block runs This helps the program behave intelligently instead of running the same code every time. Why Do We Need If–Else in C++? Without decision-making statements: Programs would run only in one direction No logical thinking would be possible Real-world problems cannot be solved Uses of if–else: Checking age eligibility Student grading system Login validation ...