Order the sequence to get the correct algorithm for the below given scenario
A famous music director is now composing a pop album. To speed up his composition of
generating unpredictable rhythms, he wants the list of prime numbers available in a range of
numbers
Can you help him out?
1.Read the values for N and M
2.If N is divisible by
Skip loop
3.While I is smaller than N
4.While N is smaller than M
Initialize I to 2
5.Increment I
6.Increment N
7.I N is equal to 1
Print N
Answers
Answer:
#include <stdio.h>
int main() {
int low, high, i, flag, temp;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
// swap numbers if low is greather than high
if (low > high) {
temp = low;
low = high;
high = temp;
}
printf("Prime numbers between %d and %d are: ", low, high);
while (low < high) {
flag = 0;
// ignore numbers less than 2
if (low <= 1) {
++low;
continue;
}
for (i = 2; i <= low / 2; ++i) {
if (low % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
return 0;
}
Answer:
Explanation:
Read the values for N and M
While N is smaller than M
Initialize I to 2
While I is smaller than N
If N is divisible by I
Skip loop
Increment I
If N is equal to I
Print N
Increment N