# C Programming – HackerRank Solution | Finding Primes |

### (HackerRank) Write a Modular C Programming code to solve Finding Primes, Write a user-defined function checkprime() to check the roll number of a student in a classroom is a prime.

Karan was late to school by 15 minutes. The teacher punished him to perform the following task:

The teacher told him to go to each classroom of different divisions, call out all the students whose roll number is prime and collect Rs. 10/- from each.

Karan has to collect money and give away a treat to his classmates and teacher.

Find out how much maximum money he collects.

Write a user-defined function checkprime() to check the roll number of a student in a classroom is a prime.

Input Format

N – a number.

Constraints

N should be greater than 1 and less than 70.

Output Format

Students with prime roll numbers.

Money collected.

Sample Input 0

```5
```

Sample Output 0

```Students with prime roll numbers = 2 3 5
Money collected = 30
```

Sample Input 1

```1
```

Sample Output 1

```Invalid input
```

Sample Input 2

```70
```

Sample Output 2

```Students with prime roll numbers = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
Money collected = 190
```

Sample Input 3

```-5
```

Sample Output 3

```Invalid input
```

Sample Input 4

```25
```

Sample Output 4

```Students with prime roll numbers = 2 3 5 7 11 13 17 19 23
Money collected = 90```

CODE:

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

void prime(int n)
{
int count=0,i,j,status=0;
printf("Students with prime roll numbers =");
for(i=2;i<=n;i++)
{
status=1;
for(j=2;j<i;j++)
{
if(i%j==0)
{
status = 0;
break;
}
}
if(status==1)
{
printf(" %d",i);
count++;
}
}
printf("\nMoney collected = %d",count*10);
}
int main() {

int n;
scanf("%d",&n);
if(n<2||n>70)
{
printf("Invalid input");
exit(0);
}
prime(n);
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)

5

Students with prime roll numbers = 2 3 5
Money collected = 30
Expected Output

Students with prime roll numbers = 2 3 5
Money collected = 30```

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

Top 100+ C Programming codes – KLE Technological University 