Recursion and printing in C

My professor asked me to do this:

Input Number: 5 +++++ +++++ +++++ +++++ +++++ 

I tried so hard to get it; I continued to end with a “+” with a huge space and a “+”.

Could you help me fix this code in C?

 #include <stdio.h> #include <conio.h> int space(int space1) { if(space1 == 0) { return 1; } else { return printf("\n") && space(space1 - 1); } } int visual(int plus) { if (plus == 0) { return 1; } else { return printf("+") && visual (plus - 1) && space(plus - 1); } } int main() { int number; printf("Please give the number\n"); scanf("%d",&number); visual(number); getch(); } 

New editing; disappointment for me. This gives me 5 lines + and a lot of space.

+4
source share
6 answers

Complete solution Updated without a loop:

 #include <stdio.h> #include <conio.h> int space(int space1) { if(space1 == 0) { return 1; } else { return printf("+") && space(space1 - 1); } } int visual(int plus,int add) { if (plus == 0) { return 1; } else { space(add); printf("\n"); visual (plus - 1,add); } } int main() { int number,i; printf("Please give the number\n"); scanf("%d",&number); visual(number,number); getch(); } 
0
source

Inside the recursion function in the if section (plus == 0) you must type the \ n character and then use the loop to call the visual (number) number of times.

If you really want to do this only with recursions, you will also need another recursive function. One recursion function will call another, one will print "+++++ \ n", and the other will call this function x times to create x number of lines.

  function printpluses (int x){ if (x==0){ printf ("\n"); return; } else { printf ("+"); printpluses (x-1); } } 

and another function will be

  function printline (int x, int no_of_pluses){ if (x==0){ printf ("\n"); return; } else{ printpluses(no_of_pluses); printline (x-1, no_of_pluses); } } 

you can make no_of_pluses global so you don't go through it in every call.

+3
source

check this out for a solution ::

 #include <stdio.h> #include <conio.h> int visual(int plus) { for(i=0;i<plus;i++) { for(j=0;j<plus;j++) { printf("+"); } printf("\n"); } int main() { int number; printf("Please give the number\n"); scanf("%d",&number); visual(number); getch(); } 
+2
source
 #include <stdio.h> #include <conio.h> int number; int visual(int plus) { if(plus % number == 0 && plus!=number*number) printf("\n"); if (plus == 0) { return 1; } else { printf("+"); visual (plus - 1); } } int main() { printf("Please give the number\n"); scanf("%d",&number); visual(number*number); getch(); } 
+2
source
 #include <stdio.h> #include <conio.h> int number; int visual(int plus) { int i; if (plus == 0) { return 1; } else { for(i=1;i<=number;i++) printf("+"); printf("\n"); visual (plus - 1); } } int main() { printf("Please give the number\n"); scanf("%d",&number); visual(number); getch(); } 
0
source
 #include <stdio.h> #include <conio.h> int number; int visual(int plus) { int i; if (plus == 0) { return 1; } else { for(i=1;i<=number;i++) printf("+"); printf("\n"); visual (plus - 1); } } int main() { printf("Please give the number\n"); scanf("%d",&number); visual(number); getch(); } 

This will help.

0
source

Source: https://habr.com/ru/post/1391211/


All Articles