Sunday, 18 January 2026

Programming for Problem Solving-Control Structures

 1. Check Positive or Negative (if)

#include <stdio.h>

#include<conio.h>

int main() {

    int n = 5;

    if (n > 0)

        printf("Positive number");

    return 0;

   getch();

}


Output

Positive number


2. Check Even or Odd (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int n = 4;

    if (n % 2 == 0)

        printf("Even number");

    else

        printf("Odd number");

    return 0;

    getch();

}


Output

Even number


3. Find Largest of Two Numbers (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 10, b = 20;

    if (a > b)

        printf("A is greater");

    else

        printf("B is greater");

    return 0;

    getch();

}

Output

B is greater


4. Check Voting Eligibility (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int age = 18;

    if (age >= 18)

        printf("Eligible to vote");

    else

        printf("Not eligible");

    return 0;

    getch();

}


Output

Eligible to vote


5. Check Leap Year (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int year = 2024;

    if (year % 4 == 0)

        printf("Leap year");

    else

        printf("Not a leap year");

    return 0;

    getch();

}


Output

Leap year


6. Largest of Three Numbers (else–if ladder)

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 10, b = 30, c = 20;

    if (a > b && a > c)

        printf("A is largest");

    else if (b > c)

        printf("B is largest");

    else

        printf("C is largest");

    return 0;

    getch();

}


Output

B is largest


7. Grade System (else–if ladder)

#include <stdio.h>

#include<conio.h>

int main() {

    int marks = 85;

    if (marks >= 90)

        printf("Grade A");

    else if (marks >= 75)

        printf("Grade B");

    else if (marks >= 50)

        printf("Grade C");

    else

        printf("Fail");

    return 0;

    getch();

}


Output

Grade B


8. Simple Calculator (switch)

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 10, b = 5;

    char op = '+';

    switch (op) {

        case '+': printf("Sum = %d", a + b); break;

        case '-': printf("Difference = %d", a - b); break;

        case '*': printf("Product = %d", a * b); break;

        case '/': printf("Quotient = %d", a / b); break;

    }

    return 0;

    getch();

}

Output

Sum = 15


9. Print Numbers 1 to 5 (for loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

    for (i = 1; i <= 5; i++)

        printf("%d ", i);

    return 0;

    getch();

}

Output

1 2 3 4 5


10. Sum of First 5 Numbers (for loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int i, sum = 0;

    for (i = 1; i <= 5; i++)

        sum += i;

    printf("Sum = %d", sum);

    return 0;

    getch();

}

Output

Sum = 15


11. Print Numbers Using while Loop

#include <stdio.h>

#include<conio.h>

int main() {

    int i = 1;

    while (i <= 5) {

        printf("%d ", i);

        i++;

    }

    return 0;

    getch();

}

Output

1 2 3 4 5


12. Reverse a Number (while loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int n = 123, rev = 0;

    while (n != 0) {

        rev = rev * 10 + n % 10;

        n /= 10;

    }

    printf("Reverse = %d", rev);

    return 0;

    getch();

}

Output

Reverse = 321


13. Print Numbers Using do–while Loop

#include <stdio.h>

#include<conio.h>

int main() {

    int i = 1;

    do {

        printf("%d ", i);

        i++;

    } while (i <= 5);

    return 0;

}

Output

1 2 3 4 5


14. Use of break Statement

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

    for (i = 1; i <= 5; i++) {

        if (i == 3)

            break;

        printf("%d ", i);

    }

    return 0;

    getch();

}

Output

1 2


15. Use of continue Statement

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

    for (i = 1; i <= 5; i++) {

        if (i == 3)

            continue;

        printf("%d ", i);

    }

    return 0;

    getch();

}

Output

1 2 4 5