Count doesn't print it correctly - c

I have following program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, br, j=1, zbroj=0;
printf("Ucitati cijeli broj(manji od 1000):\n");
scanf("%d", &n);
for(br=1; br<=n; br++)
{
if(br % 6 == 0)
printf("%d ", br);
j = br;
while(j != 0)
{
if(j % 6 == 0)
zbroj++;
j /= 6;
}
}
printf("\nPerica je zapisao %d cifara\n", zbroj);
return 0;
}
As I was doing some practice in C, I encountered on unusual problem.So my count(zbroj) prints how many numbers were printed(in this case 3). But it won't print how many digits it has(if I input 18, it should print total digits (6,12,18), and that is 5 digits)). So I'm little confused why it prints total of numbers, but not total of digits.

The only place the value if zbroj is modified is in the loop:
j = br;
while(j != 0)
{
if(j % 6 == 0)
zbroj++;
j /= 6;
}
This is not going to count the number of digits in each number. It's going to count the number of times each number is divisible by 6 into zbroj.
To count digits, you can replace your loop with:
if (br % 6 == 0)
{
printf("%d ", br);
j = br;
while (j != 0)
{
zbroj++;
j /= 10;
}
}
Also notice that I moved the while loop into the scope of the if statement so that only the numbers that are actually printed out are counted by zbroj.
For brevity, you could also #include <math.h> and use (int) log10(j) + 1 to count digits on a match.

Related

Cant get my list to print out correctly with commas

I'm trying to get my program to find if a number is prime if it isn't then list out what the number is divisible by
#include <stdio.h>
int main()
{
int n, i, j, k = 0, c = 0;
printf("Enter an integer between 1 and 1000 (inclusive): \n");
scanf("%d", &n);
if (n > 1000 || n < 0) {
printf("You must enter a number between 1 and 1000 (inclusive).\n");
}
else
{
for (i = 1; i <= n; i++)
{
if (n % i == 0) // check divisible number from 1 to n
{
c++; // count the divisible numbers
}
}
if (c == 2) // c is 2 the number is prime
printf("%d is prime.", n);
else
{
printf("%d is divisible by ", n);
for (i = 2; i <= 31; i++) // first 11 prime numbers
{
k = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0) //i=(2,3,7,11,13,17,19,23,31)
{
k++;
}
}
if (k == 2)
{
if (n % i == 0) //if i prime number. n is divisible by i or not
printf("%d", i);
if (i < 5)
{
printf(", ");
}
}
}
printf(".");
printf("\n%d is not prime.\n", n);
}
}
return 0;
}
Currently, when I enter 62 it outputs
62 is divisible by 2, , 31.
But when I attempt to change the if(i < 3) statement than it'll mess with other printings such as trying with 468 it'll print out
468 is divisible by 2, 313.
the following proposed code:
cleanly compiles
does not check for I/O errors
performs the desired functionality
makes use of the Variable Length Arrays feature of C
and now, the proposed code:
#include <stdio.h>
int main()
{
int n, c = 0;
do {
printf("Enter an integer between 1 and 1000 (inclusive): \n");
scanf("%d", &n);
} while( n > 1000 || n < 0 );
int divisors[n];
divisors[ 0 ] = 0;
divisors[ 1 ] = 0;
for ( int i = 2; i < n; i++)
{
if (n % i == 0)
{
divisors[ i ] = i;
c++; // count the divisible numbers
}
else
divisors[ i ] = 0;
}
if ( !c )
printf("%d is prime.", n);
else
{
printf("%d is divisible by ", n);
for( int i = 0; i < n; i++ )
{
if( divisors[i] )
{
printf( "%d ", i );
}
}
printf(".");
printf("\n%d is not prime.\n", n);
}
return 0;
}
The following runs are with the values supplied by the OP
Enter an integer between 1 and 1000 (inclusive):
62
62 is divisible by 2 31 .
62 is not prime.
Enter an integer between 1 and 1000 (inclusive):
468
468 is divisible by 2 3 4 6 9 12 13 18 26 36 39 52 78 117 156 234 .
468 is not prime.
The posted code, first tests the given number against each number up to itself, counting the number of divisors, only to determine if it's prime. If it's not, then it somehow (with a lot of magic numbers) recalculates those factors and tries to print them as requested.
It would be easier to calculate the primes (once) and the list of factors first, storing them in some arrays and only then generate the desired output.
You can produce the same output while calculating each factor by keeping at least track of the number of factors already printed, if any.
In that case, I'd change the algorithm into something like this
// The orignal number will be consumed, divided by all of its factor.
int m = n;
int count = 0;
// Start from 2, the first prime, up to the last number less or equal to the sqrt(m).
for (int i = 2; i <= m / i; ++i)
{
// Check if what is left of the original number is divisible.
if ( m % i == 0 )
{
// Cunsume the number. E.g: 81 % 3 == 0 => 81 -> 27 -> 3 -> 1
// 24 % 2 == 0 => 24 -> 12 -> 6 -> 3
do
{
m /= i;
}
while ( m != 1 && m % i == 0 );
// Here, we can add the print logic and update the count of divisors
if ( count == 0 )
{
printf("%d is divisible by %d", n, i); // <- First factor
}
else
{
printf(", %d", i); // <- I-th factor
}
++count;
}
}
if ( count == 0 )
{
printf("%d is prime.\n", n);
}
else
{
if ( m != 1 )
{
printf(", %d.\n", m); // <- Last prime factor
}
puts(". It's not prime."); // 'printf(".\n%d It's not prime.\n", n);' if you prefer
}
Testable here.
The way of printing was the problem. It found 2 and printed out 2,. It found 3 and printed out 3, then it found 13 as the last divisor. From this you got 2, 313 as output.
I modified the part when the number is not a prime:
int first_divisor = 1;
printf("%d is divisible by ", n);
for (i = 2; i <= 31; i++) // first 11 prime numbers
{
k = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0) //i=(2,3,7,11,13,17,19,23,31)
{
k++;
}
}
if ((k == 2) && (n % i == 0))
{
if (first_divisor == 0)
{
printf(", ");
}
printf("%d", i);
first_divisor = 0;
}
}
printf(".");
printf("\n%d is not prime.\n", n);

