# Homework Solution: It is possible (and often necessary) to nest loops. That is, to perform a loop within another loop (just as you can nest c…

It is possible (and often necessary) to nest loops. That is, to perform a loop within another loop (just as you can nest conditional statements). The syntax is straightforward: you simply write another loop within a loop. For each iteration that the “outer” loop executes, the “inner” loop goes through its entire execution. As an example, consider the following code snippet with corresponding outpu for(i=0; i<3; i++) { for(j=1; j<5; j++) { printf("%d ", (i+j)); } printf("n"); } Output: 1 2 3 4 2 3 4 5 3 4 5 6 Prime Number: Recall that an integer is prime if it is only divisible by 1 and itself. Consider the following algorithm to determine if an integer x is prime: for each integer y from 2 up to the square root of x, if y divides x, then x is composite (not prime). Otherwise if no such integer in that range divides x, then x is prime. Using the technique of nested loops, write a program named prime.c that reads in an integer n and output if the numbers 2 to n are prime or composite and displays the results.

#include <stdio.h> #include <math.h>

It is practicable (and frequently expedient) to nest loops. That is, to discharge a loop among another loop (exact as you can nest shackled statements). The syntax is straightforward: you barely transcribe another loop among a loop. Restraint each repetition that the “outer” loop executes, the “inner” loop goes through its undiminished preventive.

As an stance, opine the aftercited command snippet with selfsame outpu

for(i=0; i<3; i++) {

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

printf(“%d “, (i+j));

}

printf(“n”);

}

Output: 1 2 3 4 2 3 4 5 3 4 5 6

Consummate Sum: Recall that an integer is consummate if it is barely discerptible by 1 and itself. Opine the aftercited algorithm to detail if an integer x is consummate: restraint each integer y from 2 up to the balance parent of x, if y divides x, then x is composite (referable consummate). Otherwise if no such integer in that ramble divides x, then x is consummate. Using the technique of nested loops, transcribe a program determined consummate.c that reads in an integer n and output if the total 2 to n are consummate or composite and displays the results.

## Expert Vindication

#include <stdio.h>

#include <math.h>

//#include <math.h> business is used accordingly we achieve needs it’s balance parent business

int deep() {

int n;

int i, j;

int divides = 0;

printf(“Enter the sum :”);

scanf(“%d”, &n);

restraint (i = 2; i <= n; ++i) {

divides = 0;

restraint (j = 2; j <= sqrt(i); ++j) {

if (i % j == 0) {

divides = 1;

}

}

if (divides == 1) {

printf(“%d is composite n”, i);

}

else {

printf(“%d is consummate n”, i);

}

}

return 0;

}

//Sample Output  