In: Computer Science
C Programming Language
Problem Title : 4th Grade
Jojo just graduated and moved up to grade 4. Today is his first day in 4th grade. Unfortunately, the lessons are held online because of pandemic. So that the quality of learning remains good, Jojo’s teacher gives a hard task for 4th grader.
The first task is to find the prime factorization of a number. Prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. Prime factorization of a number is breaking a number down into the set of prime numbers which multiply together to result in the original number. Example below is the prime factorization of 1176.
4th Grade
Jojo just graduated and moved up to grade 4. Today is his first day
in 4th grade.
Unfortunately, the lessons are held online because of pandemic. So
that the quality of
learning remains good, Jojo’s teacher gives a hard task for 4th
grader.
The first task is to find the prime factorization of a number.
Prime number is a natural
number greater than 1 that is not a product of two smaller natural
numbers. Prime
factorization of a number is breaking a number down into the set of
prime numbers
which multiply together to result in the original number. Example
below is the prime
factorization of 1176.
As a good friend of Jojo, help Jojo to solve the prime
factorization task given by his
teacher.
Format Input
There are
T testcases. Each testcase contains an integer
N which indicates the number
to be factorized into prime factorization.
Format Output
Output
T line with format
“Case#X: ”, where
X indicates the testcase number and
then followed by the number prime factorization in ascending order
of prime factors with
the correct format.
Code:
#include <stdio.h>
void primeFactorization(int n, int p)
{
printf("Case #%d: ",p);
int c = 2;
while (c<n)
{
if (n%c == 0)
{
int e = 0;
while (n % c == 0)
{
n = n/c;
e++;
}
printf( "%d^%d",c,e);
if (n != 1)
printf( " * " );
}
c++;
}
if (n != 1)
printf( "%d^1\n",c);
else
printf("\n");
}
int main()
{
int T, N, i;
scanf("%d",&T);
for(i=0;i<T;i++)
{
scanf("%d",&N);
primeFactorization(N,(i+1));
}
}
Please refer to the screenshot of the code to understand the indentation of the code:
Output:
For any doubts or questions comment below.