1) Write a C program to print the prime numbers from 1 to 100. (A prime integer is any integer that can be divided evenly only by itself and 1)
Requirement: Use an array to take the number from 1 to 100 and another array to take the prime number.
Please follow the code and comments for description :
CODE :
/* C program to find all prime numbers from the inputted array
*/
#include<stdio.h>
int main() // driver method
{
int ar[100], i, n, j, counter, res[100], m = 0; //
local variables
for(i = 0; i < 100; i++) // iterate and save the
data
{
ar[i] = (i + 1);
}
printf("The Array is - \n"); // message
for(i = 0; i < 100; i++)
{
printf("\t %d", ar[i]); //
message
}
printf("\n");
printf("\nAll the Prime Numbers in the Array are
-\n"); // message
for(i = 0; i < 100; i++) // iterate to calcualte
the primes
{
counter = 0;
for(j = 2; j < ar[i]; j++)
{
if(ar[i] % j ==
0) // prime number condition
{
counter = 1;
break;
}
}
if(counter == 0) // if true
{
res[m] =
ar[i]; // assign to another array
printf("\t %d",
res[m]); // message
m++; //
increment the count
}
}
printf("\n"); // new line character
return 0;
}
OUTPUT :
Hope this is helpful.
Get Answers For Free
Most questions answered within 1 hours.