0% found this document useful (0 votes)
2 views5 pages

C Loops Assignment Typed

The document contains multiple C programming examples demonstrating the use of while loops, do-while loops, and for loops. Each example showcases different functionalities such as printing numbers, calculating sums, generating factorials, and creating patterns. The code snippets are structured to illustrate basic programming concepts and control flow in C.

Uploaded by

lalatele39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

C Loops Assignment Typed

The document contains multiple C programming examples demonstrating the use of while loops, do-while loops, and for loops. Each example showcases different functionalities such as printing numbers, calculating sums, generating factorials, and creating patterns. The code snippets are structured to illustrate basic programming concepts and control flow in C.

Uploaded by

lalatele39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

While Loop - 1: Print numbers 1 to 10

#include <stdio.h>
#include <conio.h>

void main() {
int i = 1;
while (i <= 10) {
printf("%d\n", i);
i++;
}
getch();
}

While Loop - 2: Sum of first 10 natural numbers


#include <stdio.h>
#include <conio.h>

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

While Loop - 3: Reverse print from 10 to 1


#include <stdio.h>
#include <conio.h>

void main() {
int i = 10;
while (i >= 1) {
printf("%d\n", i);
i--;
}
getch();
}

While Loop - 4: Print even numbers from 2 to 20


#include <stdio.h>
#include <conio.h>

void main() {
int i = 2;
while (i <= 20) {
printf("%d ", i);
i += 2;
}
getch();
}

While Loop - 5: Factorial of 5


#include <stdio.h>
#include <conio.h>

void main() {
int n = 5, fact = 1, i = 1;
while (i <= n) {
fact *= i;
i++;
}
printf("Factorial = %d\n", fact);
getch();
}

Do-While Loop - 1: Print numbers 1 to 10


#include <stdio.h>
#include <conio.h>

void main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 10);
getch();
}

Do-While Loop - 2: Menu until user selects Exit


#include <stdio.h>
#include <conio.h>

void main() {
int choice;
do {
printf("1. Start\n2. Help\n3. Exit\nEnter choice: ");
scanf("%d", &choice);
} while (choice != 3);
printf("Exited!\n");
getch();
}

Do-While Loop - 3: Sum of digits of 1234


#include <stdio.h>
#include <conio.h>

void main() {
int num = 1234, sum = 0, digit;
do {
digit = num % 10;
sum += digit;
num /= 10;
} while (num > 0);
printf("Sum of digits = %d\n", sum);
getch();
}

Do-While Loop - 4: Multiplication table of 7


#include <stdio.h>
#include <conio.h>

void main() {
int i = 1;
do {
printf("7 x %d = %d\n", i, 7 * i);
i++;
} while (i <= 10);
getch();
}

Do-While Loop - 5: Power of 2^5


#include <stdio.h>
#include <conio.h>

void main() {
int base = 2, exp = 5, result = 1, i = 1;
do {
result *= base;
i++;
} while (i <= exp);
printf("2^5 = %d\n", result);
getch();
}

For Loop - 1: Print numbers 1 to 10


#include <stdio.h>
#include <conio.h>

void main() {
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
getch();
}

For Loop - 2: Square of first 5 numbers


#include <stdio.h>
#include <conio.h>

void main() {
for (int i = 1; i <= 5; i++) {
printf("%d^2 = %d\n", i, i * i);
}
getch();
}

For Loop - 3: Fibonacci series up to 10 terms


#include <stdio.h>
#include <conio.h>

void main() {
int a = 0, b = 1, c;
printf("%d %d ", a, b);
for (int i = 3; i <= 10; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
getch();
}

For Loop - 4: Check if 17 is prime


#include <stdio.h>
#include <conio.h>

void main() {
int n = 17, flag = 1;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = 0;
break;
}
}
if (flag)
printf("%d is prime\n", n);
else
printf("%d is not prime\n", n);
getch();
}

For Loop - 5: Print right-angle triangle pattern


#include <stdio.h>
#include <conio.h>

void main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
getch();
}

You might also like