Print number of recursions in C - c

So I have an exercise that goes like this:
Code a program that user inputs 2 natural numbers X and Y. The output must print the number of combinations that are recursively computed to generate binary numbers using X 1s and Y 0s.
For example, user inputs 2 and 3. The combinations of 2 and 3 generating binary numbers are:
00011
00101
00110
01001
01010
01100
10001
10010
10100
10. 11000
The program must print "10".
So I've coded the recursion but I cant figure out a way to print the number "10".
Here is my code
#include <stdio.h>
#include <stdlib.h>
int recursion(int a, int z)
{
if(a==0 && z==0)
{
printf(".");
return 1;
}
if(a!=0 && z==0)
return recursion(a-1,z);
if(a==0 && z!=0)
return recursion(a,z-1);
if(a!=0 && z!=0)
return recursion(a-1,z)+recursion(a,z-1);
}
int main()
{
int a,z;
scanf("%d %d", &a, &z);
recursion(a,z);
return 0;
}
This code only prints 10 "." instead of the number of dots that I need. Any thoughts?

#include <stdio.h>
#include <stdlib.h>
int recursion(int a, int z)
{ int d;
if(a==0 && z==0)
{
d++;
return 1;
}
if(a!=0 && z==0)
return recursion(a-1,z);
if(a==0 && z!=0)
return recursion(a,z-1);
if(a!=0 && z!=0)
return recursion(a-1,z)+recursion(a,z-1);
}
int main()
{
int a,z,d;
scanf("%d %d", &a, &z);
d=recursion(a,z);
printf("%d",d);
return 0;
}

Your program already returns its level of recursion, a simple way to print it could be:
int main()
{
int a, z, level, read;
do {
read = scanf("%d %d", &a, &z);
if (read != 2) {
printf("Incorrect input, please try again");
}
} while (read != 2);
level = recursion(a,z);
printf("\n%d\n", level); /* Newline characters added to improve readability */
return 0;
}
So the output would be the dots you are printing on line 9 and the level of recursion (10 in your example).
..........
10

Related

Stuck on asking for input (C program)

so, I ran into this problem when i was trying to make a program that checks if the entered number is an Armstrong number and it seems easy and ok, but when I tried to execute the following code the output is not shown and i'm just just stuck with the program asking for input even though I already entered it.
#include <stdio.h>
#include <math.h>
int main(void)
{
int i, a, b, c, d, e, n, remainder, result;
printf("Enter a positive number: ");
scanf("%d", &a);
b = a;
e = a;
while (a != 0)
{
a = a % 10;
n++;
}
while (b != 0)
{
remainder = b % 10;
result += pow(remainder, n);
b /= 10;
}
if (result == e)
{
printf("%d is an Armstrong number.", e);
}
else
{
printf("%d isn't an Armstrong number.", e);
}
}
The program is supposed to return "number" is an Armstrong number, if the user entered an Armstrong number, and "number" isn't an Armstrong number if the user didn't enter an Armstrong number,
Your first loop will never exit if you enter a number where the last digit is anything but zero (0). I think you meant for it to be:
while (a != 0)
{
a /= 10;
n++;
}
Also, scanf is hardier if you allow (and discard) whitespace, as in int cnt = scanf(" %d ", &a); You should also check the return value from scanf to make sure a value was actually read.

C Program to find maximum and minimum between two numbers using functions

I was solving a code of maximum and minimum using functions. I wrote the code like this:
#include <stdio.h>
int maxmin(int x, int y);
int main () {
int a, b, c;
scanf("%d%d", &a, &b);
c = maxmin(a, b);
if (maxmin == 1) {
printf("%d is maximum,%d is minimum", a, b);
}
else
printf("%d is maximum,%d is minimum", b, a);
return 0;
}
int maxmin(int x, int y) {
int XisMax = 0;
if (x > y) {
XisMax=1;
}
else {
XisMax=0;
}
return XisMax;
}
So my output shows this results:
Input:9,10;
10 is maximum,9 is minimum
Input:10,9;
9 is maximum,10 is minimum
What is the mistake here? What should I do?
PS:I have an exam on functions so solutions using functions will be helpful.
if (maxmin==1)
change toif (c==1)
your problem is solve.
Have a Good day
You should be checking if(c == 1), not if(maxmin == 1).
Your function can also be made shorter:
int maxmin(int x, int y)
{
if (x>y)
{
return 1;
}
else
{
return 0;
}
}
Also, I think your scanf is missing a comma between the two %d's.

