B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP C while Loop | TutorialsApp

C while Loop

Explanation (English): In C programming, the while loop is used to repeat a block of code as long as a specified condition is true. It is useful when you don't know in advance how many times you need to loop.

Explanation (Hinglish): C language mein while loop का use तब hota है jab hume koi code block ko baar-baar execute karna hota hai jab tak condition true ho. Yeh tab useful hota hai jab iterations ki sankhya pehle se fix nahi hoti.

Print Numbers 1 to 5
#include <stdio.h>

int main() {
  int i = 1;
  while(i <= 5) {
    printf("%d\n", i);
    i++;
  }
  return 0;
}

Output: 1 2 3 4 5

Prints numbers from 1 to 5 using while loop.

Yeh program 1 se 5 tak numbers print karta hai using while loop.

Sum of First 10 Natural Numbers
#include <stdio.h>

int main() {
  int i = 1, sum = 0;
  while(i <= 10) {
    sum += i;
    i++;
  }
  printf("Sum = %d", sum);
  return 0;
}

Output: Sum = 55

Calculates the sum of first 10 natural numbers.

Yeh program pehle 10 natural numbers ka sum nikalta hai.

Even Numbers from 2 to 10
#include <stdio.h>

int main() {
  int i = 2;
  while(i <= 10) {
    printf("%d ", i);
    i += 2;
  }
  return 0;
}

Output: 2 4 6 8 10

Prints even numbers from 2 to 10.

Yeh program 2 se 10 tak ke even numbers print karta hai.