I am trying to write a program that shows the results of prime factorization as below:
prompt: Input a positive integer
result examples:
100 = 2^2*5^2
It is a composite integer !
13 = 13
It is a prime number !
I tried to write it only with the basic loops, not using sophisticated techniques because it was for beginner's class. The problem is that when I enter 100 as an input, the result is just
100 =
being printed and the program stops there. Other than that, it gives me results I intended. Can anyone help me find what part of this code is the problem?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int inputNumber (int* input);
int primeFactors (int input);
int main (void) {
int input;
while ( 1 ) {
inputNumber (&input);
primeFactors (input);
}
return 0;
}
int inputNumber (int* input){
printf("\nInput a positive integer : ");
scanf("%d", input);
if(*input == 0){
printf("\n End of program");
exit(0);
}
return;
}
int primeFactors (int input){
int cnt = 0, i = 3, cnt_sum;
printf("%d = ", input);
if(input > 0){
while ( input % 2 == 0){
cnt++;}
if(cnt == 1){
printf("%d",2);}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",2,cnt);
}
cnt_sum += cnt;
for ( ; i <= sqrt(input); i = i+2){
cnt = 0;
while(input % i == 0){
cnt++;
input /= i;
}
if(cnt == 1){
printf("%d",i);
}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",i,cnt);
}
cnt_sum += cnt;
}
if(cnt_sum > 1){
printf("\nIt is a composite number !\n");
}
else{
printf("%d\nIt is a prime number !\n",input);
}
}
else{
printf("\nIt is an invalid number !\n");
}
}
There were few bugs.
while ( input % 2 == 0){cnt++;} loops infinitely if input is even( #Marian )
if(input > 1){ instead of if(input > 0){. since 1 is neither prime nor composite.
Corrected function:
int primeFactors (int input){
int cnt = 0, i = 3, cnt_sum;
printf("%d = ", input);
if(input > 1){
while ( input % 2 == 0){
input/=2;
cnt++;
}
if(cnt == 1){
printf("%d",2);}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",2,cnt);
}
cnt_sum += cnt;
for ( ; i <= sqrt(input); i = i+2){
cnt = 0;
while(input % i == 0){
cnt++;
input /= i;
}
if(cnt == 1){
printf("%d",i);
}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",i,cnt);
}
cnt_sum += cnt;
}
if(cnt_sum > 1){
printf("\nIt is a composite number !\n");
}
else{
printf("%d\nIt is a prime number !\n",input);
}
}
else{
printf("\nIt is an invalid number !\n");
}
}
ideone
You seem to be missing some steps in your algorithm, as well as checking redundant conditions in your primeFactors function.
int cnt = 0, i = 3, cnt_sum;
Because these won't ever be negative, you can change the type to unsigned.
while ( input % 2 == 0){
cnt++;
}
This while statement will loop infinitely if input is odd, because nothing is done to change input. You can add input = input / 2; to stop this.
if(cnt == 1){
printf("%d",2);}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",2,cnt);
}
The else if expression can be reduced to an else statement, because unsigned variables are always greater than 0 and we already know cnt != 1, otherwise the first statement would be triggered.
while(input % i == 0){
cnt++;
input /= i;
}
The while statement is all good here, how peculiar. :P
if(cnt == 1){
printf("%d",i);
}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",i,cnt);
}
Same deal here as with the if / else if statements before: change the else if to else.
With these corrections applied (and some style inconsistencies fixed), the function now looks like this:
int primeFactors (int input) {
unsigned cnt = 0, i = 3, cnt_sum;
printf("%d = ", input);
if (input > 0) {
while (input % 2 == 0) {
cnt++;
input /= 2;
}
if (cnt == 1) {
printf("%d", 2);
} else {
printf("%d^%d", 2, cnt);
}
cnt_sum += cnt;
for ( ; i <= sqrt(input); i += 2){
cnt = 0;
while (input % i == 0) {
cnt++;
input /= i;
}
if (cnt == 1) {
printf("%d", i);
} else {
printf("%d^%d", i, cnt);
}
cnt_sum += cnt;
}
if (cnt_sum > 1) {
printf("%d\nIt is a composite number !\n");
} else {
printf("%d\nIt is a prime number !\n",input);
}
}
else{
printf("\nIt is an invalid number !\n");
}
}
Related
I have been doing on decomposing Integers to prime numbers, but i got stucked , it s working for one prime number, but when the iteration continues then it s not working properly and I can t come up with any solution.
For example for number 12 i expect output 2^2 x 3
but instead program stops at 2^2 x and new input is expected.
int main(int argc, char *argv[])
{
int number;
int stop = 1;
int prime = 0;
int sqr = 0;
while (stop == 1) {
/**------ERROR------ **/
if(scanf("%d",&number) != 1 || number < 0 ){
fprintf(stderr,"%s","Error: Chybny vstup!\n");
return 100;}
if(number == 0){
stop = 0;
break;}
/**------DECOMPOSITION------ **/
for (int index = 1;index <=number;){
if(number == 1){
//Ak sa input cislo rovna 1 tak vypise nasledovne riadky
printf("Prvociselny rozklad cisla 1 je:\n");
printf("1\n");
break;}
if(number % (index + 1) == 0){
// ak je cislo % index +1 (pociatocna hodnota je 2) tak sa vydeli cislom index + 1
// do prime sa ulozi dane prvocislo
// ak je delitelne prvocislom pripocitame sqr +1
// cyklus pokracuje s rovnakym indexom
number = number / (index + 1);
prime = index + 1;
sqr = sqr + 1;
continue;}
else{
if(sqr == 1){
// vypise len prvocislo
printf("%d",prime);}
if(sqr != 1){
//vypise prvocislo aj mocninu
printf("%d^%d", prime,sqr);}
if(number != 1){
printf(" x ");}
else{
printf("\n");
break;}
}
index = index + 1;
sqr = 0;
}
}
return 0;
}
I see two problems in the code:
The special case for number == 1 is being handled inside the loop. That special case is much easier to handle before the loop.
Counting the exponent is mixed into the loop that updates the factor. It's much easier to use a second loop to count the exponent.
#include <stdio.h>
int main(void)
{
while (1){
/**------GET USER INPUT------ **/
int number;
if(scanf("%d",&number) != 1 || number < 0 ){
fprintf(stderr,"%s","Error: Chybny vstup!\n");
return 100;
}
/**------HANDLE SPECIAL CASES------ **/
if(number == 0){
break;
}
if(number == 1){
printf("Prvociselny rozklad cisla 1 je:1\n");
continue;
}
/**------DECOMPOSITION------ **/
for (int factor = 2; factor <= number; factor++){
int exponent = 0;
while(number % factor == 0){
number /= factor;
exponent++;
}
if (exponent == 1){
printf("%d", factor);}
else if (exponent > 1){
printf("%d^%d", factor, exponent);}
if(exponent != 0 && number != 1){
printf(" x ");}
}
printf("\n");
}
}
I am having trouble refining some code. My code takes a number "n" and calculates that many prime numbers. I need to display 10 primes per line of output data. Any tips would be appreciated.
#include <stdio.h>
int main()
{
int n, i = 3, count, c;
printf("How many primes would you like?");
scanf("%d",&n);
if ( n >= 1 )
{
printf("2");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf(" %d",i);
count++;
}
i++;
}
return 0;
}
Just try
printf(" %5d", i);
/* ^ to help align the numbers
and
if ((count + 1) % 10 == 0)
fputc(stdout, '\n');
fix for the first time when you already print 2.
bool is_prime(int anyNum) //takes an integer array returns, is_prime
{
bool is_prime = true;
for (int c = 2; c <= anyNum - 1; c++)
{
if (anyNum % c == 0)
{
//printf("%d is not prime\r\n" , anyNum);
is_prime = false;
}
}
return is_prime;
}
int main()
{
int num_primes;
printf("How many primes would you like: ");
std::cin >> num_primes;
printf("\r\nScanned Primes Are---\r\n");
int foundPrimes = 0;
int x = 0;
for (; x <= num_primes; x++)
{
bool gotLuckyFindingPrime = is_prime( x );
if (gotLuckyFindingPrime)
{
if (foundPrimes % 10 == 0)
{
printf("\r\n");
}
printf(" %d", x);
foundPrimes = (foundPrimes + 1) % 10;
}
}
}
Does handle ten digit showing on cmd too, you can experiment with formatting
How do you break out of a loop without a break statement? My professor HATES break statements and tells us not to use it. I'm just curious how would I break out of the while-loop if the number I got WAS NOT a prime number?
Here's my code:
#include <stdio.h>
#include <stdlib.h>
/* Prototypes */
void primeChecker(int num1);
int main() {
int num1 = 5;
primeChecker(num1);
return 0;
}
void primeChecker(int num1) {
int i, flag = 0;
printf("Enter a number to check for prime numbers: ");
scanf("%d", &num1);
/* Number to start with */
i = 2;
while (i <= num1/2) {
if (num1 % i == 0) {
flag = 1;
} else {
i++;
}
}
if (flag == 0) {
printf("The number is a prime number!");
} else {
printf("The number is NOT a prime number!");
}
}
Or
int prime = 1;
while (i <= num1/2 && prime) {
if (num1 % i == 0){
prime = 0;
} else {
i++;
}
}
if(prime){
printf("The number is a prime number!");
}else{
printf("The number is not prime.");
}
I mean, you almost had it:
while (i <= num1/2 && !flag){
would have done the trick as well
You can do
while (i <= num1/2) {
if (num1 % i == 0) {
i = num1;
} else {
i++;
}
}
This makes i larger then num1/2 and the while loop exits.
You probably need some more changes to make this work.
In your case, you can use the value of flag as condition:
while (flag == 0 && i <= num1/2) {
if (num1 % i == 0) {
flag = 1;
} else {
i++;
}
}
But that looks like Pascal rather than C. A better solution might be to refactor the loop so that it is in a separate function:
int is_prime(int num1)
{
int i = 2;
while (i <= num1/2) {
if (num1 % i == 0) return 0;
i++;
}
return 1;
}
This makes the code simpler and separates the input stuff in primeChecker from the actual prime checking.
Hello i want to avoid code duplication in my program (buzzfizz including negative numbers)
#include <stdio.h>
int myseries(int n) {
int i, cpt = 0;
if (n < 0) {
for (i = 0; i >= n; i--) {
// if the number is multiple of both three and five
if (i % 15 == 0) {
printf("lancelot\n");
}
// if the number is multiple of 3
else if(i % 3 == 0) {
printf("Fizz\n");
}
// if the number is multiple of 5
else if(i % 5 == 0) {
printf("Buzz\n");
cpt++;
}
else {
printf("%d\n", i);
}
}
return cpt;
}
else {
for (i = 0; i <= n; i++) {
// if the number is multiple of both three and five
if (i % 15 == 0) {
printf("lancelot\n");
}
// if the number is multiple of 3
else if(i % 3 == 0) {
printf("Fizz\n");
}
//if the number is multiple of 5
else if(i % 5 == 0) {
printf("Buzz\n");
cpt++;
}
else {
printf("%d\n",i);
}
}
return cpt;
}
}
//example
main() {
printf("the number of buzz is : %d", myseries(-16));
}
You can use the absolute value of n (i.e. abs(n)) as i's upper bound, and record n's sign (i.e. bool sgn = ( n > 0 ) ? 1 : 0;) for output.
I have created this C program that is supposed to tell me how many prime numbers are between 1-25, but it is printing that every number is prime number. Please Help
#include<stdio.h>
int main(void) {
int n = 1, counter = 0;
int i, flag = 0;
while ( n <= 25 )
{
for ( i = 2; i <= (n/2); i++ )
{
if (n%i == 0)
{
flag = 0;
}
}
if (flag == 0)
{
counter++;
printf("%d is Prime Number.\n", n);
}
else
{
printf("%d is not Prime Number.\n", n);
}
n++;
}
return 0;
}
flag = 0;
Your flag is always 0. You have to set it to 1, while entering the while loop
while ( n <= 25 ) {
flag = 1; //<-- here
for ( i = 2; i <= (n/2); i++ ) {
You can as well break in the segment, just for efficiency
if (n%i == 0)
{
flag = 0;
break;
}
you can use the code as,
for ( i = 2; i <= sqrt(n); i++ )
{
if (n%i == 0)
{
flag = 0;
}
}
if (flag == 0)
{
counter++;
printf("%d is Prime Number.\n", n);
flag=1;
}
else
{
printf("%d is not Prime Number.\n", n);
}
n++;
use sqrt() instead of n/2 program will be more efficient.