Printing Prime numbers in a range - c

My code is not running the loop. I've tried changing it from for to while loop, tried changing the condition for prime number but still the same response. I've attached my output in this program output image.
#include<stdio.h>
int main(void) {
int a, b, flag = 0;
int i, j;
printf("Enter lower limit of range\n");
scanf("%d", &a);
printf("Enter upper limit of range\n");
scanf("%d", &b);
printf("The prime numbers between %d and %d are\n", a, b);
i = a;
j = 2;
while (i <= b) {
while ( j < i/2 )
{
if (i % j == 0)
{
flag = 1;
}
j++;
}
if (flag == 0)
{
printf("%d\n", i);
}
i++;
}
return 0;
}

You need at least to reset the variable flag to 0 and j to 2 in each iteration of the outer loop. Also the condition j < i /2 should be changed to j <= i / 2 (for example when i is equal to 4).
while (i <= b) {
flag = 0;
j = 2;
while ( flag == 0 && j <= i/2 )
{
if (i % j == 0)
{
flag = 1;
}
j++;
}
if (flag == 0)
{
printf("%d\n", i);
}
i++;
}
Pay attention to that the program can incorrectly consider 2 as a non-prime number and 1 as a prime number.
I would write the loops the following way
for ( ; i <= b; i++ )
{
int prime = i % 2 == 0 ? i == 2 : ( i == 3 ) || ( i != 1 && i % 3 != 0 );
for ( int j = 5; prime && j <= i / j; j += 6 )
{
prime = i % j != 0 && i % ( j + 2 ) != 0;
}
if ( prime )
{
printf("%d\n", i);
}
}
Or a more simple variant
for ( ; i <= b; i++ )
{
int prime = i % 2 == 0 ? i == 2 : i != 1;
for ( int j = 3; prime && j <= i / j; j += 2 )
{
prime = i % j != 0;
}
if ( prime )
{
printf("%d\n", i);
}
}
You should declare the variables a and b as having the type unsigned int instead of int to avoid using negative numbers.
Here is a demonstration program.
#include <stdio.h>
int main( void )
{
unsigned int a = 0, b = 0;
printf( "Enter lower limit of range " );
scanf( "%u", &a );
printf( "Enter upper limit of range " );
scanf( "%u", &b );
printf( "The prime numbers between %u and %u are\n", a, b );
for ( unsigned int i = a; i <= b; i++ )
{
int prime = i % 2 == 0 ? i == 2 : i != 1;
for ( unsigned int j = 3; prime && j <= i / j; j += 2 )
{
prime = i % j != 0;
}
if (prime)
{
printf( "%u ", i );
}
}
putchar( '\n' );
}
The program output might look like
Enter lower limit of range 0
Enter upper limit of range 100
The prime numbers between 0 and 100 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Related

How can i multiply the prime numbers of a number from user input and display it in C language

So I started learning C language for Uni and got stuck with this exercise, I found a way to ge the prime numbers of a number but I don't know how to multiply the prime numbers and display them.
int main()
{
int number;
int prime;
int i,j;
printf("Insert number:");
scanf("%d", &number);
printf("Prime numbers of %d are: ",number);
for(i = 2; i <= number; i++)
{
prime = 1;
for(j = 2; j <= i/2; j++)
{
if(i % j == 0)
{
prime = 0;
}
}
if(prime == 1)
{
printf(" %d", i);
}
}
return 0;
}
You need to introduce one more variable that will store the nultiplication of prime numbers.
Here is a demonstration program.
#include <stdio.h>
int main( void )
{
unsigned int number;
printf( "Insert number (0 - exit ): " );
if ( scanf( "%u", &number ) == 1 && number != 0 )
{
if ( number < 2 )
{
printf( "There are no prime numbers in the range [0, %u].\n", number );
}
else
{
unsigned long long product = 2;
printf( "Prime numbers in the range [0, %u] are: ", number );
printf( "%u ", 2 );
for ( unsigned int i = 3; i <= number; i += 2 )
{
int prime = 1;
for( unsigned int j = 3; prime && j <= i / j; j += 2 )
{
if ( i % j == 0 )
{
prime = 0;
}
}
if ( prime )
{
printf( "%d ", i );
product *= i;
}
}
printf( "\nTheir multiplication is equal to %llu\n", product );
}
}
}
Its output might look like
Insert number (0 - exit ): 10
Prime numbers in the range [0, 10] are: 2 3 5 7
Their multiplication is equal to 210

Pyramid patterns using C

