Card Hangover C [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to do this problem: http://poj.org/problem?id=1003
#include <stdio.h>
int c;
int a = 0;
int i;
int main()
{
scanf("%.2f", &c);
if (0.01 <= c <= 5.20){
for (i = 1; a < c; ++i){
a += (1/(i + 1));
}
printf("%d card(s)", i + 1);
}
return 0;
}
My code isn't working? For some reason it always returns 2 card(s) no matter what I enter. Can someone find the problem?
Thanks!

Problem 1: This is not how you test if a variable is between two values:
if (0.01 <= c <= 5.20){
The correct way is
if (0.01 <= c && c <= 5.20){
Your code is interpreted as if you'd written:
if ((0.01 <= c) <= 5.20){
(0.01 <= c) will be either 0 or 1, and both of these are less than 5.20, so it's always true.
Problem 2: The variables a and c need to be float, not int, because int variables can't have fractions in them, and %f format in scanf requires that the corresponding argument be a pointer to float.

Related

why doesn't this program in C language print anything? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
why doesn't this program print anything?
int main(){
int a = getchar()-'0';
getchar();
int b = getchar()-'0';
int vsota = 0;
vs = (a+b)%10;
putchar(vs);
printf("\n");
}
I put in the numbers 7 and 9 and it was supossed to outprint 6 but it does not.
putchar(vs); writes the character whose code is the value in vs. The value in vs is 6. So it writes the character with code 6. That character is not the digit “6”. You do not see anything because it is a “control character” with no visible appearance. To write the character for the digit whose value is in vs, use putchar('0' + vs);.
Also, fix this:
int vsota = 0;
vs = (a+b)%10;
That would not have compiled, so presumably you made a mistake when entering code into Stack Overflow. Use the same name in both places.
because you try to output non printable character
int main(){
int a = '1'-'0';
int b = '6'-'0';
int vsota = 0;
vsota = (a+b)%10;
putchar(vsota + '0');
printf("\n");
}
https://godbolt.org/z/NAyrNn

Taking power with loop(with-out pow() function) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`
Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 1.

How to sum 2 numbers in C language? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am new to programming. I am learning how to sum in C language. Please see below code, What am I missing? Why its giving error?
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3, d = 0;
d = a + b + c;
printf(d);
return 0;
}
The "f" in "printf" means "formatted". The first parameter you pass to it has to be the format that you want to print with, not what you want to print. In this case, it seems to me that you likely want to:
printf("%d\n", d);
The %d means that printf should interpret the second parameter as a signed integer (which d actually is). The \n adds a newline (and usually flushes the buffer).
You can learn more about printf and its format by googling for it, or reading a man page about it, or its page in a compiler help file.
you are printing a integer value. In order to print a integer value you have follow this way...
printf("%d",d); // for integer it's %d
// so your progam should looks like this
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3, d = 0;
d = a + b + c;
printf("%d\n", d);
return 0;
}

How to write (a^n-1) in c programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to write (a^n-1) in c programming. I wrote pow(a,n-1) and the output is
wrong. How can I solve this problem? Thanks.
Here is my code:
#include <stdio.h>
#include<math.h>
int main() {
float a, r,n;
float sum = 0;
a = 1.04*pow(a, n-1);
r = 1.02*pow(1.04,-1);
n = 2;
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
The correct output should be 286376 but the program showed 2903773
Every C program executes line by line. So, at the time when compiler came on
a = 1.04*pow(a, n-1);
this line, variable a and n was not assigned with any value, resulted in giving you a garbage value...
So, the problem is, you had not assigned values in variables, and still, you were using them.
You have to first assign values in variables before using them. Otherwise, they will pick any garbage value from memory (Any Random number).
Assign value in a and n and try again.
Edit: As chux's comment suggests, if your program is supposed to give 286376 as output, then value of a should be 1 and value of n should be 2.
So, your correct code would be this:
#include <stdio.h>
#include <math.h>
int main() {
float a=1, r, n=2;
float sum = 0;
a = 1.04*pow(a, n-1) ;
r = 1.02*pow(1.04,-1);
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
pow(a, n-1) translates into exp(log(a) * (n-1)) which isn't precisely the same.. You can try to round the output like this:
round(pow(a, n-1))

Binary To Decimal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.

Resources