C program to find the trailing ZEROS at the end of a FACTORIAL of a given number

I have return the code to find a factorial and to display trailing zeros at the end of the factorial, but the output is wrong... could you please help me to find the mistake?
#include <stdio.h>
int main() {
int m = 1, i, N, count = 0;
scanf("%d", &N);
for (i = 1; i <= N; i++) {
m = m * i;
}
printf("%d", m);
while (m > 0) {
if ((m % 10) == 0) {
count = count + 1;
m = m / 10;
}
break;
}
printf("%d", count);
return 0;
}
Your code only works for very small values of N: up to 9. For slightly larger values, you would need to add an else keyword before the break statement and you would get a correct result for a few more cases.
For larger values, you must compute the power of 5 that divides the factorial. You can do this incrementally by summing the power of 5 that divide each individual number up to and including N.
#include <stdio.h>
int main() {
int N, count;
if (scanf("%d", &N) != 1)
return 1;
/* only consider factors that are multiples of 5 */
count = 0;
for (int i = 5; i <= N; i += 5) {
for (int j = i; j % 5 == 0; j /= 5)
count++;
}
printf("%d\n", count);
return 0;
}
An even simpler and faster solution is this: compute the number of multiples of 5 less or equal to N, add the number of multiples of 5*5, etc.
Here is the code:
#include <stdio.h>
int main() {
int N, count;
if (scanf("%d", &N) != 1)
return 1;
count = 0;
for (int i = N; (i /= 5) > 0;) {
count += i;
}
printf("%d\n", count);
return 0;
}
you have two problems
your collapse the two outputs so you see only one of them / you cannot see who is who, just add a separator between them
an else is missing when you count so you count to only up to 1 and the result is wrong from factorial 10
So the minimal changes produce :
int main()
{
int m=1,i,N,count=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
m=m*i;
}
printf("%d\n",m); /* <<< added \n */
while(m>0)
{
if((m%10)==0)
{
count=count+1;
m=m/10;
}
else /* <<< added else */
break;
}
printf("%d\n",count); /* <<< added \n */
return 0;
}
after the changes :
pi#raspberrypi:/tmp $ ./a.out
5
120
1
pi#raspberrypi:/tmp $ ./a.out
10
3628800
2
Of course that supposes first you are able to compute the factorial without overflow
I also encourage you to check a value was read by scanf, checking it returns 1
#include <stdio.h>
int main()
{
int n,i,f=1,t,c=0;
printf("Enter number ");
scanf("%d",&n);
t=n;
for(i=1;t>=5;i++)
{
t=n/5;
c=c+t;
n=t;
}
printf("number of zeros are %d",c);
return 0;
}

How to print multipliers 3 and 5, but not print multipliers 15?

