Computer Science, asked by S8da2weshmimsk, 1 year ago

Program to Check Whether a Number is Prime or Composite
Write a program that gets a number, checks whether it is a prime or composite and outputs the result?

Answers

Answered by yasas
4
#include<stdio.h>
void main()
{
   int a,i;
   clrscr();
   printf("given number:");
   scanf("%d",&a);
     for(i=2;i<=a;i++)
       {
           if(a%i==0)
            {
                printf("%d is composite number",a);
                break;
            }
          }
           if(a==i)
            printf("%d is prime number",a);
       getch();
}
output of the above program is 
given number:17
17 is prime number
Answered by AskewTronics
2

Python Program:

Explanation:

number=int(input("Enter the number to check prime or composite: "))#Take the number from the user.

count=0

for i in range(2,int(number/2)+1):#Loop which is used to dive the number from other number.

   if(number%i==0):

       count=1

       break

if(count==0):#For prime number.

   print("The number is prime number")

else:#For composite number.

   print("The number is composite number")

Output :

  • If the user input as "4", it will print the composite number.
  • If the user input as "5", it will print prime number.

Code Explanation :

  • The above code is in python language, which has one for loop which runs from 2 to number/2 because any number is divisible by only less than half of its.
  • Then if the number is divisible count will be 1 else count will be 0 which will be check after the for-loop by the if-else statement.

Learn More ;

  • Python : https://brainly.in/question/14689905
Similar questions