C Programming – HackerRank Solution | primes in each row |

(HackerRank) Write a Modular C Programming code to solveprimes in each row, Count prime numbers in each row of a matrix

Count prime numbers in each row of a matrix

Input Format

First Line contains R and C separted by a space which says number of rows and number of columns.

Second line contains R * C number of elements separated by a space

Constraints

1<=R, C< = 100

Output Format

Output count of primes of each row in a new line.

Sample Input 0

0 2

Sample Output 0

Invalid

Sample Input 1

2 3
1 2 3 4 5 6

Sample Output 1

2
1

Explanation 1

There are two primes in 0th row ( 2 and 3) 
There is one prime in 1st row (5)

Note: Prime numbers are >=2

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int check_prime(int n);
int main() {
    int r,c;
    scanf("%d%d",&r,&c);
    if(r>0 && c>0)
    {
        int num[r][c],i,j;
        for(i=0;i<r;i++)
        {
            int prime=0;
            for(j=0;j<c;j++)
            {
                scanf("%d",&num[i][j]);
               if(check_prime(num[i][j]))
                   prime++;
            }
            printf("%d\n",prime);
        }
    }
    else{
        printf("Invalid");
    }
    return 0;
}
int check_prime(int n)
{
    int i=0,count=0;
    for(i=1;i<=n;i++)
    {
        if(n%i==0)
        {
            count++;
        }
    }
    if(count==2)
        return 1;
    else
        return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

0 2
Your Output (stdout)

Invalid
Expected Output

Invalid

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!