C recursion practice won't display output [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This is my attempt at recursion. It compiles and runs, but doesn't display the factorial of the number that I input. I'm attempting this with Geany on Ubuntu.
#include <stdio.h>
int fact(int n);
int main() {
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
int fact(int n) {
if (n <= 1)
return 1;
else
return n * fact(n - 1);
}

You are missing the print statement.
You could save the result in a variable and then print it.
printf("%d",fact(n));

Your code is fine, but you forget to to print the return value of function fact() change this portion of your code
scanf("%6d", &n);
fact(n);
to this:
scanf("%6d", &n);
printf("%d", fact(n));
Your work will done. after replacing your main() function will look like:
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
printf("%d", fact(n));
}
Note: The factorial of 17 or higher is not adjust in integer limit.

doesn't display the factorial of the number that I input.
in
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
you do not print the result of factorial, then it is not print
why are you using "%6d" rather than "%d" in the scanf ? you do not print so you do not need that
I also encourage you
to add a separator after Give me a number else the input number will seem attached to it (I used a ':' below)
to test the result of scanf
So, for instance :
int main()
{
int n;
printf("Give me a number:");
if (scanf("%d", &n) == 1)
printf("fact(%d)=%d\n", n, fact(n));
}

Related

integer print wrong number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator(){
int k;
printf("Masukkan batas bilangan genap = ");
scanf("%i", &k);
printf("Bilangan genap dari 0 sampai %i adalah :\n");
for (int i=0; i<=k;i+=2){
printf("%i\n", i);
}
}
int main(){
genap_generator();
system("pause");
}
I made a program to generate odd numbers with int k as the max number but when i print the integer it doesnt print kprint error
in the line where you want to print k: printf("Bilangan genap dari 0 sampai %i adalah :\n");, you didnt pass k.
The line should be: printf("Bilangan genap dari 0 sampai %i adalah :\n", k);.
What the function does print right now is what is placed on the stack where k should have been.
Also, you are printing all the even numbers, if you want to print the odd numbers start from i=1.
Welcome to our community! Could you use English language in your code next time?
The problem was with printing in printf, you did not declare which variable you wanted to print. Here is the working code:
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator()
{
int k;
printf("Enter an even number limit = ");
scanf("%i", &k);
// Had to declare, which variable to print
printf("The even numbers from 0 to %i are:\n", k);
for (int i = 0; i <= k; i += 2)
{
printf("%i\n", i);
}
}
int main()
{
genap_generator();
system("pause");
}

Counting the amount of digits in a while loop does not work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I use the following code to count the amount of digits in a while loop, so "0" should be 1, "10" should be 2 etc. - however the code does not seem to work. Can you please help me?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
division=x/10;
counter++;
}
while(division!=0);
printf("This number contains : %d digits",counter);
return 0;
}
please change division=x/10; to x /= 10 and corresponding while condition. x is not changed your code, thus you get stucked in you while loop forever
This line:
division = x / 10;
Will be performed forever since the condition given in the while logic never becomes false.
If you do:
do {
x = x / 10;
counter++;
} while (x != 0);
It'll work.
Enhanced version of your code:
#include <stdio.h>
int main() {
int x;
int counter = 0;
printf("Enter a number : ");
// looping until a correct format is provided
while (scanf("%d", &x) == 0) {
printf("Incorrect values, enter again: ");
fseek(stdin, 0, SEEK_END);
}
do {
x = x / 10;
counter++;
} while (x != 0);
printf("This number contains : %d digits.", counter);
return 0;
}
The intention behind the "enhanced version" is to verify if the input is correctly given as formatted in coding (i.e. accepting an integer and nothing else) which isn't in your program.
Also, you don't need to include stdlib.h for your own code. That works without it too.
You'll then get the following sample output:
Enter a number : asdlfjal;sdk
Incorrect values, enter again: asdf sdf
Incorrect values, enter again: 33334
This number contains : 5 digits.
You are not changing the division value. This should work
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
x=x/10;
counter++;
}
while(x!=0);
printf("This number contains : %d digits",counter);
return 0;
}

