The output is weird on my C Console App. Any ideas? - c

I want to make an app that determines whether the given input is a positive number, and then prints them out if there are more than 2 positive numbers, but there is a weird output which I've been trying to fix for a few hours.
Note : I'm somewhat of a beginner in C.
#include <stdio.h>
int main() {
const char arr[][20] = {"First Number : ", "Second Number : ", "Third Number : "};
int numbers[2]; // number list
int posNumbers[2]; // positive number list
int n = 0; // n for itinerating posNUmbers
for ( int i = 0 ; i <= 2; i++) {
printf("%s", arr[i]);
scanf("%i", &numbers[i]);
} // puts input in array
for ( int i = 0 ; i <= sizeof(numbers) / sizeof(numbers[0]) + 1 ; i++ ) {
if (numbers[i] > 0) {
posNumbers[n + 1] = numbers[i];
}
} // adds positive in array
if (sizeof(posNumbers) / sizeof(posNumbers[0]) + 1 > 1) {
printf("There are atleast 2 pos numbers, which are : \n");
for ( int i = 0; i <= sizeof(posNumbers) / sizeof(posNumbers[0]) + 1 ; i++) {
printf("%i", posNumbers[i]);
}
} else {
printf("THere are not enough positive numbers.");
}
}
Output : There are atleast 2 pos numbers, which are : 4419368778941968054388

You have several misconceptions about arrays and the use of sizeof that cause you to attempt to access values beyond the end of your numbers and posNumbers arrays that invokes Undefined Behavior1 in your code. You further invoke Undefined Behavior when you attempt to read from uninitialized elements in the posNumbers array.
In C, arrays are:
zero based. Meaning that an array of nelem elements has valid indexes of 0 <= index < nelem,
when looping over all elements in an array, you loop from i = 0; i < nelem; i++,
if you fail to initialize your arrays all zero and do not fill all elements and attempt to loop over all elements in your array, you invoke Undefined Behavior when you attempt to access the uninitialized element (lesson -- initialize all arrays to begin with),
when you use sizeof array / sizeof array[0], you get the total number of elements in the array, not the number of elements filled,
your loop over posNumbers ignores the fact that less than all elements can be filled invoking Undefined Behavior if you attempt to read from an uninitialized element of the array,
when filling less than all values in an array, simply keep a counter to track the number of elements filled, you have n declared already.
Additional points to consider:
avoid using MagicNumbers in your code and instead #define a constant. This helps avoid the problems you are having with 2,
above all learn you cannot use scanf() (or any user input function) correctly unless you check the return, especially where numeric conversions are involved. What if the user enters "four" instead of 4?
Putting it altogether, and making a few additional changes to your output calls, you can rewrite your code to avoid the problems above as:
#include <stdio.h>
#define NVALS 3 /* if you need a constant, #define one (or more) */
int main (void) {
/* an array of pointers to the string-literals is fine */
const char *arr[] = { "First Number : ",
"Second Number : ",
"Third Number : " };
int numbers[NVALS] = {0}, /* initialize all arrays */
posNumbers[NVALS] = {0},
n = 0;
/* loop NVALS times reading number input */
for (int i = 0 ; i < NVALS; i++) {
/* no conversion involved, fputs is fine for end-of-line control */
fputs (arr[i], stdout);
/* you can't use scanf correctly unless you CHECK THE RETURN
* to validate each conversion was successfully completed
*/
if (scanf("%i", &numbers[i]) != 1) {
fputs ("error: invalid integer input.\n", stderr);
return 1;
}
/* compare if numbers[i] positive */
if (numbers[i] > 0) {
posNumbers[n++] = numbers[i];
}
}
if (n > 1) { /* check if at least two numbers positive */
puts ("\nThere are atleast 2 pos numbers, which are :");
for (int i = 0; i < n; i++) {
printf ("%i\n", posNumbers[i]);
}
}
else {
puts ("\nMust have at least two positive numbers.");
}
}
(note: a good compiler will convert printf ("const string"); to fputs ("const string", stdout); for you, but choosing fputs() over the variadic printf() when there are no conversions involved indicates you understand how to choose the proper tool for the job on your own)
Example Use/Output
If less than two positive integers:
$ ./bin/threeposnumbers
First Number : -10
Second Number : 0
Third Number : 4
Must have at least two positive numbers.
If the two positive numbers are provided:
$ /bin/threeposnumbers
First Number : -10
Second Number : 2
Third Number : 4
There are atleast 2 pos numbers, which are :
2
4
If all positive numbers are provided:
$ ./bin/threeposnumbers
First Number : 10
Second Number : 2
Third Number : 4
There are atleast 2 pos numbers, which are :
10
2
4
(if you are old enough, you will recall the special significance of those numbers for a specific soft-drink marketing campaign, hint: initials DP :)
Handling non-integer input:
$ ./bin/threeposnumbers
First Number : -10
Second Number : two
error: invalid integer input.
Let me know if you have further questions.
footnotes:
1.) See:
Undefined, unspecified and implementation-defined behavior and
What is indeterminate behavior in C++ ? How is it different from undefined behavior? and
Undefined behavior

