Write a C, C++ program to check whether given number is prime or not. In this program user input one number at a time and your program prints whether the input number is prime or not.

A prime number is a number which is greater than 1 , and

For example - 7 is a prime number it is divisible by 1 and 7 only.

We run a loop from

Program to Print all prime numbers between 1 to 100.

Print even numbers from 1 to 100.

Sorting algorithms and their time complexity.

**What is Prime Number**A prime number is a number which is greater than 1 , and

**divisible by 1 and itself**.For example - 7 is a prime number it is divisible by 1 and 7 only.

**NOTE**- 2 is the only even prime number.__Logic for checking whether number is prime or not__We run a loop from

**2 to num/2**and check whether the number is divisible. If the number is not divisible by any number then it's a prime number.Program to Print all prime numbers between 1 to 100.

Print even numbers from 1 to 100.

Sorting algorithms and their time complexity.

## Program to Check whether Given Number is Prime or Not

#include<iostream.h> using namespace std; int main(){ int num,i,val=0; cout<<"Enter a number\n"; cin>>num; /* Check whether number is divisible by number other than 1 and itself */ for(i = 2; i <= num/2; i++){ if(num % i == 0){ val++; break; } } if(val==0) cout<<"Number is a prime number"; else cout<<"Number is not a prime number"; return 0; }

## C Program to Check whether Given Number is Prime or Not

#include <stdio.h> int main(void) { int num,i,val=0; printf ("Enter a number\n"); scanf ("%d" , &num); /* Check whether number is divisible by number other than 1 and itself */ for(i = 2; i <= num/2; i++){ if(num % i == 0){ val++; break; } } if(val==0) printf (" The number you have input is a prime number"); else printf (" The number you have input is not a prime number"); return 0; }

**Output :**

**Enter a number : 5**

The number you have input is a prime number

Enter a number : 6

The number you have input is not a prime number

## No comments:

## Post a Comment