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
Related
I have a problem to solve which is. I have to make a program which will take 2 numbers from the sequence of numbers and compare them if it's '<' '>' or '=' and the list of numbers needs to end with number 0. So basically I have a sequence of numbers {5, 7, 8, 4, 3, 3, 0} and the program have to check by couples 5,7 (it is 5 < 7) then it will go to 8, 4 (it is 8 > 4) then 3, 3 so it is 3 = 3. And the 0 is basically as the exit.
So far I wrote down the comparison of the numbers but now I only have a program which takes 2 inputs from the user and compares them. But I kinda need to specify for the user let's say to enter 11 numbers which will end with 0 (as 0 will not be counted to the comparison) and store those numbers in an array and then let the program to compare the 2 numbers after each other (in a sequence) with <, > or =.
Thanks in advance guys. I'm kinda new to C and these arrays and specially malloc, calloc, realloc are really complicated for me.
So far my code looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define post 10
int main(){
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
if (a > b)
{
printf("%d > %d\n", a, b);
}
else if(a < b)
{
printf("%d < %d\n", a, b);
}
else
{
printf("%d = %d\n", a, b);
}
system("pause");
return 0;
}
You can create large array and then input how many number you want to compare. Then you can use for to walk through the numbers in the array and compare them.
Here is exemple:
#include <stdio.h>
#define SIZE 100
int main()
{
int arr[SIZE] = {0};
printf("Enter number: ");
int number;
int counter = 0;
while (1)
{
if (scanf("%d", &number) != 1)
{
printf("Invalid number!");
return 1;
}
arr[counter] = number;
if(number == 0) {break;}
counter++;
}
for (int i = 0; i < counter; i+=2)
{
if (arr[i] > arr[i + 1])
{
printf("%d > %d\n", arr[i], arr[i + 1]);
}
else if(arr[i] < arr[i + 1])
{
printf("%d < %d\n", arr[i], arr[i + 1]);
}
else
{
printf("%d = %d\n", arr[i], arr[i + 1]);
}
}
return 0;
}
Output:
Enter number: 1 2 3 4 5 5 0
1 < 2
3 < 4
5 = 5
I am currently making a program for the collatz sequence on C, however the last value, which is 1, is not being printed. For example, when I input 8, the outcome must be 8 4 2 1, but it only prints 8 4 2, or when I input 5, it only prints 5 16 8 4 2. What can I put inside the while ( ) to print the complete answer? Thank you!!
void
CollatzSequence(int n)
{
int x = 1;
do {
x++;
printf("%3d", n);
if (n%2==0)
n /= 2;
else
n = 3 * n + 1;
}
while ( );
printf("\n");
}
int
main()
{
int n;
do {
printf("Input an integer greater than 0: ");
scanf("%d", &n);
if (n <= 0)
printf("Invalid input. Try again.\n");
} while (n <= 0);
CollatzSequence(n);
return 0;
}
Code needs new loop exit condition.
Loop is done once 1 is printed.
Sample below.
void CollatzSequence(int n) {
for (;;) {
printf("%3d", n);
if (n == 1)
break;
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
}
printf("\n");
}
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);
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.
I'm writing a program to determine whether or not a given number is a prime number or a composite. My code below is what i have so far. I'm thinking that using a WHILE LOOP wouldn't be as efficient as a FOR LOOP but i may be wrong.
#include <stdio.h>
int main(int argc, const char* argv[]) {
int num, i;
printf("Enter a number you want to check:");
scanf("%d", &num);
if (num == 1) {
printf("%d is neither prime nor composite", num);
}
i = 2;
while (i <= num - 1) {
if (num % i == 0) {
printf("%d is composite\n\n", num);
break;
}
}
}
The program works with most even numbers but when I get to, say 9 I don't get a return because it's not dividing by three. Could i add into this WHILE LOOP to compensate or would i be easier to use a FOR LOOP?
I'm thinking that if i used a FOR LOOP i could start it off sort of like this.
for (i = 2, i <= num, i++) {
num % i == 0;
}
printf("%d is Composite", num);
}
you have forgotten to increase the index inside your loop
while (i <= num-1) {
if (num%i==0)
{
printf("%d is composite\n\n",num);
return; // test is finished
}
i++; // increase i by 1
}
printf("%d is prime number\n", num); // add this line to display the prime numbers also
EDIT
I just came to see your comment about the use of for loop :
for (i = 2; i < num; i++) // ① be careful of ; and , ② i<num not i<=num
{
if(num % i == 0) // if your num is dividable ==> not prime
{
printf("%d is Composite", num);
return 0; // return from the main func
}
}
printf("%d is prime number\n", num); // no divisor = no return = prime number
EDIT 2
Now let's talk about efficiency :
to determine whether the number is prime or not, you can iterate for less than even half of it, how ?
if p is the number and k is its divisor then :
p = k * n
if ( n > sqrt(p) && k > sqrt(p))
==> n * k > p
take any pair of divisors for any integer, both the divisors cannot be greater than the square root of the integer at the same time!
that's why you can iterate like this:
while (i <= sqrt(num)) {
if (num%i==0)
{
printf("%d is composite\n\n",num);
return; // test is finished
}
i++; // increase i by 1
}
So for your 10000 you only iterate 100 time! and not 5000 as you thought ;)
FURTHER
If you're really on a tight budget, you can check only for odd numbers greater than 2 :D, I mean if it was not dividable by 2 then it'll never be dividable by 4, 6, 8 ... skip them
Example :
if(num%2 == 0) // check for 2
{
printf("%d is composite\n\n",num);
return;
}
while (i <= sqrt(num)) // check only for odd # greater than 2
{
if (num%i==0)
{
printf("%d is composite\n",num);
return; // test is finished
}
i+=2; // increase i by 2
}
printf("%d is prime\n", num);
Where you wrote:
for (i = 2, i <= num, i++)
you want:
for (i = 2; i < num; i++)
you might also think about why this:
for (i = 2, i*i <= num, i++)
might be a big win...