So I started learning C language for Uni and got stuck with this exercise, I found a way to ge the prime numbers of a number but I don't know how to multiply the prime numbers and display them.
int main()
{
int number;
int prime;
int i,j;
printf("Insert number:");
scanf("%d", &number);
printf("Prime numbers of %d are: ",number);
for(i = 2; i <= number; i )
{
prime = 1;
for(j = 2; j <= i/2; j )
{
if(i % j == 0)
{
prime = 0;
}
}
if(prime == 1)
{
printf(" %d", i);
}
}
return 0;
}
CodePudding user response:
You need to introduce one more variable that will store the nultiplication of prime numbers.
Here is a demonstration program.
#include <stdio.h>
int main( void )
{
unsigned int number;
printf( "Insert number (0 - exit ): " );
if ( scanf( "%u", &number ) == 1 && number != 0 )
{
if ( number < 2 )
{
printf( "There are no prime numbers in the range [0, %u].\n", number );
}
else
{
unsigned long long product = 2;
printf( "Prime numbers in the range [0, %u] are: ", number );
printf( "%u ", 2 );
for ( unsigned int i = 3; i <= number; i = 2 )
{
int prime = 1;
for( unsigned int j = 3; prime && j <= i / j; j = 2 )
{
if ( i % j == 0 )
{
prime = 0;
}
}
if ( prime )
{
printf( "%d ", i );
product *= i;
}
}
printf( "\nTheir multiplication is equal to %llu\n", product );
}
}
}
Its output might look like
Insert number (0 - exit ): 10
Prime numbers in the range [0, 10] are: 2 3 5 7
Their multiplication is equal to 210