-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfprime.c
More file actions
72 lines (60 loc) · 1.27 KB
/
fprime.c
File metadata and controls
72 lines (60 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Assignment name : fprime
// Expected files : fprime.c
// Allowed functions: printf, atoi
// --------------------------------------------------------------------------------
// Write a program that takes a positive int and displays its prime factors on the
// standard output, followed by a newline.
// Factors must be displayed in ascending order and separated by '*', so that
// the expression in the output gives the right result.
// If the number of parameters is not 1, simply display a newline.
// The input, when there is one, will be valid.
// Examples:
// $> ./fprime 225225 | cat -e
// 3*3*5*5*7*11*13$
// $> ./fprime 8333325 | cat -e
// 3*3*5*5*7*11*13*37$
// $> ./fprime 9539 | cat -e
// 9539$
// $> ./fprime 804577 | cat -e
// 804577$
// $> ./fprime 42 | cat -e
// 2*3*7$
// $> ./fprime 1 | cat -e
// 1$
// $> ./fprime | cat -e
// $
// $> ./fprime 42 21 | cat -e
// $
#include <stdio.h>
#include <stdlib.h>
void fprime(int n)
{
int div = 1;
if (n == 0)
return ;
if (n == 1)
printf("1");
while (n > div)
{
div++;
if (n % div == 0)
{
printf ("%d", div);
if (div == n)
break ;
printf ("*");
n = n / div;
div = 1;
}
}
}
int main(int ac, char **av)
{
int n = atoi(av[1]);
if (ac == 2)
{
fprime(n);
}
printf("\n");
return (0);
}