# Thread: joining two pieces of code

1. ## joining two pieces of code

hi there I'm new to programing so sorry about the questions as I no there basic steps. but I just cant figure it out. I have wrote two pieces of codes now I wish to join them up, at the moment I have one code that will multiply the number I enter by 9 and give me the answer. and I have another code that will add up all the numbers I enter apart from the last one. what I wish to do is join the two codes. so when I enter a number it will multiply it by 9, then adds up all the numbers in the answer apart from the last one and gives me the answer. can anyone show me how to do this as iv spent hours and hours trying it but cant get it to work. hear are the two codes I have
Code:
```#include <stdio.h>
#include <iostream>
using namespace std;
void calc(int);
int main()
{
int num;
printf("enter 3 diget number above 0:\n");
scanf("%i", &num);
calc (num);
return 0;
}
void calc (int n)
{
n=n*9;
printf("the enterd number multiplyed by 9 id %i\n",n);
return;
}

void calc (long)
{
return;
}```
and the second one
Code:
```#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int sum = 0;
printf(" Enter a number above 0 : ");
scanf("%d", &num);
num /= 10;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
printf("Sum = %d", sum);
getch();
return 0;
}```
if anyone can help me it would be great, thanks in advance 2. Join how?

Do you want a main() where you print two choices like
- multiply by 9
- sum digits

and you call the appropriate function?

Well the first thing to do is to make both sets of code a function.
Eg, your 2nd program could be
Code:
```#include <stdio.h>
#include <conio.h>
void sumDigits() {
int num;
int sum = 0;
printf(" Enter a number above 0 : ");
scanf("%d", &num);
num /= 10;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
printf("Sum = %d", sum);
}

int main()
{
sumDigits();
getch();
return 0;
}```
Now, copying the function sumDigits() to another program source becomes that much easier.

FYI, the first program is C++ and the second program is archaic C.
Be clear on what it is you're trying to learn. Popular pages Recent additions #include, enter, int, num, number 