Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C Program to Print Prime Numbers In A Given Range

Prime numbers in a given range in C programming

Prime Numbers in a Given Range

A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).

The C program reduces the number of iteration within the for loop. It is made to identify or calculate the prime numbers within a given range of numbers inserted by the user.

Ex:- if user enter a range as 40-50 

In that range 41,43,47 these three number are prime number.

Problem Description

The C program reduces the number of iteration within the for loop. It is made to identify or calculate the prime numbers within a given range of numbers inserted by the user. The program takes the range and identifies all the prime numbers between the given range as well as similarly prints the digits coming under the prime numbers. User is required to take the range as input that will be stored in the variables num1 and num2 respectively.

Ex:- if user enter a range as 40-50 

In that range 41,43,47 these three number are prime number.

Working

  • Step 1: Start
  • Step 2: The user is asked to insert a given range of numbers as an input to finds the prime numbers.
  • Step 3: Find prime numbers within the given range that should be only odd values.
  • Step 4: Check whether the odd numbers are divisible by any of the natural numbers 
  • Step 5: Print the calculated prime numbers.
  • Step 6: Stop

C Code


    #include<stdio.h>
    #include<stdlib.h> 
    void main()
    {
    //To initialize variables
        int num1, num2, i, j, flag, temp, count = 0;  
    //for taking user input
        printf(“Insert the value of num1 and num2 \n);
        scanf(“%d %d”, &num1, &num2);
    //check condition first range is less than 2 
        if (num2 < 2)
        {
            printf(“No prime nums found up-to %d\n, num2);
            exit(0);
        }
    //to display prime numbers
        printf(“Prime nums are \n);
        temp = num1;
    //if num1 modules 2 is equal to zero  
    if( num1 % 2 == 0)
        {   
    //increment on that number.
            num1++;
        }
    //use for loop with first rang and second rang
        for (i = num1; i <= num2; i = i + 2)
        {
            flag = 0;
            for (j = 2; j <= i / 2; j++)
            {
                if ((i % j) == 0)
                {
                    flag = 1;
                    break;
                }
            }
    //check if flag equal to zero
            if (flag == 0)
            {
            //display
                printf(“%d\n, i);
                count++;
            }
        }
    //display total prime number b/w lie on given range 
        printf(“Num of primes between %d & %d = %d\n, temp, num2, count);
    }
    Output
    
    Insert the value of num1 and num2:
    70, 80
    Prime nums are
    71
    73
    79
    83
    Num of primes between 70 and 85 = 4