I need to make a program in C that reads two positive integers from user,
numbers N
and S
. The program should display all unique number
combinations of N
single digits whose sum equals S
using recursion.
Eg.
for N = 3
and S = 10
the program should display:
8+1+1=10
7+2+1=10
6+3+1=10
5+4+1=10
6+2+2=10
5+3+2=10
4+4+2=10
4+3+3=10
If a number is out of the scope it will display
"Wrong input
".
This is the code i wrote so far:
void check(int n,int s)
{
int arr[n];
for (int i=1;i<=n;i++)
{
arr[i]=1;
}
if (n==s)
{
int j=1;
while (j<n)
{
printf("%d+",arr[j]);
j++;
if (j==n)
{
printf("%d=%d",arr[j],s);
break;
}
}
}
else if (n<s)
{
printf("Wrong input");
exit(0);
}
else
{
}
}
int main()
{
int N,S;
scanf("%d %d",&N,&S);
if (N<=0 || S<=0)
{
printf("Wrong input");
exit(0);
}
check(N,S);
}
I cant figure out how to solve this problem and i must use recursion for this project.Can anyone help me?