WebNest
Team/Shahnawaz Sazid/M-6-PROBLEM-SOLVING-WITH-LOOP

Repository

M-6-PROBLEM-SOLVING-WITH-LOOP

View on GitHub ↗
C0 stars0 forks

README

Problem solving with loop

  • post increment
#include <stdio.h>

int main()
{
    int x = 10;
    int y = x++;
    printf("x = %d, y = %d\n", x, y); // x = 11, y = 10
    return 0;
}
  • pre increment
#include <stdio.h>

int main()
{
    int x = 10;
    int y = ++x;
    printf("x = %d, y = %d\n", x, y); // x = 11, y = 11
    return 0;
}
#include <stdio.h>

int main()
{
    int x = 10;
    int y = x++;
    int z = ++y;

    printf("%d", z++);  
    // x=11 , y =10 , z= 11 y=11, z=11, z=11
    return 0;
}
#include <stdio.h>

int main()
{
    int x = 10;
    ++x;
    printf("%d", ++x); //12
    return 0;
}

B. Even Numbers Even Numbers

#include <stdio.h>

int main()
{
    int N;
    scanf("%d", &N);

    if(N ==1){
        printf("-1");
    }

    for (int i = 1; i <= N; i++)
    {
        if (i % 2 == 0)
        {
            printf("%d\n", i);
        }
    }

    return 0;
}

C. Even, Odd, Positive and Negative Even, Odd, Positive and Negative


#include <stdio.h>

int main()
{
    int n;
    int x;
    int even = 0, odd = 0, pos = 0, neg = 0;

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &x);
        // printf("%d\n", x);

        if (x % 2 == 0)
        {
            even++;
        }
        else
        {
            odd++;
        }

        if (x > 0)
        {
            pos++;
        }
        else if (x < 0)
        {
            neg++;
        }
    }

    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);
    printf("Positive: %d\n", pos);
    printf("Negative: %d\n", neg);
    return 0;
}

D. Fixed Password (EOF) Fixed Password (EOF)

#include <stdio.h>

int main()
{
    int pass;
    while (scanf("%d", &pass))
    {
        if (pass == 1999)
        {
            printf("Correct\n");
            break;
        }
        else
        {
            printf("Wrong\n");
        }
    }
    return 0;
}

E. Max Max

#include <stdio.h>
#include<limits.h>

int main()
{
    int totalnums;
    int max = INT_MIN;
    // there is INT_MAX AS WELL
    scanf("%d", &totalnums);

    for (int i = 0; i < totalnums; i++)
    {
        int val;
        scanf("%d", &val);

        if(val > max){
            max = val;
        }
    }

    printf("%d",max);
    return 0;
}

F. Multiplication table Multiplication

#include <stdio.h>

int main()
{
    int num;
    scanf("%d", &num);

    for(int i=1; i<=12; i++){
        printf("%d * %d = %d\n", num, i, num*i);
    }

    return 0;
}

Q. Digits
Digits

#include <stdio.h>

int main()
{
    int t;
    scanf("%d", &t);

    for (int i = 1; i <= t; i++)
    {
        int n;
        scanf("%d", &n);
        do
        {
            printf("%d ", n % 10);
            n /= 10;
        } while (n != 0);
        printf("\n");
    }

    return 0;
}
← Back to profile