Write a C program to count all the composite numbers in an array

Write a C program to count all the composite numbers in an array, In this C program example, we will see how to count composite numbers in an array.

What is a composite number?

Those number which has divided by more than two numbers, or the number give more two factorial, or not a prime number is known as a composite number. for example 4= 4/1=0,  4/2=0, 4/4=0, 4 is divided by 3 numbers that is 1,2,and 4.

Sample input

enter the size of an array: 4

enter elements:

2

4

16

18

Sample output

total composite number is: 3

Algorithm to count all the composite numbers in an array

  • Declare one array and three variables.
  • Take two inputs first for the size of an array and another for taking elements in the array.
  • Take two for loop, one is for input and another for loop is for to give logic and condition.
  • Take if, else if and if statement.
  • Print with an appropriate message.

C program to count  all the composite numbers in an array

#include<stdio.h>
void main (){
    int i,n,a[100],count=0;
    printf("enter size:");
    scanf("%d",&n);
    printf("enter elements\n");
    for(i=0;i<n;i++){
    	scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++){
       if(a[i]==2){
     	 continue;
       }       
        else if(a[i]%2==0){
         count++;
        }
    }
        if(count>2){    
        }
       printf("total composite number are: %d",count);
 }

Explanation of this C program

Step 1: Start.

Step 2: Create a header file and include the library on it.

Step 3: Create void main function.

Step 4: Declare one array and three variables.

Step 5: Create two inputs one is for the size of an array and another for taking elements in an array to create an input with help of scanf.

Step 6: Create two for loop first one for input of element and the second one for logic and conditon to count composite number.

Step 7: In for loop create if,  and else if statement for logic :

– condition of for loop = (i=0;i<n;i++),

– condition of if statement = (a[i]==2) (to continue without two because two is aa prime number),

– condition of else if statement = (a[i]%2==0) (and increment of count++ to cheak next number after incriment).

Step 8: After for loop create a if statement, and the condition is “count>2” to print the number which is divide by moe than two number.

Step 9: Print a appropiate message with the help of printf and count.

Step 10: End.

Output of this C program

In this way, we learned how to write a C program to count all the composite numbers in an array