WebNest
Team/Shahnawaz Sazid/M-3-LOOP-IN-C

Repository

M-3-LOOP-IN-C

View on GitHub ↗
C0 stars0 forks

README

M-3-LOOP-IN C

For Loop

#include<stdio.h>
int main(){
    for(int i=0; i<5; i++){
        printf("Sorry Brother\n");
    }
    return 0;
}

  • even odd
#include <stdio.h>

int main() {
    for (int i = 1; i <= 20; i++) {   // You can change 20 to any limit
        if (i % 2 == 0) {
            printf("%d is even\n", i);
        } else {
            printf("%d is odd\n", i);
        }
    }
    return 0;
}
  • sum
#include <stdio.h>

int main() {
    int n;
    scanf("%d",&n);
    int sum = 0;
    
    for (int i = 1; i <= n; i++) {   
        sum = sum+i;
    }
    printf("Sum = %d", sum);
    return 0;
}
#include <stdio.h>

int main() {
    for (int i = 1; i <= 20; i++) {   
        if (i % 2 == 0) {
            printf("%d is even\n", i);
        } else {
            printf("%d is odd\n", i);
        }
    }
    return 0;
}
  • break (stops entire loop)
#include <stdio.h>

int main() {
    for (int i = 0; i <= 10; i++) {   
        if (i == 5) {
            printf("%d I am Breaking\n", i);
            break;
        } 
        printf("%d\n", i);
    }
    return 0;
}
  • continue (skips the specific targeted loop)
#include <stdio.h>

int main() {
    for (int i = 0; i <= 10; i++) {   
        if (i == 5) {
            continue;
        } 
        printf("%d\n", i);
    }
    return 0;
}
  • while and do while loop

  • while

#include <stdio.h>

int main() {
    int i=0;
    while (i <= 10) {   
        printf("%d\n", i);
        i++;
    }
    return 0;
}
  • do while loop
#include <stdio.h>

int main()
{
    int i = 0;
    do
    {
        printf("%d\n", i);
        i++;
    } while (i < 10);
    return 0;
}
← Back to profile