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
Related
I need to input my own array and give its own elements, from that array i need to print the same one but if theres a number that is prime, it needs to switch it with the next number. Example:
My array: 4 6 3 5 7 11 13
The new array: 4 6 5 3 11 7 13
Here prime numbers are, 3 5 7 and 13, but 13 doesnt have an element to switch itself, so it stays the same.
#include <stdio.h>
#define array 100
int prime(int b
)
{
int i;
for (i = 2; i <= b / 2; i++)
{
if (b % i == 0)
{
return b; // not prime
}
break;
}
return b;
}
int main()
{
int n, i, a[array];
printf("How many elements does the array have?\n");
scanf("%d", &n);
printf("Put in %d elements from the array!\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("My array is: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
for (i = 0; i < n; i++)
{
if (prime(a[i]))
{
int temp;
temp = prime(a[i]);
prime(a[i]) == prime(a[i + 1]);
}
}
printf("\nThe new array is:\n");
printf("%d ", prime(a[i]));
return 0;
}
I haven't learned pointers so is there a way without it or?
there are few things needs to modify
need to change function prime return type to bool. since we are interest to check if array element is Prime. if array element is Prime, return True
int prime(int b)
changed to
bool prime(int b)
also need to extend check if prime() function return true and if array index is not last element then only swap array element to next, else skip
if (prime(a[i]) == 1 && a[i-1] != n)
prost(a[i]) looks typo (I guess). corrected to a[i + 1]
this is not optimized code, it just modified version of your code. if you have concern specific performance, please follow suggestion mentioned by
chux - Reinstate Monica
code:
#include <stdbool.h>
#include <stdio.h>
#define array 100
bool prime(int b)
{
int i;
for (i = 2; i <= b / 2; i++)
{
if (b % i == 0)
{
return false; // not prime
}
break;
}
return true;
}
int main()
{
int n, i, a[array];
int temp;
printf("How many elements does the array have?\n");
scanf("%d", &n);
printf("Put in %d elements from the array!\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("My array is: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
for (i = 0; i < n; i++)
{
if (prime(a[i]) == 1 && a[i] != a[n-1]) /* enter loop only array element is Prime number and it is not last element */
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
a[i++];
}
printf("\nThe new array is:\n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Output for above code: check out this link
How many elements does the array have?
7
Put in 7 elements from the array!
4
6
3
5
7
11
13
My array is:
4 6 3 5 7 11 13
The new array is:
4 6 5 3 11 7 13
...Program finished with exit code 0
Press ENTER to exit console.
First of all, you have a for loop that only makes one iteration because of a break keyword, also in main in a for loop with your swapping you need to assign return values from the prime function to variables, and in the same function, you should use singe '=' because you want to assign value but not to compare. Also in your same for loop, you should check if(prime(a[i+1])) so there won't be any segfaults.
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 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;
}
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.
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