C program to find the prime numbers between intervals - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, November 19, 2019

C program to find the prime numbers between intervals - My CS Tutorial

In this tutorial we will learn how to write a program that find the prime number between two number.

Solution:



  • We will use five variables in this program as num1,num2,count,i and j.                                                                    
  • We will use int data type.                        
  • Enter the value of num1 and num2.      
  • Use for loop in this program.                  
  • After it use if conditions.                          
  • Print the all numbers between these entered two numbers.



C program code


#include<stdio.h>
#include<conio.h>
void main()
{
  int num1,num2,i,count=0,j;
  printf("\n Enter the first number: \n ");
  scanf("%d",&num1);
  printf("\n Enter the second number \n ");
  scanf("%d",&num2);
  printf("\n The Prime numbers between %d and %d are\n ",num1,num2);
  for(i=num1;i<=num2;i++)
  {
    count=0;
    for(j=1;j<=i;j++)
    {
      if(i%j==0)
      {
        count++;
      }
    }
    if(count==2)
    {
      printf("%d\n ",i);
    }
  }
  getch();
}



OUTPUT 1:





OUTPUT 2:







In first output,the value of first number is 15
And the value of second number is 40
After calculation
The prime number between these two numbers are

17, 19, 23, 29, 31, 37



In second output,the value of first number is 50
And the value of second number is 75
After calculation
The prime number between these two numbers are

53, 59, 61, 67, 71, 73



Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.


Created by-- HARSH CHAUHAN

No comments:

Post a Comment