What is a recursive function for this program I coded for adding all the digits of a five digit number

I'm not able to code a recursive function for this, I have given the example after the program,
#include<stdio.h>
int sum(int x);
int main()
{
int n,s;
printf("enter the five digit number whose digits need to be added");
scanf("%d",&n);
s= sum(n);
printf("The sum of all the digits of a five digit number is %d",s);
}
int sum(int x)
{
int d=0,a;
for(i=1;i>=5;i++)
{
a=x%10;
x=x/10;
d= d+a;
}
return(d);
}
The following is the recursive code for the above program that I coded myself,
#include<stdio.h>
int sum(int x);
int main()
{
int n,s;
printf("enter the five digit number whose digits need to be added\n");
scanf("%d",&n);
s= sum(n);
printf("The sum of all the digits of a five digit number %d",s);
}//This is my poor try inspired by coderedoc
//please fix this code my laptop battery gonna be over already
int sum(int x)
{
int d=0, a;
if(x<0)
return sum(-x);
else
a= x%10;
x= x/10;
d=d+a;
return sum(x);
else
if(x==0)
return d;
}
int sum(int x){
if( x < 0) return sum(-x);
return (x==0)?0:(x%10)+sum(x/10);
}
The code will be as simple as this. If you reach the x=0 state you are done. Else add the last digit with the sum of the rest of the digits.
Also when building your solution - try to make it generalize to some extent. 5 digit is good but think if you can extend it for more numbers of digits. This works for that too.
int sum(int x){
if(x < 0) return sum(-x);
if(x == 0)
return 0;
else
return (x%10)+sum(x/10);
}

C program on part of algorithm which focus on finding Armstrong-like number

I have a problem with C program. The idea of it is similar to Armstrong number checking. Say if the input number is 123. Program needs to check if condition, for example 123=1^1+2^2+3^3 is true. I know how to add digits,but have a problem with powers. It is obvious that I need a loop for powers from 1 to the number of digits. In Armstrong number algorithm you have similar power on every digit. For example 153=1^3+5^3+3^3. Here is what I have so far:
#include<stdio.h>
int main()
{
int n,d,s=0,o,i,k;
printf("n=");scanf("%d",&n);
d=n;
while(d!=0)
{
o=d%10;
s=s+o;
d=d/10;
k++
}
printf("sum:%d",s);
printf("number of digits:%d",k);
return 0;
}
Thanks for the answers.
You need first get the lenth of number, which is used to determine how many times you need to get into loop to calculate each bit.
For example, number 123, you first need to know the number is 3 bits len, then you can mutilply number 3 three times, number 2 twice, and number 1 once.
I use a temporary string to achieve this
here is codeļ¼Œ a little bit alteration on yours
#include <stdio.h>
#include <string.h>
#define MAX_NUM_LEN 16
int main()
{
char tmp_num[MAX_NUM_LEN] = {0};
int len,n,d,s=0,o,i,tmp_len, tmp_o;
printf("n=");scanf("%d",&n);
sprintf(tmp_num, "%d", n);
len = strlen(tmp_num);
tmp_len = len;
d=n;
while(d!=0)
{
o=d%10;
for (tmp_o = 1, i = tmp_len; i > 0; i--)
tmp_o *= o;
s=s+tmp_o;
d=d/10;
tmp_len--;
}
printf("sum:%d\n",s);
printf("number of digits:%d\n",len);
return 0;
}
results:
According of what I've understood I think this is what the OP is looking for:
int power(int base, int exp)
{
if (base == 0) return 0;
int result=1;
while (exp-- > 0) result*=base;
return result;
}
void calculate(int number)
{
int d=number;
int tmpnumber=number;
int n=0;
while (d > 0)
{
n++;
d /=10;
}
printf("Number of digits: %d\n", n);
int k=0;
int sum=0;
while (n--)
{
// get digits from left to right
d=number / power(10, n);
k++;
sum+=power(d, k);
number %= power(10, n);
printf("%d^%d=%d\n", d, k, power(d, k));
}
printf("\n%5d %5d", tmpnumber, sum);
}
int main(int argc,char *argv[])
{
int value;
while (TRUE)
{
printf("Enter value (0 = Quit): ");
scanf("%d", &value);
if (value <= 0) return 0;
calculate(value);
printf("\n");
}
}