You say you want to store 3 numbers but you are declaring your array size to be 2. If you want to store 3 numbers this code:
int numbers[2]; // number list
int posNumbers[2]; // positive number list
Should be
int numbers[3]; // number list
int posNumbers[3]; // positive number list
You are supposed to declare the array sizes as how big as you want them but you can only use up to size - 1 since indexes start from 0.
Also you should probably fix the things that #WeatherVane is stating in the comments.

Addendum on the answers: you can interleave then in a struct to make it clearer which variables go together. This can also reduce the number of times you repeat yourself.
You have: a) a fixed number of numbers to enter, and b) you are going to copy all the positive numbers to a second, possibly smaller, array. The simplest is to make an equal-size array and explicitly have the size. I would refactor the code as such,
#include <stdio.h>
int main(void) {
struct {
const char *title;
int number;
} arr[] = { { "First", 0 }, { "Second", 0 }, { "Third", 0 } };
struct {
size_t size;
int number[sizeof arr / sizeof *arr];
} positives;
const int arr_size = sizeof arr / sizeof arr[0];
// enter numbers
for ( size_t i = 0 ; i < arr_size; i++) {
printf("%s Number : ", arr[i].title);
if(scanf("%i", &arr[i].number) != 1) return 1;
}
// adds positive in array
positives.size = 0;
for ( size_t i = 0 ; i < arr_size ; i++ ) {
if (arr[i].number > 0) {
// bounded by arr_size
positives.number[positives.size++] = arr[i].number;
}
}
if (positives.size >= 2) {
printf("There are atleast 2 pos numbers, which are : \n");
for ( size_t i = 0; i < positives.size ; i++) {
printf("%i\n", positives.number[i]);
}
} else {
printf("THere are not enough positive numbers.\n");
}
}

Related

How to check for duplicates in random numbers [duplicate]

I tried generating 10 unique random numbers in C. I have an array numout[] for 10 numbers but this gets to "segmentation fault" after some time.
Tho code is:
int i,j,numout[10],randnum;
void main()
{
srand(time(NULL));
for(i=0;i<10;i++)
{
numout[i]=generate();
printf("%d",numout[i]);
fflush(stdout);
sleep(1);
printf("\b");
}
}
int generate()
{
randnum=1+(int)(rand()*mul_val/(RAND_MAX+1.0));
for(j=0;j<i;j++)
{
if(randnum==0 || randnum==numout[j])
{
randnum=generate();
}
}
return(randnum);
}
Throw that code away, seriously. You need a shuffling algorithm, not a piece of code that checks older values for duplicates. Doing it your way will end up taking longer and longer as your pool runs out. The advantage of a shuffling algorithm is that it doesn't degrade as the pool becomes smaller.
Here's a piece of code I used in answering a different question. It maintains a list of numbers and, when it returns a random one to you, it removes it from the list and decrements the count for the next random selection.
#include <stdio.h>
#include <stdlib.h>
#define ERR_NO_NUM -1
#define ERR_NO_MEM -2
int myRandom (int size) {
int i, n;
static int numNums = 0;
static int *numArr = NULL;
// Initialize with a specific size.
if (size >= 0) {
if (numArr != NULL)
free (numArr);
if ((numArr = malloc (sizeof(int) * size)) == NULL)
return ERR_NO_MEM;
for (i = 0; i < size; i++)
numArr[i] = i;
numNums = size;
}
// Error if no numbers left in pool.
if (numNums == 0)
return ERR_NO_NUM;
// Get random number from pool and remove it (rnd in this
// case returns a number between 0 and numNums-1 inclusive).
n = rand() % numNums;
i = numArr[n];
numArr[n] = numArr[numNums-1];
numNums--;
if (numNums == 0) {
free (numArr);
numArr = 0;
}
return i;
}
int main (void) {
int i;
srand (time (NULL));
i = myRandom (20);
while (i >= 0) {
printf ("Number = %3d\n", i);
i = myRandom (-1);
}
printf ("Final = %3d\n", i);
return 0;
}
A sample output shows it in action:
Number = 19
Number = 10
Number = 2
Number = 15
Number = 0
Number = 6
Number = 1
Number = 3
Number = 17
Number = 14
Number = 12
Number = 18
Number = 4
Number = 9
Number = 7
Number = 8
Number = 16
Number = 5
Number = 11
Number = 13
Final = -1
Call it with a non-negative pool size and it sets up a new sequence and returns the first random value. Following that, you can call it with -1 and it will get the next random, unique number from the pool. When the pool is exhausted, it will return -1.
The other answer that contained this code has a version that can maintain multiple pools as well if you want to be able to use this function in threaded code.
You will get a segmentation fault when you run out of stack space. Your code is recursive (i.e. generate() calls generate()). So when you run out of unused random numbers, it will call itself forever.
However, I will not recommend a fix for your code, because you really need to write it again from scratch. Follow paxdiablo's example.
If you need a large set of unique random numbers you should consider using LFSR approach. LFSR generates unique random numbers that does not repeat unless the entire pool is exhausted, so a 32-bit LFSR will generate 2^32 - 1 unique random numbers -- It does not generate 0. The coding is straight forward, look it up in google.
The program below stores n unique random numbers i.e, from [1 to n] in an array.
#include<iostream.h>
#include<conio.h>
void main()
{
int i, j, Array[100];
cout<<"Enter value of n : "; //upper limit
cin>>n;
randomize();
int rnd;
Array[1]=rand()%n+1;
for(i=2;i<=n;i++)
{
rnd=rand()%n+1;
for(j=1;j<i;j++)
{
if(rnd==Array[j])
{
i--;
break;
}
}
if(j>=i)
Array[i]=rnd;
}
//for printing from random numbers from 1 to n
for(i=1;i<=n;i++)
cout<<Array[i]<<"\n";
getch();
}

How to make my code take an input of numbers on one line separated by spaces between, and output the avg of non-repeated numbers?

My code:
#include <stdio.h>
int main()
{
int count = 0;
int size = 0;
float num[size];
int i = 0;
float avg = 0;
float sum = 0;
while (scanf("%f",&num) != EOF)
{
if ((num[i] != num[i+1]) && (num[i] != num[i-1]))
{
sum = sum + num[i];
size++;
}
}
avg = sum/size;
printf("%0.2f", avg);
}
My input and output:
//input
2
2
1
^Z
//output
1.67
Correct input and output:
2 2 1
^Z
1.50
My question:
1) How can I make my code prompt input of numbers all on one line separated with spaces in between each input? Right now, my code always starts a new line after entering a number.
2) How can I fix my code so that it only calculates the average of non-repeated numbers? (NOTE: my code has to run no slower than O(nlogn). ) So I can't use nested loops as it will then have a run time of O(n^2).
You have some problems in your code:
the usage of num
test on scanf
the test of already used number.
the usage of num
When you write
int size = 0;
float num[size];
You do not allocate memory to store numbers.
and
while (scanf("%f",&num) != EOF)
Is not correct since you are not storing the value read into a float: gcc warns:
warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘float (*)[(sizetype)(size)]’ [-Wformat=]
A more correct way to do would be to write:
float num;
...
while (scanf("%f",&num) != EOF)
test on scanf
You test that scanf does not return EOF, but what if your conversion failed if you do not give a number?
The correct way to test what the user gives, is to check that you have the number of conversion wanted:
while (scanf("%f",&num) == 1)
the test of already used number.
Writting
if ((num[i] != num[i+1]) && (num[i] != num[i-1]))
You test last number (i) against past and future number (?!)
You have a simplier approch: have an array to store the already got number. This imply to have a function to test a number has already been got.
Warning The current implementation of is_number_in_array it very naive and make your program run in O(n). You can easyly replace it with some dichotomic search which is O(log n)
So a corrected version of your code could be:
#include <stdio.h>
int is_number_in_array(float num, float *array, int size)
{
for (int i =0; i < size; ++i)
{
if (num == array[i])
return 1;
}
return 0;
}
#define MAX_NUMBER 50
int main(void)
{
/* number of number read */
int size = 0;
/* number already read */
float array[MAX_NUMBER] = {};
/* currently read number */
float num = 0;
float avg = 0;
float sum = 0;
/* get next number, stop when a conversion failed */
while (scanf("%f",&num) == 1)
{
/* test if number is already in array */
if (! is_number_in_array(num, array, size )) {
/* not in array: add it */
array[size ++] = num;
sum += num;
}
/* Add some test here to check that size is not as big as MAX_NUMBER */
}
avg = sum/size;
printf("%0.2f", avg);
return 0;
}
First, read the numbers into an array nums. Let n be the number of numbers in the array. This can be done in O(N). I leave this to you.
Secondly, sort the array using a O(N log N) algorithm. I leave this to you.
Finally, we can identify duplicates by simply consulting neighbours. The tricky part is avoiding going out of bounds. Accessing nums[-1] or nums[n] would result in Undefined Behaviour, so we have to avoid that.
We don't need to look ahead and backwards. We want to use a number the first time we encounter it, so we only need to look backwards.
Don't forget to make sure we have at least one number, because we can't divide by zero.
if (!n) {
fprintf(stderr, "Can't find the average of zero numbers.\n");
exit(1);
}
float sum = 0;
size_t uniques = 0;
for (size_t i=0; i<n; ++i) {
if (i > 0 && nums[i] == nums[i-1])
continue;
sum += nums[i];
++uniques;
}
float avg = sum/uniques;
The complexity analysis is O(N + N log N + N) = O(N log N).
(Both of the other answers include O(N^2) solutions
As an aside, we can do better than O(N log N).
In practical terms, inserting into a well-written hash table has an amortized performance of O(1), and lookups are O(1). We can use this to devise an O(N) solution to the problem.
Using Perl, since it has such hash tables built-in:
#nums
or die("Can't find the average of zero numbers.\n");
my %seen; # Hash table.
my $sum = 0;
my $uniques = 0;
for my $num (#nums) {
next if $seen{$num}++;
$sum += $num;
++$uniques;
}
my $avg = $sum/$uniques;
This approach saves all integers in an array, as long they are not saved already, as to avoid duplicates.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int bytes_read;
int size = 1000;
int i = 0;
int amount = 0;
int number = 0;
// boolean flag to detect duplicate numbers.
int duplicate = 0;
int sum = 0;
float unique_avg = 0;
// Allocate char / int arrays on the heap.
// These can be resized with realloc() if needed.
char* string = (char *) malloc (size);
int* unique_num = (int *) calloc (2 * size, 0);
// Declare char pointers to be used will splitting the string.
char* token;
char* rest = string;
printf ("Please enter a string: ");
bytes_read = getline (&string, &size, stdin);
// In case getline fails to get the input.
if (bytes_read == -1) {
puts ("error!");
}
// getline() read input succesfully.
else {
// Iterate over all space separated string tokens.
while ((token = strtok_r(rest, " ", &rest))){
// Convert string token to number
number = atoi(token);
for(i = 0; i < 2 * size; ++i){
if(number == unique_num[i]){
// Duplicate found.
duplicate = 1;
break;
}
}
if(!duplicate){
unique_num[amount] = number;
++amount;
}
// Restore value of duplicate for next iteration
duplicate = 0;
}
}
// Sum all unique numbers.
for(i = 0; i < amount; ++i){
sum += unique_num[i];
}
// Calculate the avg of unique numbers.
// Float casting is required for the fractional part of the division.
unique_avg = (float) sum / (float) (amount);
// Print the average.
printf("%f", unique_avg);
return 0;
}
Running this code in console yields with example input of 2 2 1:
Please enter a string: 2 2 1
1.500000

C - How to read input separated by commans and whitespaces

I have a question on how to read input and assigning it to an array (or even two arrays).
I have a project where I have to:
Create a C console application to compile the following statistics on a list of real number pairs:
• minimum value;
• maximum value;
• median value;
• arithmetic mean;
• mean absolute deviation – (mean, median, mode)
• variance (of a discrete random variable);
• standard deviation (of a finite population);
• mode (including multi-modal lists).
• least squares regression line
• outliers
Your program must handle any length of list. The list will be input (or piped) from the console, or read from a file. The list is terminated with end-of-stream (^Z) or non-numeric input.
So basically, the program has to read:
1,2
2,23
3,45
5,34
or: 1,2 3,4 5,6 7,8
and be able to calculate their statistic properties.
I know how to do the calculations and create the functions etc. My question is, how to implement the parts: "any length of the list" and "list of real number pairs". See sample below.
sample output
What I tried so far:
#include <stdio.h>
int main()
{
int a[100];
int b[100];
int n = 100;
for (int i = 0; i < n; i++) {
scanf_s("%d,", &a[i]);
}
for (int i = 0; i < n; i++) {
printf(" %d", a[i]);
}
return 0;
}
which returns a the result, but only to a fixed array length and gives me a bunch of -858993460 -858993460.
For now, I just want to know how to read the input properly and assign them to one Array, so I can read the odd and even index and calculate their mean and whatever respectively...
or assign them to two different arrays (x[], y[]), x for the digit on the left of the comma, y on the right.
Hi you can get the entire input in a single line in a character array and then loop on the array to correctly split the char array to convert it into integer array.
Below is the code you can try :
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
char *src = (char*)malloc(sizeof(char) * 500);
fgets(src, 500, stdin);
int arr[500];
int index = 0;
int n;
while ( sscanf ( src, "%d%n", &arr[index], &n ) == 1 ) {
//while ( sscanf ( src, "%d,%n", &arr[index], &n ) == 1 ) { ////Use this for values separated by commas
printf("%d\n", arr[index]);
index++;
src += n;
}
return 0;
}

How do I record the frequency of numbers inputted by a user using functions in C

Hey there i'm currently developing a lotto type game and one of my requirements is to record the frequency of the numbers inputted by the user and then display them if the users wishes to see them. The program also must be modular hence the functions.
My problem is that i can't seem to figure out how to keep track of the numbers I tried numerous things and this is the closest I've gotten...
void num_free(int *picked_nums)
{
static int elements[MAX] = { 0 };
int i;
for (i = 0; i < MAX; i++)
if (*(picked_nums + i) == i)
{
elements[i]++;
}
for (i = 0; i < MAX; i++)
{
if (elements[i] != 0)
{
printf("\nThe amount of times you chose %d is %d", i, elements[i]);
}
}
printf("\nEnter any key to return to main menu");
getchar();
}
The output of this every time i run it no matter the input is
"The amount of times you chose 11 is 1"
I'm really clueless as to what to do next so any and all help would be appreciated. Thanks in advance!
EDIT: The user can play multiple rounds and thats how the frequency of the numbers can add up.
I think the main problem in your code is here:
if (*(picked_nums + i) == i)
{
elements[i]++;
}
you actually check if the i-th number the user chose equals to i. That means that increment is done only in that case - which is not what you want (if I got you right).
I think you should give up the if statement, and, assuming that the user chooses only non-negative numbers (and that the elements array is properly zeroed at the beginning), do this:
elements[picked_nums[i]]++;
Namely, you increment the array cell matching the chosen number (and the i is only the index you use to iterate the picked_num array).
The problem is how you count and store the numbers:
if (*(picked_nums + i) == i)
{
elements[i]++;
}
Your i is moving and at the same time the element chosen from picked_nums is moving. This loop will not count or store properly.
The provided solution assumes that picked numbers are stored in the numbers array. I assumed that numbers are in 1 to 64 range. You can adjust program to your needs. Test provided:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void num_free(int picked_nums[], int size )
{
static int elements[65] = { 0 }; // numbers can be from 1 to 64 range
int i;
for (int j = 0; j < size; j++)
{
int n = picked_nums[j];
for (i = 1; i < 65; i++) // numbers can be from 1 to 64 range
{
if ( n == i)
{
elements[i] = elements[i]+1;
}
}
}
for (i = 0; i < 65; i++)
{
if (elements[i] != 0)
{
printf("\nThe amount of times you chose %d is %d", i, elements[i]);
}
}
// printf("\nEnter any key to return to main menu");
// getchar();
}
// array of entered numbers:
int numbers[] = { 2, 2, 2, 40, 7, 7, 8, 9, 40 };
int main(void) {
num_free(numbers, 9); // call with sizeof numbers
return 0;
}
Test:
The amount of times you chose 2 is 3
The amount of times you chose 7 is 2
The amount of times you chose 8 is 1
The amount of times you chose 9 is 1
The amount of times you chose 40 is 2

Finding numbers with unique digits in C

