I am trying to find the cube of the numbers from 1 to 10 using various types of for loops. I was wondering why after my for loop evaluates, the program stops and does not go on to evaluate the while loop? Can someone help? The for loop and while loop are supposed to do the same thing by the way. Thanks.
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(void)
{
int num;
for ( num=1; num<11; num++){
printf("The cube of %d is %d\n", num, num*num*num);
}
getchar();
return 0;
}
#include <stdio.h>
int main1(void)
{
int num1;
scanf("%d", &num1);
while (num1<11) {
printf("The cube of %d is %d\n", num1, num1*num1*num1);
num1++;
}
getchar();
return 0;
}
PS My first programming language is Python, so I am confused why C stop after the first for loop...
:(
main() and main1() are two different functions. Your system calls main(), the entry point of your program. When main() ends, that is when your program terminates. main1 isn't touched at all.
Either concatenate the two functions (copy-paste main1 into main) or call main1() from main(). But you would need a forward-declaration first:
int main1(void);
int main(void)
{
// ...
main1();
}
int main1(void) { ... }
BTW, main1 isn't a really good name for a function since it resembles main. That can confuse people who maintain your code.
after declaring num and num1 you set num in the for loop to 1, but not num1 in the while loop. plus the main functions issues mentioned
Related
Note: I'm fairly new to C programming so I don't know everything just yet.
So I'm working on this assignment for my programming class where I have to write a recursive function count_digits( ) that counts all the digits in a string. I wrote the program and got it to compile but when I type in a number, it always gives me the same answer.
This is what my code is:
#include <stdio.h>
int count_digits(int num)
{
static int count=0;
if(num>0)
{
count++;
count_digits(num/10);
}
else
{
return count;
}
}
int main()
{
int number;
int count=0;
printf("Enter any number:");
scanf("%d",&number);
count=count_digits(number);
printf("\nTotal digits in [%d] are: %d\n",number,count);
return 0;
}
Your non void function returns nothing if num is greater than zero. The compiler should warn you about not returning value. The fix:
return count_digits(num/10);
there are a few things to consider:
What happens if you call your function count_digit() more than one time in the program?
What if you enter 0, 10, 100 as number?
Perhaps you should rethink using a static variable here.
Also for debugging, insert some printfs (or use the debugger) in count_digit() to check how your function behaves.
I am currently attempting to learn C, and have made this program to calculate the area of a regular hexagon:
#include <stdio.h>
#include <math.h>
void main(){
int a;
float ans;
scanf("%d", &a); // get length of side
ans = ((pow(a, (1/3)))/2)*(a*a);
printf("%f", ans);
}
However, it outputs seemingly random numbers.
Firstly your code doesn't compile (Missing semicolon) and also you should use int main() instead of void main().
Secondly your formula also wrong, the area of a regular hexagon of side length a is calculated as ((3√3)/2)*a².
Thirdly Expression like 1/3 always yield zero as both are integer, to get expected behavior make one of them float/double. like 1.0/3 or (float)1/3 etc.
#include <stdio.h>
#include <math.h>
int main()
{
int a;
float ans;
scanf("%d", &a); // get length of side
ans = (3*sqrt(3)/2.0)*a*a;
printf("%f", ans);
}
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 7 years ago.
I am new in programming. I need something which can generate random number with C. I found "rand()". But it is not generating random values. Please check the following simple code.
The following code gives
roll the first dice : 6
roll the second dice : 6
roll the third dice : 5
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
int dice1,dice2,dice3,total1,total2;
char prediction[10];
int dice_generator()
{
dice1= (rand()%6)+1;
printf("roll the first dice: %d \n", dice1);
dice2= (rand()%6)+1;
printf("roll the second dice: %d \n", dice2);
dice3= (rand()%6)+1;
printf("roll the third dice: %d \n", dice3);
return 0;
}
dice_generator();
total1 = dice1+dice2+dice3;
printf("final value is = %d\n",total1);
return 0;
}
You need to "seed" the random number generator.
Try calling
srand(time(NULL));
once at the top of your program.
(There are better ways, but this should get you started.)
Firstly, C language does not support nested functions. It is illegal to define dice_generator() inside the definition of main() as in your code. Your compiler might support this, but in any case this is not C.
Secondly, rand() does not generate random numbers. rand() produces a seemingly "erratic" but perfectly deterministic sequence of integers, which begins at some initial number and always follows the same path. All you can do is make rand() start its sequence from a different "seed" number by calling srand with a new seed as an argument.
By default rand() is required to work as if you called srand(1) in order to seed the sequence.
here is the code, after being corrected so the sub function dice_generator()
is properly separated, rather than buried in main().
(in C, nested functions are not allowed)
the rand() function is properly initialized via the srand() function.
unused variables ( total2 and prediction[] ) are commented out
(another excellent reason to only place one variable declaration per line)
Strongly suggest enabling all the warnings when compiling,
so your compiler can tell you about problems in the code.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
// prototypes
int dice_generator( void );
// global data
int dice1;
int dice2;
int dice3;
int total1;
//int total2;
//char prediction[10];
int main( void )
{
srand(time( NULL ));
dice_generator();
total1 = dice1+dice2+dice3;
printf("final value is = %d\n",total1);
return 0;
} // end function: main
int dice_generator()
{
dice1= (rand()%6)+1;
printf("roll the first dice: %d \n", dice1);
dice2= (rand()%6)+1;
printf("roll the second dice: %d \n", dice2);
dice3= (rand()%6)+1;
printf("roll the third dice: %d \n", dice3);
return 0;
} // end function: dice_generator
OK, so my task is to get a single digit from a natural number and sum the square numbers (Using function while, which means no arrays yet :S). For instance I type 123 so sum=1*100+2*10+3*1; However the problem is that the digit could be whatever. My problem is that the power rises with int but its like so - 1, 10, 99, 1000. The problem for me is 99. Also answer is looping but I'll fix it later. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int N,
number=0,
answer=0,
a=1,
i=0;
printf("Type natural number: ");
scanf("%d",&N);
while(N>i)
{
number=N%10;
N/=10;
a=10;
a=pow(a,i);
answer+=number*number*a;
printf("%d\n", answer);
i++;
}
return 0;
}
Try it the other way around. Don't make the input an integer. Start at the beginning of the stream, get the character, convert it to an int 'number'. Then do
answer = 10 * answer;
answer += (number * number);
This will build up your answer little by little. Note that I am not sure that this is what you are asking for due to your example not seeming to match the code.
Let me know if this is off-base and I will update it.
#include <stdio.h>
#include <math.h>
int fib();
int scan;
int main() {
scanf("%d", &scan);
printf("%d\n", fib());
scanf("%s");
return 0;
}
int fib() {
return floor((pow(1+sqrt(5)/2, scan)-(-pow(1-sqrt(5)/2, scan)))/sqrt(5));
}
I'm pretty new to programming with C and decided to try and calculate any number in the Fibonacci series. I based it off of my lua script here. I'm at a loss of what I've done wrong, could someone give me some insight?
You have the formula wrong. You want fib to be:
int fib() {
return round((pow((1+sqrt(5))/2, scan)-(-pow((1-sqrt(5))/2, scan)))/sqrt(5));
}
instead. You were missing parenthesis around the 1+sqrt(5) and 1-sqrt(5) terms and were using floor instead of round, which was underestimating the fibonacci numbers in my tests. (This mostly has to do with low precision in the pow function. The seventh fibonacci number, 13, came out to 12.969)
You also probably want to change
scanf("%s");
to
char tmp;
scanf("%c", &tmp);
Since the way you have it incorrectly omits an argument.
Hope this helps!