How to create C program to determine lagest number by entered value?

I've created a program to determine largest number, but my lecturer says it isn't perfect, can anybody make it perfect?
#include <stdio.h>
int main () {
double a,b=0,n, i;
printf("limit of n input: ");
scanf ("%lf",&n);
for (i=1;i<=n;i++) {
scanf("%lf",&a);
if (a>b) b=a;
}
printf("%.2lf", b);
return 0;
}
If by "not perfect" she meant "doesn't properly handle negative numbers or an empty set", then you'd want to
Treat n<1 as a special case (why should 0 be the largest of an empty set?)
Read the first number outside of the loop, so you're not making as assumption as to the smallest possible number
I would do it that way, sorry for the mass of text. I think it is coming from the typical Objective-C style programming with long words:
#include <stdio.h>
int clean_stdin() {
while (getchar()!='\n');
return 1;
}
int main(int argc, char *argv[]) {
char c;
signed int count = 0; // number of numbers to scan
unsigned int fireErrorMessage = 0;
do {
if (fireErrorMessage == 1) {
printf("You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012\n"); // output for the user
}
if (fireErrorMessage == 0) {
fireErrorMessage = 1;
}
printf("How many integers do you want to insert (Inser a number >0)? ");
} while (((scanf("%d%c", &count, &c) != 2 || c != '\n') && clean_stdin()) || count < 1);
signed int indexOfNumber; // for index, declared outside because of output at the end
signed int highestNumberIndex;
double highestNumber; // saving the highest value in a helper variable
fireErrorMessage = 0;
for (indexOfNumber = 1; indexOfNumber <= count; indexOfNumber++) {
double scannedNumber;
do {
if (fireErrorMessage == 1) {
printf("You entered not a number. Please enter a number. Examples: 3.0 -1 14\n"); // output for the user
}
if (fireErrorMessage == 0) {
fireErrorMessage = 1;
}
printf("Input number %d: ", indexOfNumber); // output for the user
} while (((scanf("%lf%c", &scannedNumber, &c) != 2 || c != '\n') && clean_stdin()));
fireErrorMessage = 0;
if (indexOfNumber == 1 || scannedNumber > highestNumber) {
highestNumberIndex = indexOfNumber;
highestNumber = scannedNumber;
}
}
printf("Highest input number on index %d, the value is about %.2lf\n", highestNumberIndex, highestNumber);
return 0;
}
Output
How many integers do you want to insert (Inser a number >0)? aa5
You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012
How many integers do you want to insert (Inser a number >0)? -3
You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012
How many integers do you want to insert (Inser a number >0)? 3
Input number 1: aa
You entered not a number. Please enter a number. Examples: 3.0 -1 14
Input number 1: -50.0001
Input number 2: 51a
You entered not a number. Please enter a number. Examples: 3.0 -1 14
Input number 2: -1.00
Input number 3: -0.1
Highest input number on index 3, the value is about -0.10
This code caters for negative, not a number input for the loop index as well as negative and not a number inputs inside the loop. Thanks
#include <stdio.h>
#include <math.h>
int main () {
int n, i;
double a,b=0;
printf("limit of n input: ");
scanf ("%lf",&n);
if(n < 0){
printf("value of n cannot be negative");
return 0;
}
else if (n == 0)
return 0;
else if (isnan(n))
return 0;
else{
for (i=1;i<=n;i++)
{
scanf("%lf",&a);
if(!isnan(a) && a > 0)
{
if (a>b) b=a;
}
}
printf("%.2lf", b);
return 0;
}
}

Resources