Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Java program to Display all prime numbers between two intervals

Code to Display all prime numbers in an intervals of C

Posted on November 26, 2020November 29, 2020

Table of Contents

  • Code to Display all prime numbers in an intervals of C
    • Display prime numbers between two intervals
      • Display prime numbers between two intervals using for loops
      • Display prime numbers between two intervals using while loop
      • Display prime numbers between two intervals using do-while loop
      • Display prime numbers between two intervals using function

Code to Display all prime numbers in an intervals of C

In this article, we will discuss the concept of Code to Display all prime numbers in an intervals of C

In this program, we are going to learn how to write the code to display prime numbers between two intervals using different methods in C language.

This is done using for loop , while loop , do-while loop and function in C language

Display prime numbers between two intervals

Display prime numbers between two intervals using for loops

In this program, we will print prime numbers between two intervals  using a for loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,min,max;
    printf("Enter two number with intervals\n");
    scanf("%d %d",&min,&max);

    printf("Prime numbers %d to %d are\n",min,max);
    for(i=min; i<=max; i++){
        int count=0;
        for(j=1; j<=i; j++){
        if(i%j==0){
            count++;
        }
    }
    if(count==2){
        printf("%d ",i);
    }
    }
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter two number with intervals
20
80
Prime numbers 20 to 80 are:
23 29 31 37 41 43 47 53 59 61 67 71 73 79

 

Display prime numbers between two intervals using while loop

In this program, we will print prime numbers between two intervals  using a while loop in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,min,max;
    printf("Enter two number with intevals\n");
    scanf("%d %d",&min,&max);

    printf("Prime numbers %d to %d are\n",min,max);
    i=min;
    while(i<=max){
        int count=0;
        j=1;
        while(j<=i){
        if(i%j==0){
            count++;
        }
         j++;
    }
    if(count==2){
        printf("%d ",i);
    }
     i++;
    }
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter two number with intervals
25
75
Prime numbers 25 to 75 are:
29 31 37 41 43 47 53 59 61 67 71 73

Display prime numbers between two intervals using do-while loop

In this program, we will print prime numbers between two intervals  using a do-while loop in C language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,min,max;
    printf("Enter two number with intevals\n");
    scanf("%d %d",&min,&max);

    printf("Prime numbers %d to %d are\n",min,max);
    i=min;
   do{
        int count=0;
        j=1;
        do{
        if(i%j==0){
            count++;
        }
         j++;
    }while(j<=i);
    if(count==2){
        printf("%d ",i);
    }
     i++;
    } while(i<=max);
getch();
    return 0;
}


When the above code is executed, it produces the following result

Enter two number with intervals
30
50
Prime numbers 30 to 50 are:
31 37 41 43 47


Display prime numbers between two intervals using function

In this program, we will print prime numbers between two intervals  using a function in C language

Program 4

#include <stdio.h>
#include <stdlib.h>
int printPrime(int n);//function prototype
int main()
{
    int i,flag,min,max;
    printf("Enter two number with intervals\n");
    scanf("%d %d",&min,&max);

    printf("Prime numbers between %d to %d are\n",min,max);

   for(i=min+1; i<max; i++){
        flag=printPrime(i);//function call


        if(flag==1){

        printf("%d ",i);
    }

    }
getch();
    return 0;
}

int printPrime(int n){//function definition
int j,flag=1;
for(j=2; j<=n/2; j++){
    if(n%j==0){
        flag=0;
        break;
    }
}
return flag;
}


When the above code is executed, it produces the following result

Enter two number with intervals
50
100
Prime numbers 50 to 100 are:
53 59 61 67 71 73 79 83 89 97

 

Suggested post

for loop in C language

while loop in C language

do-while loop in C language

if statements in C language

function in C language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes