Skip to main content
added 444 characters in body; added 49 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

Assuming you don't want to add them and store the result in a new variable. To print them concatenated to each other like 15, you may write something like this.

printf("%d%d\n", x, y);

To add them and store the result in a new variable, you may multiply x by 10 first, then add them and store the result in a new int variable

int sum = (x * 10) + y;
printf("%d\n", sum);

Update: in case you have an array of 4 ints and you want to add them in such a way as the first, the second, the third, the fourth elements are positioned in the ones, tens, hundreds, thousands places respectively, you may write something like this

int arr[] = {2, 0, 1, 4};
int position = 1;
int year = 0;

for (int i = 3; i >= 0; i--)
{
    year += arr[i] * position;
    position *= 10;
}

After the loop ends execution, year should be 2014.

Assuming you don't want to add them and store the result in a new variable. To print them concatenated to each other like 15, you may write something like this.

printf("%d%d\n", x, y);

To add them and store the result in a new variable, you may multiply x by 10 first, then add them and store the result in a new int variable

int sum = (x * 10) + y;
printf("%d\n", sum);

Assuming you don't want to add them and store the result in a new variable. To print them concatenated to each other like 15, you may write something like this.

printf("%d%d\n", x, y);

To add them and store the result in a new variable, you may multiply x by 10 first, then add them and store the result in a new int variable

int sum = (x * 10) + y;
printf("%d\n", sum);

Update: in case you have an array of 4 ints and you want to add them in such a way as the first, the second, the third, the fourth elements are positioned in the ones, tens, hundreds, thousands places respectively, you may write something like this

int arr[] = {2, 0, 1, 4};
int position = 1;
int year = 0;

for (int i = 3; i >= 0; i--)
{
    year += arr[i] * position;
    position *= 10;
}

After the loop ends execution, year should be 2014.

added 80 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

I assumeAssuming you don't wannawant to add them and store themthe result in a new variable. To print them out as concatenated to each other like 15, you may write something like this.

printf("%d%d\n", x, y);

To add them and store the result in a new variable, you may multiply x by 10 first, then add them and store the result in a new int variable

int sum = (x * 10) + y;
printf("%d\n", sum);

I assume you don't wanna add them and store them in a new variable. To print them out as 15, you may write something like this.

printf("%d%d\n", x, y);

To add them and store the result in a new variable, you may multiply x by 10 first, then add them

int sum = (x * 10) + y;
printf("%d\n", sum);

Assuming you don't want to add them and store the result in a new variable. To print them concatenated to each other like 15, you may write something like this.

printf("%d%d\n", x, y);

To add them and store the result in a new variable, you may multiply x by 10 first, then add them and store the result in a new int variable

int sum = (x * 10) + y;
printf("%d\n", sum);
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

I assume you don't wanna add them and store them in a new variable. To print them out as 15, you may write something like this.

printf("%d%d\n", x, y);

To add them and store the result in a new variable, you may multiply x by 10 first, then add them

int sum = (x * 10) + y;
printf("%d\n", sum);