I have to write a program that finds every number (except 0) which can be factored by numbers from 2-9.
For example first such a number would be number 2520 as it can be divided by every single number from 2 to 9.
It also has to be a number that contains only 1 type of digit of its own (no multiple digits in a number). So for example 2520 will not meet this requirement since there are two same digits (2). The example of a number that meets both requirements is number 7560. That is the point I don't how to do it. I was thinking about converting value in an array to string, and then putting this string in another array so every digit would be represented by one array entry.
#include <stdio.h>
#include <math.h>
int main() {
int i, n, x, flag, y = 0;
scanf("%d", &n);
double z = pow(10, n) - 1;
int array[(int)z];
for (i = 0; i <= z; i++) {
flag = 0;
array[i] = i;
if (i > 0) {
for (x = 2; x <= 9; x++) {
if (array[i] % x != 0) {
flag = 1;
}
}
if (flag == 0) {
y = 1;
printf("%d\n", array[i]);
}
}
}
if (y == 0) {
printf("not exist");
}
return 0;
}
This should give you a base:
#include <stdio.h>
#include <string.h>
int main()
{
char snumber[20];
int number = 11235;
printf("Number = %d\n\n", number);
sprintf(snumber, "%d", number);
int histogram[10] = { 0 };
int len = strlen(snumber);
for (int i = 0; i < len; i++)
{
histogram[snumber[i] - '0']++;
}
for (int i = 0; i < 10; i++)
{
if (histogram[i] != 0)
printf("%d occurs %d times\n", i, histogram[i]);
}
}
Output:
Number = 11235
1 occurs 2 times
2 occurs 1 times
3 occurs 1 times
5 occurs 1 times
That code is a mess. Let's bin it.
Theorem: Any number that divides all numbers in the range 2 to 9 is a
multiple of 2520.
Therefore your algorithm takes the form
for (long i = 2520; i <= 9876543210 /*Beyond this there must be a duplicate*/; i += 2520){
// ToDo - reject if `i` contains one or more of the same digit.
}
For the ToDo part, see How to write a code to detect duplicate digits of any given number in C++?. Granted, it's C++, but the accepted answer ports verbatim.
If i understand correctly, your problem is that you need to identify whether a number is consisted of multiple digits.
Following your proposed approach, to convert the number into a string and use an array to represent digits, i can suggest the following solution for a function that implements it. The main function is used to test the has_repeated_digits function. It just shows a way to do it.
You can alter it and use it in your code.
#include <stdio.h>
#define MAX_DIGITS_IN_NUM 20
//returns 1 when there are repeated digits, 0 otherwise
int has_repeated_digits(int num){
// in array, array[0] represents how many times the '0' is found
// array[1], how many times '1' is found etc...
int array[10] = {0,0,0,0,0,0,0,0,0,0};
char num_string[MAX_DIGITS_IN_NUM];
//converts the number to string and stores it in num_string
sprintf(num_string, "%d", num);
int i = 0;
while (num_string[i] != '\0'){
//if a digit is found more than one time, return 1.
if (++array[num_string[i] - '0'] >= 2){
return 1; //found repeated digit
}
i++;
}
return 0; //no repeated digits found
}
// test tha function
int main()
{
int x=0;
while (scanf("%d", &x) != EOF){
if (has_repeated_digits(x))
printf("repeated digits found!\n");
else
printf("no repeated digits\n");
}
return 0;
}
You can simplify your problem from these remarks:
the least common multiple of 2, 3, 4, 5, 6, 7, 8 and 9 is 2520.
numbers larger than 9876543210 must have at least twice the same digit in their base 10 representation.
checking for duplicate digits can be done by counting the remainders of successive divisions by 10.
A simple approach is therefore to enumerate multiples of 2520 up to 9876543210 and select the numbers that have no duplicate digits.
Type unsigned long long is guaranteed to be large enough to represent all values to enumerate, but neither int nor long are.
Here is the code:
#include <stdio.h>
int main(void) {
unsigned long long i, n;
for (n = 2520; n <= 9876543210; n += 2520) {
int digits[10] = { 0 };
for (i = n; i != 0; i /= 10) {
if (digits[i % 10]++)
break;
}
if (i == 0)
printf("%llu\n", n);
}
return 0;
}
This program produces 13818 numbers in 0.076 seconds. The first one is 7560 and the last one is 9876351240.
The number 0 technically does match your constraints: it is evenly divisible by all non zero integers and it has no duplicate digits. But you excluded it explicitly.

Resources