Prime number program in c using if else

Prime number program in c using if else

 Prime number program in c using if else:
#include <stdio.h>
int main()
{
    int i, j, flag = 0;
    printf("Enter the Number: ");
    scanf("%d", &i);
    for(j = 2; j <= i/2; ++j)
    {
        if(i%j == 0)
        {
            flag = 1;
            break;
        }
    }
    if (i == 1)
    {
      printf("1 is neither a prime nor a composite number.");
    }
    else
    {
        if (flag == 0)
          printf("%d is a prime number.", i);
        else
          printf("%d is not a prime number.", i);
    }
    return 0;

}
Output:
Enter a number: 4

4 is a not Prime Number.
See also: Prime number program in c
Share:

Prime number program in c using do-while loop

Prime number program in c using do-while loop

 Prime number program in c using do-while loop:
#include<stdio.h>
int main()
{
    int i,j,k;
    printf("Enter a number: ");
    scanf("%d",&i);
    k=0;
    j=2;
    do
    {
        if(i%j == 0)
        {
            k=1;
            break;
        }
        j++;
    }
    while(j <= i/2);
    if(k==0)
        printf("The entered number is Prime Number");
    else
        printf("Entered number is Not Prime Number");
    return 0;

}
Output:
Enter a number: 7

The entered number is Prime Number.
See also: Factorial Program in Java
Share:

Factorial Program in Java with examples.

Factorial Program in Java and Examples


Factorial:  Factorials are products of every whole number from 1 to n Factorials are introduced in algebra. The value of 0! is 1, according to the convention for an empty product. The notation n! was introduced by the French mathematician Christian Kramp in 1808.

Formula:

n! = n(n-1)(n-2)(n-3)(2)(1)

 (or)

n! = n.(n-1)!.

Example:

4! =  4*(4-1)*(4-2)*(4-3)*(4-4)
4! =  4*3*2*1
4! = 24

Factorial Program in Java:

Factorial Program:
class Fact
{
 public static void main(String args[])
 {
  int fact = 1,j = 6;
  for(int i = 1;i<=j;i++)
  {
   fact = fact * i ;
  }
 System.out.println("Fact of 6 = "+fact);
 }
}
Output:
Fact of 6 = 720

Factorial program in Java using user entered value:

Java Factorial program:
import java.util.Scanner;
class Fact1
{               
 public static void main(String args[])
 {
  Scanner s = new Scanner(System.in); 
  int fact = 1;
  System.out.print("Enter the value of j = ");
  int j = s.nextInt();
  for(int i = 1;i<=j;i++)
  {
   fact = fact * i ;
  }
 System.out.println("Fact of " + j + " = " +fact);
 }
}
Output:
Enter the value of j = 10
Fact of 10 = 3628800

Factorial program in Java using recursion:

Java Factorial recursion program:
class FactorialRecursion
  int factorial(int n)
 {   
  if (n == 0)   
    return 1;   
  else   
    return(n * factorial(n-1));   
 }             
 public static void main(String args[])
 { 
  int i,fact = 1; 
  FactorialRecursion f1 = new FactorialRecursion();
  fact = f1.factorial(5);  
  System.out.println("Factorial of 5 = "+fact);   
 } 
Output:
Factorial of 5 = 120

Factorial program in Java:

Recursion program using user entered value:

Java Recursion Factorial program using user entered value:
import java.util.Scanner;
class FactorialRecursion1
  int factorial(int n)
 {   
  if (n == 0)   
    return 1;   
  else   
    return(n * factorial(n-1));   
 }   
 public static void main(String args[])
 { 
  int i,fact = 1; 
  Scanner s = new Scanner(System.in);
  FactorialRecursion1 f1 = new FactorialRecursion1();
  System.out.print("Enter the value of J = ");
  int j = s.nextInt();
  fact = f1.factorial(j);  
  System.out.println("Factorial of J = "+fact);   
 } 
Output:
Enter the value of J = 6
Factorial of J = 720
See also: Prime Number Program in C using For loop
Share:

Prime Number Program in C using For loop

Prime Number Program in C using For loop

Prime number program to print from 1 to 100.

C program to print numbers from 1 to 100:
#include<stdio.h>
int main()
{
  int limit = 100;
    printf("Prime numbers between 1 and 100 are: ");
    for(int i = 2; i < limit; i++)
    { 
     int flag = 0;
     for(int j=2;j < i;j++)
      {
             if(i%j==0)
              {
                 flag = 1;
                 break;
             }
        }
         if(flag ==0)
             printf("%d ",i);
    }
   return 0;
}
Output:
Prime numbers between 1 and 100 are: 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.

Explanation of the above program: 

  1. If the For loop condition (i <= limit) is true then loop enters into for loop and check the if condition (i%j == 0). 
  2. If the condition is true the Entered number is not the prime number.
  3.  If the condition is False, then the Prime number is printed up to the given limit. When for loop condition (101 <=100) then the program terminates.

Prime Number Program in C using For loop

Prime number program to print from 1 to N.

C program to print numbers from 1 to N:
#include<stdio.h>
int main(){
  int i,j,count,k;
    printf("Enter max range: ");
    scanf("%d",&k);
    for(i = 1;i<=k;i++)
     {
         count = 0;
         for(j=2;j<=i/2;j++){
             if(i%j==0){
                 count++;
                 break;
             }
        }
         if(count==0 && i!= 1)
             printf("%d ",i);
    }
   return 0;

}
Output:
Enter max range: 70

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67.

Explanation of the above program: 

  1. First, the user should give the range value from the keyboard. the value is stored in the variable "k". 
  2. If the For loop condition (i <= limit) is true then loop enters into for loop and check the if condition (i%j == 0). 
  3. If the condition is true the Entered number is not the prime number.
  4.  If the condition is False, then the Prime number is printed up to the given limit. When for loop condition (101 <=100) then the program terminates.

Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...