C - What is wrong with this code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I made a program which takes a user to input number, if user inputed number less than 0 or greater than 100, then program returns to main function.
Here is my code:
#include <stdio.h>
int main(){
int a; scanf("%d", &a);
if(a > 100 || a < 0) {
printf("Going back to program.");
return main();
} else {
printf("Your number: %d\n", a);
}
}
But this code doesn't take a input and it just prints "Your number: 0".
So, what is wrong with this code?
Using recursion here (calling main again from main) is overly complicated and useless.
You want this:
#include <stdio.h>
int main()
{
int a;
do
{
scanf("%d", &a);
} while (a > 100 || a < 0); // simply repeat scan if a is out of bounds
printf("Your number: %d\n", a);
}
This is basic C knowledge. You probbaly should start reading your C textbook.
Your code is not good... A better way of doing it would be to use a loop
Better Solution
int main(){
int a;
do {
scanf("%d", &a);
} while(a > 100 || a < 0);
printf("Your number: %d\n", a);
return 0;
}
Your working solution
int main(){
int a;
scanf("%d", &a);
if(a > 100 || a < 0) {
printf("Recursion");//You are not going back you go further in depth (Recursion)
return main();
} else {
printf("Your number: %d\n", a);
return 0;//You need to return in if and in else
}
}
This is basic knowledge about programming (with c). I think you are a beginner so maybe you want to start with an Tutorial?
There is nothing wrong with your code, it should work.
scanf() reads from stdin. Depending on how and where your program runs stdin can have different meanings.
On tutorialspoint.com, you have to supply data to stdin before running your program by using the "Stdin" tab.

My program.exe has stopped working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
int main(void){
int n, i, a[10], sum = 0;
for(i = 0; i < 10; i++){
printf("Enter the marks of %dth student ", i + 1);
scanf("%d", a[i]);
sum = sum + a[i];
}
printf("The total sum is %d", sum);
return 0;
}
Is there an error in my program?
Everytime I run the program, after entering the marks for the first student, I get an error saying that my program has stopped working!
This happens for most of my programs where I have used arrays!
It should be
scanf("%d",&a[i]);
Pass-by-pointer, not by value. Unfortunately, some compilers cannot perform compile-time type safety checks on calls to scanf(). So basically scanf() is treating your (uninitialized value in) a[i] as a pointer, which leads to undefined behavior.
Try this:
#include <stdio.h> //stdio not Stdio
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
scanf("%d",&a[i]); // &a[i] not a[i]
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}
scanf needs a pointer, not the value.
You invoked undefined behavior by passing data having the wrong type to scanf(). You have to pass int* to scanf(), not int, for %d.
I also corrected the #includes and added input error check.
Try this:
#include<stdio.h>
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
if(scanf("%d",&a[i])!=1){
fputs("read error\n",stdout);
return 1;
}
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}

C language_Error:expected ')' before ';' token [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Just in case you need to know what the program I'm working on -It's a homework question:A five-digit number is entered through the keyboard. Write a function to obtain the reversed number and another function to determine whether the original and reversed numbers are equal or not. Use this functions inside the main() and provide the necessary arguments to get a result.
My code is:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (){
int num,result;
printf("Please enter a number that has five digits:");
scanf("%d", &num);
result=Equality(num);
return 0;
}
int Reversed(int num){
int number=num;
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while ((num>0));
return rev;
}
int Equality(num){
int reve,numb;
if ( (numb=num)== (reve=Reversed(num);) )
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
}
There are some points to fix:
Remove semicolon in if ( (numb=num)== (reve=Reversed(num);) ), which is not ought to be. ( the main problem)
Format your code properly.
Equality() doesn't have return statement, so what it returns is not usable.
I guess Equality() is supposed to only determine that, not to print the result.
The variables reve and numb in Equality(), and number in Reversed() are not used other than being assigned, so you won't need them.
Type of function arguments shouldn't be omitted.
corrected code:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (void){
int num;
printf("Please enter a number that has five digits:");
if(scanf("%d", &num) != 1){
puts("read error");
return 1;
}
if(Equality(num))
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
return 0;
}
int Reversed(int num){
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while (num>0);
return rev;
}
int Equality(int num){
return ( num == Reversed(num) );
}

Resources