0% found this document useful (0 votes)
29 views10 pages

Loops

The document provides multiple C programming examples demonstrating the use of for, while, and do-while loops to print various patterns of asterisks, numbers, and prime numbers. It includes code snippets for creating right-angled triangles, inverted triangles, and sequences of numbers without nested loops. Additionally, it features a program to find prime numbers between two given integers.

Uploaded by

snehasingh270804
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)
29 views10 pages

Loops

The document provides multiple C programming examples demonstrating the use of for, while, and do-while loops to print various patterns of asterisks, numbers, and prime numbers. It includes code snippets for creating right-angled triangles, inverted triangles, and sequences of numbers without nested loops. Additionally, it features a program to find prime numbers between two given integers.

Uploaded by

snehasingh270804
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/ 10

1.

using while ,do while and for loops :

**

***

****

*****

for loop :

#include <stdio.h>

int main() {

int i, j;

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

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

printf("* ");

printf("\n");

return 0;

while loop:

#include <stdio.h>

int main() {

int i = 1, j;

while (i <= 5) {

j = 1;

while (j <= i) {

printf("* ");

j++;

printf("\n");

i++;

return 0;

do-while loop:

#include <stdio.h>
int main() {

int i = 1, j;

do {

j = 1;

do {

printf("* ");

j++;

} while (j <= i);

printf("\n");

i++;

} while (i <= 5);

return 0;

2. using while ,do while and for loops :

*****

****

***

**

for loop:

#include <stdio.h>

int main() {

for (int i = 5; i > 0; i--) {

for (int j = 0; j < i; j++) {

printf("* ");

printf("\n");

return 0;

while loop:

#include <stdio.h>

int main() {

int i = 5;

while (i > 0) {
int j = 0;

while (j < i) {

printf("* ");

j++;

printf("\n");

i--;

return 0;

do-while loop:

#include <stdio.h>

int main() {

int i = 5;

do {

int j = 0;

do {

printf("* ");

j++;

} while (j < i);

printf("\n");

i--;

} while (i > 0);

return 0;

3. using while ,do while and for loops :

*****

****

***

**

for loop:

#include <stdio.h>

void main() {

for (int i = 0; i < 2; i++) {


for (int s = 0; s < i * 2; s++) printf(" "); // Modify this for left alignment

for (int j = 5 - i; j > 0; j--) printf("* ");

printf("\n");

for (int i = 3; i > 0; i--) {

for (int s = 0; s < (5 - i) * 2 + 4; s++) printf(" "); // Adjusted spaces for continuous alignment

for (int j = 0; j < i; j++) printf("* "); // Stars

printf("\n");

while loop:

#include <stdio.h>

void main() {

int i = 0;

while (i < 2) {

int s = 0;

while (s < i * 2) {

printf(" "); // Modify this for left alignment

s++;

int j = 5 - i;

while (j > 0) {

printf("* ");

j--;

printf("\n");

i++;

i = 3;

while (i > 0) {

int s = 0;

while (s < (5 - i) * 2 + 4) {

printf(" "); // Adjusted spaces for continuous alignment

s++;

}
int j = 0;

while (j < i) {

printf("* "); // Stars

j++;

printf("\n");

i--;

do-while loop:

#include <stdio.h>

int main() {

int i = 0, j, s;

do {

s = 0;

do {

printf(" ");

s++;

} while (s < i * 2);

j = 5 - i;

do {

printf("* ");

j--;

} while (j > 0);

printf("\n");

i++;

} while (i < 2);

i = 3;

do {

s = 0;

do {

printf(" ");
s++;

} while (s < (5 - i) * 2 +4);

j = 0;

do {

printf("* ");

j++;

} while (j < i);

printf("\n");

i--;

} while (i > 0);

return 0;

6. 1

22

333

4444

55555

for loop:

#include <stdio.h>

int main() {

int i, j;

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

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

printf("%d",i);

printf("\n");

return 0;

7. 1

12

123

1234

12345
#include <stdio.h>

int main() {

int i, j;

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

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

printf("%d",j);

printf("\n");

return 0;

8. 1

01

101

0101

10101

#include <stdio.h>

int main() {

int i, j;

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

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

printf("%d",(i+j)%2);

printf("\n");

return 0;

9. 5

44

333

2222

11111

#include <stdio.h>

int main() {
int i, j;

for (i = 5; i >= 1; i--) {

for (j = 1; j <= (6 - i); j++) {

printf("%d ", i);

printf("\n");

return 0;

10. 5

54

543

5432

54321

include <stdio.h>

int main() {

int i, j;

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

for (j = 5; j >= (6 - i); j--) {

printf("%d ", j);

printf("\n");

return 0;

11. 1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

#include <stdio.h>

int main() {

int num = 1;

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

for (int j = 1; j <= i; j++) {


printf("%d ", num++);

printf("\n");

return 0;

12.without using nested loops :

**

***

****

*****

#include <stdio.h>

int main() {

int i = 1;

char str[] = "";

while (i <= 5) {

printf("%.*s*\n", i * 2 - 1, "**********");

i++;

return 0;

14. Write a program to print all prime numbers between 2 given integers.

#include <stdio.h>

int main() {

int start, end, i, j, isPrime;

printf("Enter two numbers: ");

scanf("%d %d", &start, &end);

printf("Prime numbers between %d and %d are:\n", start, end);

for (i = start; i <= end; i++) {

if (i < 2)

continue;

isPrime = 1;

for (j = 2; j < i; j++) {

if (i % j == 0) {
isPrime = 0;

break;

if (isPrime) printf("%d ", i);

return 0;

You might also like