Code duplication in buzzfizz program including negative numbers - c

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.

Related

Checking whether space should be printed in the middle or at the end in for loop?

I am trying to give info to my program whether it should print space or not.
My code looks something like this, and its printing spaces at the end (which is not what I want)
void function(int given_limit)
{
int current_num = 2;
while (given_limit > 1)
{
if (given_limit % current_num == 0)
{
if (current_num == 2)
{
printf("%d ", current_num);
}
else if (current_num == 3)
{
printf("%d ", current_num);
}
else if (current_num == 5)
{
printf("%d ", current_num);
}
else if (current_num == 7)
{
printf("%d ", current_num);
}
else
{
printf("%d ", current_num);
}
given_limit /= current_num;
}
else
current_num++;
}
printf("\n");
}
In main() I am calling it something like this:
int main()
{
int given_limit = 13;
for (int i = 0; i <= given_limit; i++)
{
printf("%d\t\t", i);
function(i);
}
}
I would appreciate any tips and help.
One of the ideas is maybe to store it in an array.
I replaced spaces with asterisks for better visibility and removed the redundant if-elements. Then I introduced a flag which indicates whether it is the output of the first factor or a later one. In front of each later one we put the space (or asterisk).
#include <stdio.h>
#include <stdbool.h>
void function(int given_limit)
{
bool is_first_factor = true;
int current_num = 2;
while (given_limit > 1)
{
if (given_limit % current_num == 0)
{
if (is_first_factor) {
is_first_factor = false; // not first anymore
// print nothing
} else {
printf("*"); // between two factors
}
printf("%d", current_num);
given_limit /= current_num;
}
else
current_num++;
}
printf("\n");
}
int main(int argc, char **argv)
{
int given_limit = 13;
for (int i = 0; i <= given_limit; i++)
{
printf("%d\t\t", i);
function(i);
}
}
$ gcc spacing.c
$ ./a.out
0
1
2 2
3 3
4 2*2
5 5
6 2*3
7 7
8 2*2*2
9 3*3
10 2*5
11 11
12 2*2*3
13 13
$
As mentioned above move the space character to in front of each prime factor and then align the output to take the initial starting space character into account.
This example also skips unnecessary factors.
/* primefactors.c
*/
#include <stdio.h>
void primeFactors(int number)
{
printf(" %2d ", number);
// only test factors <= sqrt(number)
// skip even factors > 2
int factor = 2;
while (factor <= number / factor) {
if (number % factor == 0) {
printf(" %d", factor);
number /= factor;
}
else if (factor == 2){
factor = 3;
}
else {
factor += 2;
}
}
// at this point number equals the greatest prime factor
printf(" %d\n", number);
}
int main (void)
{
int max = 45;
printf("\nnumber prime factors\n");
printf("------ -------------\n");
// skip 0 and 1 which have no prime factors
printf(" %2d\n", 0);
printf(" %2d\n", 1);
for (int i = 2; i <= max; ++i) {
primeFactors(i);
}
return 0;
}

writing a prime factorization program C

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");
}
}

Need 10 outputs per line

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

Different output between compilers

I am doing the first problem on Project Euler.
I have the following code:
#include <stdio.h>
int main() {
int number;
int sum;
while (number < 1000) {
if (number % 3 == 0 || number % 5 == 0) {
sum += number;
number++;
}
else {
number++;
}
}
printf("The answer is %d", sum);
return 0;
}
When I compile this via compileonline.com, I get 233168. When I compile this in gcc I get 2686824. What causes this difference?
Compileonline probably initializes the variables.
You have to initialize them manually.
#include <stdio.h>
int main() {
int number = 0;
int sum = 0;
while (number < 1000) {
if (number % 3 == 0 || number % 5 == 0) {
sum += number;
number++;
}
else {
number++;
}
}
printf("The answer is %d", sum);
return 0;
}

How to print prime numbers without using modulus (%)?

How to print prime numbers without using modulus(%)? I tried making an array of the prime numbers and checking whether the remainder was equal to 1. But later I realized that that was not possible:
int main()
{
for (int i = 2; i < num; i++)
{
if (num % i == 0) // without using % I am suppposed find prime no
c++;
}
if (c == 0)
printf("prime");
else
printf("not prime");
}
int main()
{
for(int i=2;i<num;i++)
{
if((num-(num/i)*i)==0) // without using %
c++;
}
if(c==0)
printf("prime");
else
printf("not prime");
}
This should work.
a - (n * (a/n)) is equivalent to a % n
// Java Program
class PrimeNumber {
public static void main(String[] args) {
int num = 30;
for (int j = 2; j < n; j++) {
if ((num - (num / j) * j) == 0) {
count++;
}
}
if (count == 0) {
System.out.println(num + "is Prime");
}
}
}

Resources