I need to do the following pattern using do-while, while or for.
I tried the following code but it prints the pattern only 1-5
I also tried to alter the n being 10 but then the spacing goes nuts.
#include <stdio.h>
int main(void)
{
int n = 5, i, j, num = 1, gap;
gap = n - 1;
for ( j = 1 ; j <= n ; j++ )
{
num = j;
for ( i = 1 ; i <= gap ; i++ )
printf(" ");
gap --;
for ( i = 1 ; i <= j ; i++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( i = 1 ; i < j ; i++)
{
printf("%d", num);
num--;
}
printf("\n");
}
return 0;
}
Try replacing both occurences of:
printf("%d", num);
with
printf("%d", num % 10);
Now only the last digit will be shown.
After the change, for n set to 10 the program produces:
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210

Find common element in two from three arrays

I need to find elements of array which are hold in two of three given arrays.
It seems easy, but it's quite dificult and i have been strugling with this for few days.
I hope you can help me..
For input:
1 2 3 5
1 2 4 6 7
1 3 4 8 9 10
Output should be 3 (because 3,4,2 are common for two arrays)
for input
1 2 3 4
2 3 4
3 4 1
Output should be: 2 (because 1 is common for two arrays)
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main()
{
int duzina_prvog_niza = 0, duzina_drugog_niza = 0, duzina_treceg_niza = 0; //deklaracija duzina nizova
printf("Unesite broj clanova prvog niza:\n"); // unosimo duzine nizova i elemente nizova
do
{
scanf("%d", &duzina_prvog_niza);
} while (0 > duzina_prvog_niza || duzina_prvog_niza > 50); // mozda ne bi trebalo stavljati gornju granicu za duzinu niza
int niz1[duzina_prvog_niza]; //zavisi kako vam sistem provjere radi, mislim da nece praviti problem
// alociramo niz odgovarajuce duzine, iterativno popunimo niz uz odgovarajucu provjeru
for (int i = 0; duzina_prvog_niza > i; i++)
{
do
{
scanf("%d", &niz1[i]);
} while (0 > niz1[i] || niz1[i] > 10); // citaj elemente sve dok ne ucitas cifre iz odgovarajuceg opsega
}
for (int i = 0; duzina_prvog_niza > i; i++)
printf(" %d ", niz1[i]);
printf("\n");
// ** drugi niz ** -- bilo bi zgodno ovo sve strpati u jednu fju, meni je ovako bilo lakse.. c/p
printf("Unesite broj clanova drugog niza:\n");
do
{
scanf("%d", &duzina_drugog_niza);
} while (0 > duzina_drugog_niza || duzina_drugog_niza > 50);
int niz2[duzina_drugog_niza];
// alociramo niz odgovarajuce duzine, iterativno popunimo niz uz odgovarajucu provjeru
for (int i = 0; duzina_drugog_niza > i; i++)
{
do
{
scanf("%d", &niz2[i]);
} while (0 > niz2[i] || niz2[i] > 10); // citaj elemente sve dok ne ucitas cifre iz odgovarajuceg opsega
}
for (int i = 0; duzina_drugog_niza > i; i++)
printf(" %d ", niz2[i]);
printf("\n");
// ** treci niz **
printf("Unesite broj clanova treceg niza:\n");
do
{
scanf("%d", &duzina_treceg_niza);
} while (0 > duzina_treceg_niza || duzina_treceg_niza > 50);
int niz3[duzina_treceg_niza];
// alociramo niz odgovarajuce duzine, iterativno popunimo niz uz odgovarajucu provjeru
for (int i = 0; duzina_treceg_niza > i; i++)
{
do
{
scanf("%d", &niz3[i]);
} while (0 > niz3[i] || niz3[i] > 10); // citaj elemente sve dok ne ucitas cifre iz odgovarajuceg opsega
}
for (int i = 0; duzina_treceg_niza > i; i++)
printf(" %d ", niz3[i]);
printf("\n");
//pocetna vrijednost brojaca mora biti nula!
int brojac = 0;
int pomocni_brojac = 0;
// (S_1 intersect S_2) union (S_2 intersect S_3) union (S_3 intersect S_1) -- matematicko rjesenje problema
int x;
int pomocni_niz[duzina_prvog_niza + duzina_drugog_niza + duzina_treceg_niza];
for (int i = 0; duzina_prvog_niza + duzina_drugog_niza + duzina_treceg_niza > i; i++)
pomocni_niz[i] = 0;
int max;
if(duzina_prvog_niza>=duzina_drugog_niza && duzina_prvog_niza>=duzina_treceg_niza) max=duzina_prvog_niza;
if(duzina_drugog_niza>=duzina_prvog_niza && duzina_drugog_niza>=duzina_treceg_niza) max=duzina_drugog_niza;
if(duzina_treceg_niza>=duzina_drugog_niza && duzina_treceg_niza>=duzina_prvog_niza) max=duzina_treceg_niza;
//prolazimo kroz sve elemente u sva tri niza i poredimo sve elemente sa svim elementima
for (int i = 0; duzina_prvog_niza > i; i++)
{
for (int j = 0; duzina_drugog_niza > j; j++)
{
for (int k = 0; duzina_treceg_niza > k; k++)
{ // ako je element iz prvog niza jednak elementu iz drugog niza, ili je element
if (((niz1[i] == niz2[j]) && (niz2[j] == niz3[k]) && (niz1[i] == niz3[k])))
1;
else if ((niz1[i] != niz2[j]) && (niz2[j] == niz3[k]) && (niz1[i] != niz3[k]))
pomocni_niz[pomocni_brojac++] = niz2[j];
else if ((niz1[i] == niz2[j]) && (niz2[j] != niz3[k]) && (niz1[i] != niz3[k]))
pomocni_niz[pomocni_brojac++] = niz1[i];
else if ((niz1[i] != niz2[j]) && (niz2[j] != niz3[k]) && (niz1[i] == niz3[k]))
pomocni_niz[pomocni_brojac++] = niz2[j];
}
}
}
int y = 0;
for (int g = 0; pomocni_brojac > g; g++)
{
for (int l = 0; pomocni_brojac > l; l++)
{
if (pomocni_niz[g] == pomocni_niz[l])
y++;
}
if (y == 0)
brojac++;
y = 0;
}
for (int i = 0; brojac > i; i++)
printf("%d ", pomocni_niz[i]);
printf("U dva od tri niza se nalazi %d clanova.", brojac);
return 0;
}
Thanks!
There exists a pretty fast solution to your problem. You will need three more arrays each having a size of 100. Each array will record the frequency of any particular input array. The size of each frequency array is 100 since any input array will only consist of numbers in the range 0-99.
For example:
Input arrays:
A: 1 2 3 5
B: 1 2 4 6 7
C: 1 3 4 8 9 10
Frequency arrays:
0 1 2 3 4 5 6 7 8 9 10
A: 0 1 1 1 0 1 0 0 0 0 0
B: 0 1 1 0 1 0 1 1 0 0 0
C: 0 1 0 1 1 0 0 0 1 1 1
In the frequency arrays section:
The top row denotes number which may be present in any input array and the rows below contains their frequency in each input array..
Algorithm
1 : let frequencyA[100]
2 : let frequencyB[100]
3 : let frequencyC[100]
4 :
5 : for i = 0 to A.length-1
6 : if (frequencyA[A[i]] == 0) frequencyA[A[i]]++
7 : for i = 0 to B.length-1
8 : if (frequencyB[B[i]] == 0) frequencyB[B[i]]++
9 : for i = 0 to C.length-1
10: if (frequencyC[C[i]] == 0) frequencyC[C[i]]++
11:
12: for i = 0 to 99
13: if (frequencyA[i]+frequencyB[i]+frequencyC[i] == 2) Print i
The algorithm is pretty straight forward. The only that lines that deserve some explanation are mentioned below.
Line 5-10:
For each input array, we loop though each of its element and record their frequency. We record the frequency of any particular element only once, that is, if any element repeats in a single array, we will record its frequency only once. This is made sure by the if condition which checks if we have recorded the frequency of any element before or not.
Line 12-13:
We start a loop from 0 to 99 since they are the possible values of the array. In the loop, we check if sum of the frequency in the all the three frequency arrays of any element is 2 or not. If its 2, then that element is present in present exactly twice else not.
Time Complexity
The algorithm has a time complexity of O(A.length + B.length + C.length). It is a linear time complexity which is quite fast.
I can not provide you with any code as I do not code in C a lot. I hope I have helped you. If you face any trouble in understanding my answer, please do comment. I will be happy to update my answer.
For starters always use English words for identifiers. In this case your code will be readable for a larger auditorium. Otherwise it is difficult to read it.
This statement in your program
if (((niz1[i] == niz2[j]) && (niz2[j] == niz3[k]) && (niz1[i] == niz3[k])))
1;
does not make a sense.
If you need to output common elements that are present exactly in two of three arrays then there is no great sense to create a forth array with the size equal to the sum of sizes of the three arrays.
If the arrays can be unsorted and you may not sort the arrays then a straightforward approach can look the following way as it is shown in the demonstrative program below.
#include <stdio.h>
void f( const int a1[], size_t n1, const int a2[], size_t n2, const int a3[], size_t n3 )
{
size_t total = 0;
for ( size_t i1 = 0; i1 < n1; i1++ )
{
size_t count = 1;
size_t i2 = 0;
while ( i2 < n2 && a2[i2] != a1[i1] ) i2++;
count += i2 != n2;
size_t i3 = 0;
while ( i3 < n3 && a3[i3] != a1[i1] ) i3++;
count += i3 != n3;
if ( count == 2 )
{
++total;
printf( "%d ", a1[i1] );
}
}
for ( size_t i2 = 0; i2 < n2; i2++ )
{
size_t i1 = 0;
while ( i1 < n1 && a1[i1] != a2[i2] ) i1++;
if ( i1 == n1 )
{
size_t i3 = 0;
while ( i3 < n3 && a3[i3] != a2[i2] ) i3++;
if ( i3 != n3 )
{
++total;
printf( "%d ", a2[i2] );
}
}
}
if ( total != 0 ) putchar( '\n' );
printf( "%zu\n", total );
}
int main(void)
{
int a[] = { 1, 2, 3, 5 };
int b[] = { 1, 2, 4, 6, 7 };
int c[] = { 1, 3, 4, 8, 9, 10 };
size_t n1 = sizeof( a ) / sizeof( *a );
size_t n2 = sizeof( b ) / sizeof( *b );
size_t n3 = sizeof( c ) / sizeof( *c );
f( a, n1, b, n2, c, n3 );
return 0;
}
The program output is
2 3 4
3

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

Using for loop to print out output

My for loops seems not working properly. First number which tells me how many task do I want my program to do is n. When I input 1 or 2, it works, but when I input 3 and more, it starts to struggle. Every row has 3 numbers separated by a space as you can see in the code. I am not getting all outputs. Algorithm in this program works perfect so no problem there.
Please ignore comments in the code. And sorry for my english.
#include <stdio.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int r[];
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
//printf ( "max 1000 uloh min 1 \n");
return 1;//scanf("%d", &n);
}
for( i=1; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000)) {
//printf ("prekroceny limit \n");
return 1; // scanf("%d %d %d", &s, &p, &d);
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] == 0;
}
else
{
//s = k * d + A ;
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < i+1 ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
system("pause");
}
Example input so you can understand better:
4
5 4 4
6 100 3
500 5 1000
314 159 26
and output:
3
0
3
147
EDIT>
5
3434 234 2345
14455 345 12
134 145 1345
9242 2455 13455
83 34 133
output:
126
144
141
769
13
or something shorter>
input:
2
15000 1 1
1 4000 1
output:
0
0
Im getting return value 1 in both examples
My final code edit:
#include <stdio.h>
#include <stdlib.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int *r;
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
return 0;//printf ( "max 1000 uloh min 1 \n");
//scanf("%d", &n);
}
r = (int *)malloc(n*sizeof(int));
for(i=0; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000))
{
//printf ("prekroceny limit \n");
return 0; //scanf("%d %d %d", &s, &p, &d);
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] = 0;
}
else
{
//s = k * d + A ;
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < n ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
free(r);
return 0;
}
You need to use malloc() to allocate memory dynamically. (Also #include stdlib.h)
For instance, add the following line after the first while loop :
//Note - at the top, change int r[] to the below:
int *r; //Instead of int r[]
//End note
while( !(n >= 1 && n <=1000) )
{
printf ( "max 1000 uloh min 1 \n");
scanf("%d", &n);
}
r = (int *) malloc(n*sizeof(int)); //Allocate "n" integers to your 'array r'
It should also be noted that once you are done using the array r, you must free() this memory like so:
free(r);
In addition, there are some other coding problems:
Be sure for( i=1; i < n; i++) is what you want. You use "i" to index your array, but arrays in C start from 0. So it (likely) should read for( i=0; i < n; i++).
r[i] == 0; is used falsely as an assignment. Use a single = to assign a value to a variable. Use the double equals == to compare two values.
for ( koniec = 0 ; koniec < i+1 ; koniec++ ) will likely through an error. Your i will be one less than n from the for-loop above it. So trying to iterate to i+1 or n will result in an array out of bounds problem -- aka a Segmentation Fault.
Edit - This should do what you want:
#include <stdio.h>
#include <stdlib.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int *r;
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
//printf ( "max 1000 uloh min 1 \n");
//scanf("%d", &n);
return 1;
}
r = (int *)malloc(n*sizeof(int));
for(i=0; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000))
{
//printf ("prekroceny limit \n");
//scanf("%d %d %d", &s, &p, &d);
return 1;
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] = 0;
}
else
{
s = k * d + A ; //This line does nothing... Just so you know
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < n ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
free(r);
system("pause");
return 0;
}
Input:
4
5 4 4
6 100 3
500 5 1000
314 159 26
Output:
3
0
3
147
Allocate space for r[].
Change assignment.
Change for loop range.
//int r[];
main() {
scanf("%d", &n);
// while( !(n >= 1 && n <=1000) )
if (!(n >= 1 && n <=1000) ) {
return 1;//scanf("%d", &n);
}
int r[n]; // allocate
// for( i=1; i < n; i++)
for( i=0; i < n; i++) // Change range (#MrHappyAsthma)
...
// r[i] == 0;
r[i] = 0; // change
r[i] = p - x ;
// for ( koniec = 0 ; koniec < i+1 ; koniec++ )
for ( koniec = 0 ; koniec < n ; koniec++ )

Resources