Here's the program I already made:
#include <stdio.h>
int main()
{
int i,n,t,t2;
printf("enter limit number: ");
scanf("%d", &n);
for (i = 1; i<=n; i++)
{
t = i * 3;
t2 = i * 5;
printf("%d, " ,t);
printf("%d, ",t2);
}
}
How to print multipliers of 3 and 5, but not print multipliers 15?
You can check if an integer is a evenly divisible by another by using the modulus operator. If the result (the remainder) is zero, then it's evenly divisible.
Your code doesn't actually stop at the limit, it prints the numbers out of order, it prints a trailing comma, and it doesn't print a terminating newline. Fixed:
int matches = 0;
for (i=1; i<=n; ++i) {
if ((i % 3) == 0 || (i % 5) == 0) {
if ((i % 15) != 0) {
if (matches++) {
printf(", ");
}
printf("%d", t);
}
}
}
if (matches)
printf("\n");
How to print multiplier 3 and 5, but not print multiplier 15?
So what can you know from this, the number that is being printed when divided with 15 must no produce remainder as 0 because then, it'd be multiplier of 15 so just put your printf statements in a if loop like this:
if((t % 15) != 0)
{
printf("%d, " ,t);
}
if((t2 % 15) != 0)
{
printf("%d, ",t2);
}
see this for working example: https://ideone.com/2k0qLW

find the prime numbers in C

I have this code for prime numbers..it gives me the prime numbers up to 103.Maybe my first break statement is wrong? I did it this way because i want to skip as much numbers as possible.i want only the primes that have at least two digits(that is why i started from 11)
#include <stdio.h>
#define MAXNUMB 100000000
int main (void)
{
int i, j;
for (i = 11 ; i < MAXNUMB ; i += 2)
{
if ((i % 3 == 0) && (i % 5 == 0) && (i % 7 == 0))
break;
for (j = 3 ; j * j <= i ; j += 2)
{
if (i % j == 0)
break;
}
if (j * j > i)
printf ("%d \n", i);
}
}
Use continue rather then break; Here you want to skip much number ( As if there are many ways to find the prime numbers in small complexity like much popular Sieve of Eratosthenes) but it breaks. So change a little..
if((i%3==0)&& ( i%5==0) && (i%7==0))
continue;// here
it works..
#include <stdio.h>
#include <string.h>
#define MAXNUMB 100000000
int main (void)
{
int i, j, P[MAXNUMB];
memset(P, 0, sizeof(P));
for(i = 2; i < MAXNUMB; ++i)
{
if(P[i] == 0){
printf("%d\n", i);
if(i < MAXNUMB / i)
for(j = i*i; j < MAXNUMB; j += i)
P[j] = 1;
}
}
return 0;
}
As it would be obvious using a debugger, that statement is wrong. First, you check for numbers that are divisible by 3 and 5 and 7. 105 is divisible by all. Then you break, which means "exit this for loop", so the program will end. You need to just continue the looping and not report this number as a prime.
You want to change the loop to use or instead of and. Also the next for loop doesn't need to start from 3 then, since you already tried 3, 5, 7. But is there a reason for that since you could just use the for loop?
My suggestion would be to just forget the first if altogether since it will not make it any faster.
#include <stdio.h>
#define MAXNUMB 100000000
int main (void)
{
int i, j, f;
for (i = 11; i < MAXNUMB; i += 2)
{
for (j = 2, f = 1; f && j * j < i; ++j)
{
f = (i % j != 0);
}
if (f) printf("%d\n", i);
}
return 0;
}

Prime numbers in C and is_prime

I'm writing a program to find all of the prime numbers contained within a user input n. I am having trouble with the is_prime function.
#include <stdio.h>
#include <math.h>
main() {
int n;
int k;
// gets user input for length of string
// and stores it as n
printf("Enter the value of n:");
scanf("%d", &n);
for (k = 2; k <= n; k++) {
if (is_Prime(k) == 1) {
printf("Printing primes less than or equal to %d: /n %d, &n, &k");
}
}
I want the output to look like this, but I am not sure how to print the list without using different variables for each prime number.
Printing primes less than or equal to 30:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29.
//here is the is_Prime function
is_Prime (int n)
{
for(j = 2; j <= n/2; j++)
{
if(n%j != 0)
{
return 1;
break;
}
}
if(n%j == 0 )
return 0;
}
I am not sure how to call the is_prime subroutine? Any help?
printf("Printing primes less than or equal to %d:\n", n);
for(k = 2; k <= n; k++)
{
if(is_Prime(k) == 1)
{
printf("%d, ", k);
}
}
printf("Printing primes less than or equal to %d:\n%s", n, (n >= 2 ? "2" : ""));
for (k = 3; k <= n; ++k)
if (is_Prime(k))
printf(", %d", k);
printf("%s\n", (n >= 2 ? "." : ""));
Here's a slightly cleaner version of your is_Prime function:
int is_Prime(int n)
{
if (n < 2)
return 0;
int last = (int) sqrt(n) + 1; /* conservatively safe */
for (int j = 2; j <= last; ++j)
if (0 == n % j)
return 0;
return 1;
}
Note that you only really need to check up to the sqrt() of a number to find all its potential factors.
Also note that this is not a great way to find all the primes less than n, which is the prime purpose of your program, especially when you will repeatedly call this function incrementing n by 1 each time. I recommend trying to implement the Sieve of Eratosthenes or the Sieve of Sundaram instead -- so long as n isn't too large.

Resources