I am trying to write a program in C that takes a positive integer that is input and then counts down to zero and back up to the number by 1. I am getting errors when trying to run the code saying that my variables usernum and usernum2 are undeclared. In the main function I declare them both as integers so what am I doing wrong?
#include <stdio.h>
`
void loop_down_to_zero(){
while (usernum2 >= 0){
printf("%d\n", &usernum2);
usernum2--;
if (usernum2 ==0) {
printf("****\n");
}
}
}
void loop_up_to_zero(){
while (usernum2 < usernum){
usernum2++;
printf("%d\n", usernum2);
}
}
int main(int argc, char * argv[]) {
int usernum;
int usernum2;
usernum2 = usernum;
printf("Enter a positive integer: \n");
scanf("%d", &usernum);
while (usernum <= 0){
printf("Error, enter a positive integer: \n");
scanf("%d", &usernum);
}
if (usernum > 0) {
loop_down_to_zero();
loop_up_to_zero();
}
}
`
I tried moving the void functions below main because I am still trying to figure out some of the syntax in C but it did not appear to have much of an impact.
In the line usernum2 = usernum you're trying to assign value in usernum to usernum2. But in usernum there's no value at that moment, because you've only declared the variable usernum, you've never defined it. Example of declaration: int usernum;. Example of definition: int usernum = 0;
Also, in your line printf("%d\n", &usernum2); you shouldn't pass the address, you should pass the value: printf("%d\n", usernum2);
it's not enough do declare your variables in main, you have to declare then both functions, and you also should send your user input as a parameter to the functions.Also you shouldn't print &, this will print the memory address.
#include <stdio.h>
void loop_down_to_zero( int usernum ){
while (usernum >= 0){
printf("%d\n", usernum);
usernum--;
if (usernum ==0) {
printf("****\n");
}
}}
void loop_up_to_zero(int usernum,int usernum2){
while (usernum2 < usernum){
usernum2++;
printf("%d\n", usernum2);
}}
int main(int argc, char * argv[]) {
int usernum;
printf("Enter a positive integer: \n");
scanf("%d", &usernum);
while (usernum <= 0){
printf("Error, enter a positive integer: \n");
scanf("%d", &usernum);
}
usernum;
if (usernum > 0) {
loop_down_to_zero(usernum);
loop_up_to_zero(usernum,0);
}}
Related
Trying to make a code that gets the factorial of the inputted number.
int factorial(int number, int i)
{
int endval;
for(i = number - 1; i>0; i--){
endval = number * i;
}
if (endval == 0){
printf("1");
}
return endval;
}
int main()
{
int endvalue, numA, numB;
char userchoice[1];
printf("Enter a choice to make (f for factorial): \n");
scanf("%s", userchoice);
if(strcmp(userchoice, "f")== 0){
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA, numB);
printf("%d", endvalue);
return 0;}
getch();
return 0;
}
For some reason the whole for loop doesn't do anything in the function when I set the answer (number*i)= endval. It just prints out the same number I inputted and gives me an absurd answer for 0!.
int factorial(int number, int i)
{
int endval;
for(i = number - 1; i>0; i--){
endval = number * i;
}
if (endval == 0){
printf("1");
}
return endval;
}
However the code works perfectly fine when I remove endval variable entirely (with the exception that it gets 0! = 10)
int factorial(int number, int i)
{
for(i = number - 1; i>0; i--){
number = number * i;
}
if (number == 0) {printf("1");}
return number;
}
Is there anything I missed in the code that's causing these errors?
A definiton of factorial is:
factorial(0) = 1
factorial(n) = n * factorial(n-1)
Note: Factorial is legal only for number >= 0
In C, this definition is:
int factorial(int number)
{
if (number < 0)
return -1;
if (number == 0)
return (1);
/*else*/
return (number * factorial(number-1));
}
#include <stdio.h>
#include <string.h>
int factorial(int number)
{
int endval=1;
for(int i = number ; i>0; i--){
endval *= i;
}
return endval;
}
int main()
{
int endvalue=0;
int numA=0;
char userchoice[1];
printf("Enter a choice to make (f for factorial): ");
int ret=scanf("%s", userchoice);
if (!ret){
printf("Error in scanf: %d", ret);
}
if(strcmp(userchoice, "f")== 0){
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA);
printf("%d", endvalue);
return 0;
}
getchar();
return 0;
}
Code with some changes will work
factorial() function can get only one argument.
As a good habit all variables must be initialized.
Add include statement to source and be explicit not rely on compiler.
As we use strcmp() we must include string.h
use standard getchar() instead of getch()
Also can check return value of library function scanf() to ensure reading is correct or not.
You can use warnings from compiler to get most of above notes. In gcc: gcc -Wall code.c
Use a debugger to run program line by line and monitor variables value in each steps or use as many printf() to see what happens in function call.
There are possibly few things to correct. See please attached code.
int factorial(int number)
{
if (number == 0){ return 1; }
int endval=1, i;
for(i = 1; i<=number; i++) { endval *= i; }
return endval;
}
int main() {
int endvalue, numA;
char userchoice[1];
printf("Enter a choice to make (f for factorial): \n");
scanf("%s", userchoice);
if(strcmp(userchoice, "f")== 0) {
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA);
printf("%d", endvalue);
return 0;
}
getch();
return 0;
}
I am learning about functions and how to call upon them and use them in class. I don't quite understand where I've gone wrong here. I know that there are some mistakes around the int main part. I have asked my teacher and he is reluctant on giving me an example that would solve my problems or help me out. I think my main problem is at factorial_result = factorial();
#include <stdio.h>
void mystamp(void)
{
printf("My name is John Appleseed\n");
printf("My lab time is 12:30 on Sunday\n");
return;
}
int getnum(void)
{
int local_var;
printf("Please enter an integer: ");
scanf("%d%*c", local_var);
return(local_var);
}
int factorial(void)
{
int x,f=1,local_var;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial();
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
Declare local_var as a global variable and do:
local_var = getnum();
OR
Change main() to:
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial(result);
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
And factorial() to:
int factorial(int n)
{
int x,f=1,local_var=n;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
Your factorial should be calculated based on the input( i.e in your case int result ).
So, your method factorial() should looks as follows :
int factorial( int number )
{
int factorial_value = 1;
while( number > 0 )
{
factorial_value *= number;
number--;
}
return factorial_value;
}
Then, the correct factorial would be returned and printed accordingly ! Regarding the scope of the variables that you have used, see the comments under your question.
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
This is a simple factorial program using recursion calling function !
include
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
for (c = 1; c <= n; c++) fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
My function should get a non-negative integer, and return how many digits there are in the number, for Example for the number 563 the function return 3.
And for 0 will return 1.
*I'm new in c so it still very confusing for me.
Thanks.
Here is my code:
#include <stdio.h>
int numOfDigits(int n); //Declartion
void main()
{
int num1, counter = 0, newNum;
printf("Enter A Number: ");
scanf("%d", &num1);
}
int numOfDigits(int n1)
{
int counter = 0;
if (n1 == 0)
return 1;
while (n1 > 0) {
counter++;
n1 /= 10;
}
return counter;
}
What should I write to make the program work?
Just call the digit counting function that you made and assign its return value to a variable, and then print the value of that variable using printf() function; or alternatively, call your digit counting function inside printf() as an argument:
#include <stdio.h>
int numOfDigits(int n);
int main(void)
{
int num1;
int counter = 0;
printf("Enter A Number: ");
scanf("%d", &num1);
counter = numOfDigits(num1);
printf("number of digits: %d\n", counter);
//printf("number of digits: %d\n", numOfDigits(num1)); alternate method
}
int numOfDigits(int n1)
{
int counter = 0;
if (n1 == 0)
return 1;
while (n1 > 0) {
counter++;
n1 /= 10;
}
return counter;
}
A couple of notes:
Since you mentioned that you need to get a non-negative number, consider using unsigned int type, and change the format specifier inside scanf() and printf() call to "%u".
Function main should be declared as int main(void) or int main(int argc, char **argv)
Using a scanf in a for-loop, where do I store the input, if there is more then 1 loop? Trying to use arrays, but it always fails.
Let's say:
Please enter number of octets: 3
Please enter octet: 1
Please enter octet: 2
Please enter octet: 3
Then the input should be stored in the array, but it doesn't work. For the controll, I printf v[2] and it should be 3, but it is a another number.
#include<stdio.h>
#include<limits.h>
int main()
{
int c;
int v[c];
printf("Please enter number of octets:\n");
scanf("%d", &c);
if ( (c > 1) && (c < CHAR_MAX))
{
for ( i = 1; i <= c; i++)
{
printf("Please enter octet:\n");
scanf("%d", x);
v[c]=x;
}
printf("v[2]: %d\n", v[2]);
}
return 0;
}
You probably want this:
#include <stdio.h>
#include <limits.h>
int main()
{
int c;
printf("Please enter number of octets:\n");
scanf("%d", &c);
if ((c > 1) && (c < CHAR_MAX))
{
int v[c];
// scan values into array
for (int i = 0; i < c; i++) // indexes from 0 to c-1 !!
{
printf("Please enter octet:\n");
scanf("%d", &v[i]);
}
// print all values from array
for (int i = 0; i < c; i++)
{
printf("v[%d]: %d\n", i, v[i]);
}
}
else
{
printf("Input error\n"); // show an error message
}
return 0;
}
Not clear what are you trying to do here but if you modify you code like below it will work:-
for ( i = 0; i <= c; i++)
{
printf("Please enter octet:\n");
scanf("%d", &x);
v[i]=x;
}
printf("v[2]: %d\n", v[2]);
and to print all the value just add below codes
for ( i = 0; i <= c; i++)
printf("v[i]: %d\n", v[i]);
From the code you have posted, the following lines may be the reason for the problem.
int c;
int v[c];
This is invalid. In C, array must be declared like this.
data_type array_name[size];
To make your declaration valid, you must either read the value of n or specify it like this.
int c = 10;
int v[c];
Also,
scanf("%d", x);
Change it to
scanf("%d", &x);
I'll try not to answer the question but let me point out the mistakes
#include<stdio.h>
#include<limits.h>
int main()
{
int c; // uninitialized
int v[c]; // this will behave quite unexpectedly because c is uninitialized
printf("Please enter number of octets:\n");
scanf("%d", &c);
if ( (c > 1) && (c < CHAR_MAX)) // c is a int so why restrict to CHAR_MAX
{
for ( i = 1; i <= c; i++) // trying to scan c elements and i is undefined
{
printf("Please enter octet:\n");
scanf("%d", x);
v[c]=x; // assigning to v[c] everytime so if c is 2 v[2] is the only element getting modified. not something you want to
}
printf("v[2]: %d\n", v[2]);
}
return 0;
}
I need to make a program that checks to see if an entered value has any repeated digits. The user is asked to enter numbers until the entered value is 0. If there are any repeated digits, it displays "repeated digits" and then asks the user to enter another value. If there are no repeated digits, it displays "no repeated digits" and asks the user to enter another number. So far, this is what i have. It terminates the program when 0 is entered, but it always displays "no repeated digits" even if there are some.
#include <stdbool.h>
#include <stdio.h>
int main(void)
{
bool digit_seen[10] = {false};
int digit;
long int n = 0;
printf("Enter a number: ");
scanf("%ld", &n);
while(n >= 0){
if(n==0)
break;
while (n > 0){
digit = n % 10;
if (digit_seen[digit]){
digit_seen[digit] = true;
break;
}
n /= 10;
}
if (n > 0)
printf("Repeated digit: %d\n", digit);
else
printf("No repeated digit\n");
scanf("%ld", &n);
}
return 0;
}
A couple of things:
1: A bool only has two states: true and false. If you trying to build a frequency counter of each digit seen, for the presence of a digit more than once, then you should use a data type that can count to at least two, like a char or short or int, or your own enum.
2: This code:
if (digit_seen[digit]){
digit_seen[digit] = true;
break;
}
Is never going to be evaluated as true since you initialized digit_seen to be false at the start of your main function. What you should be doing is something like this:
#include <stdio.h>
int main(int argc, char *argv[])
{
int digit_seen[10] = {0};
int entry;
int i, flag = 0;
printf("Enter a number: ");
scanf("%ld", &entry);
while(entry > 0)
{
int digit = (entry%10);
digit_seen[digit]++;
if(digit_seen[digit]>=2)
{
printf("Repeated digit: %d\n", digit);
}
entry /= 10;
}
for(i = 0; i < 10; i++)
{
if(digit_seen[i]>1) flag=1;
}
if(!flag)
{
printf("No repeated digits\n");
}
return 0;
}
#include <stdio.h>
int main() {
int seen [10] ={0}; // we set every element for a number is just 0
int N,rem;
printf("Enter the number:");
scanf("%d", &N);
while(N>0){
rem = N%10;
seen[rem]+=1;
N = N/10;
}
int i;
for(i=0;i<10;i++){ // checking the number seen counts
if(seen[i]==0){
continue;
}
printf("%d seen %d times\n",i,seen[i]); // just returned the given numbers informations
}
return 0;
}