Monday, November 15, 2010

c program to check number is prime or not

Program Code


// Check whether given number is prime number and not prime number
#include "stdio.h"
#include "conio.h"
void main()
{
int num,i,z;
printf("enter the value of num");
scanf("%d",&num);
i= 2;
while (i less then num)
{
if(num%i==0)
{
z=1;
}
i=i+1;
}
printf("After checking the result is");
if(z== 1)
{
printf("given number is not prime number");
}
else
{
printf("given number is prime number");
}
getch();
}

Input/Output


enter the value of num 7
After checking the result is
given number is prime number


What is Prime Number?


Prime number is a natural number which is divisible by 1 and itself.
Example 7

First 25 Prime Number :-
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

1 comment:

geethesh said...

package com.concise;
import java.util.Scanner;
class TestDemo1
{
int Prime(int n)
{
int i=0;
int j=2;
while(j<=n)
{
if(n%j==0 && j<n)
{
return (0);
}
else if(j==n)
{
return(1);
}
j++;
}
return i;
}
}


public class FabinocciDemo
{
public static void main(String[] args)
{
TestDemo1 ts=new TestDemo1();
System.out.println("Enter the number of terms in Fabinocci series:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=1,b=1,c;

for(int i=0;i<n;i++)
{
if(i==0 || i==1)
{

}
else
{
c=a+b;
int r=ts.Prime(c);
if(r==1)
{
System.out.println(c+"=true");
}

else
{
System.out.println(c+"=false");
}
System.out.println("\t");
a=b;
b=c;
}
}
}
}

this s prime or not............ another question is
int r=ts.Prime(c);
if(r==1)

This checks whether r is prime not if n is prime. Try again

Post a Comment