Wednesday 20 March 2019

CP: Display Prime numbers

Que:
Write a program that accepts a number from the user and prints prime numbers from 0 to that
number.


Ans: 

The prime number is a positive integer greater than 1 that is only divisible by 1 and itself. For example 2, 3, 5, 7, 11 are the first five prime numbers.

Code:

#include<stdio.h>
void main()
{
    int n,i,test,j;
   
    printf("Enter the Number=");
    scanf("%d",&n);
   
    printf("Prime Numbers are: \n");
   
    for(i=1; i<=n; i++)
    {
        test=0;
        for(j=1; j<=n; j++)
        {
            if(i%j==0)
                test++;
        }
        if(test==2)
            printf("%d " ,i);
    }
    getch();

}

No comments:

Post a Comment

LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project

  LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project Dear All We will learn how to Connec...