Computer Science, asked by kashwini526, 2 months ago

Program to find prime numbers between 2 to 50 using nested for loop



please give the correct answer❤❤❤❤​

Answers

Answered by nishitkondhia
6

Answer:

# Python program to display all the prime numbers within an interval

lower = 900

upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):

# all prime numbers are greater than 1

if num > 1:

for i in range(2, num):

if (num % i) == 0:

break

else:

print(num)

Answered by MotiSani
0

Program to find prime numbers between 2 to 50 using nested for loop:

#include <iostream>

using namespace std;

int main()

{

   int c;

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

       c = 0;

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

           if((i % j) == 0)

           c += 1;

       }

       if(c == 2)

       cout << i << endl;

   }

   return 0;

}

  • First, we run a loop from 2 to 50 using the loop variable i.
  • Next, we run another nested for loop from 1 to i using the loop variable j, and count the number of factors of i.
  • Finally, we check if the number of factors of i is equal to 2 or not. If it is equal to 2, we print i and the process continues.

#SPJ3

Similar questions