- Any number which is divisible by 1 and itself is known as prime number.
- To check prime number or not in c programming we need to use for loop and iterate from 2 to half of the number.
- If any number is divisible then it is non prime number, we can exit the loop.
- Let us see an example program on c to check a number is prime number or not
- prime number program in c using for loop
Program #1: Write a c program to check a number is prime number or not using for loop.
- #include <stdio.h>
- int main()
- {
- int n, i, count = 0;
- printf("Enter number to check prime number or not");
- scanf("%d",&n);
- for(i=2; i<=n/2; ++i)
- {
- // check for non prime number
- if(n%i==0)
- {
- count=1;
- break;
- }
- }
- if (count==0)
- printf("%d is a prime number.",n);
- else
- printf("%d is not a prime number.",n);
- getch();
- }
Output:
Program #2: Write a c program to check a number is prime number or not using while loop.
- #include <stdio.h>
- int main()
- {
- int n, i, count = 0;
- printf("Enter number to check prime number or not");
- scanf("%d",&n);
- i=2;
- while( i<=n/2)
- {
- // check for non prime number
- if(n%i==0)
- {
- count=1;
- break;
- }
- i++;
- }
- if (count==0)
- printf("%d is a prime number.",n);
- else
- printf("%d is not a prime number.",n);
- getch();
- }
Output:
- Enter number to check prime number or not
- 12
- 12 is not a prime number
- Write a c program to generate fibonacci series without recursion
- Write a c program to generate fibonacci series using recursion
- Matrix multiplication in c program with explanation
- Factorial program in c without recursion
- C program to check number is armstrong number or not
- Check Armstrong number in c using recursive function
- C program for even or odd using for loop
- C program to check odd or even without using modulus operator and division operator
- Prime number program in c using for loop and while loop
- C program to print prime numbers from 1 to n
- C program to print multiplication table using while loop and for loop
- C program to convert binary to decimal using for loop
- C program to reverse a number using while loop and for loop
- C program to find sum of n numbers using for loop
- C program to find leap year using if else
- c program to check leap year using conditional operator
- C program to print patterns of numbers,alphabets and stars in a pyramid shape
- C program to swap two numbers without using third variable and using functions
- C Program to swap two numbers without using third variable
- Write a C program to swap two integer arrays
- C program for swapping of two strings
- C program to print given number in words
- C program to print 1 to 100 without using loop
- C program to find ASCII value of a string
- Convert string to integer c programming
- C program to display characters from a to z using loop
- C programming power function example program
- C program for addition subtraction multiplication and division using switch case
- C program to delete an element in an array
- C program to insert an element in an array
- Switch case in c example program
- Prime number program in c using for loop
- Fibonacci series in c without recursion
how to use in c++ this loop
ReplyDelete,CASE WHEN DATEDIFF(MONTH,dateofjoin,GETDATE()) = 12 THEN '1' ELSE CAST(DATEDIFF(HOUR,DateofJoin,GETDATE())/8766 AS VARCHAR) END AS ExpYear
ReplyDelete,CAST(DATEDIFF(MONTH,dateofjoin,GETDATE())%12 AS VARCHAR) AS ExpMonth