C math on user input [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 last month.
Improve this question
I'm new to c and I'm trying to preform maths on a user input, specifically the year of a date.
When I try to divide , or do any maths on the variable 'y' and store it in 'fpy' it will always print the value '6422032' or close to it. Any help would be great, I've been trying for hours now.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int d,m,f,y,spY;
//char y[3];
printf("Day from date calculator \n");
printf("Please enter the date in DD/MM/YYYY formate. For example , 1/7/1440 \n");
printf(">");
scanf("%d/%d/%i",&d,&m,&y);
int fpy = y/10;
printf("%d",&fpy);
return(0);
}

this
printf("%d",&fpy);
should be
printf("%d",fpy);

Related

Digits of number and output [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 1 year ago.
Improve this question
I have a problem that this code doesn't end with output.
I appreciate if help me.
#include <stdio.h>
int main(){
int number,counter=0;
scanf("%d",&number);
while (number!=0){
number=number/10;
counter++;
}
printf("the number has %d digits",&counter);
return 0;
}
You should remove "&" from your printf statement.
& is for scanning not printing
the correct form is :
printf("the number has %d digits",counter);

if statement works with any input [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'm learning c programming language, i have been told to make a password code but whenever i compile it , it gives me "correct" even tho its not. this is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int s;
scanf("%d", &s);
if (s = 1234) {
printf("the password is correct");
} else {
printf("the password is incorrect");
}
return 0;
}
also can someone give me a link where they have beginner tasks for me ?
i enrolled in an embedded system course and i had no experience for C , so i was told to learn C 1st
I think you are using wrong the equal sign. It must be == not = in the if statement. Also you have to include
#include <stdio.h>

Dev C keeps returning a wrong value [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
#include <stdio.h>
#include <stdlib.h>
main()
{
int n;
printf("Introduce un número entero\n");
scanf("%d", &n);
printf("Has introducido el número: %d", &n);
}
Every time I run this C code I get 6487628 for n, I have uninstalled and installed it over and over again and it keeps doing that, I don´t know what else to do.
You don't want the &n in the printf(), you want n. You are displaying the memory location n is stored in

I am getting absurd values for the variable 'credits' [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>
int main (void)
{
int hist,geo,phy,chem,bio;
int credits=0;
printf("Enter marks in history : ");
scanf("%d",&hist);
if(hist>40)
credits =10;
else
printf("No credits awarded for history");
printf("Credits obtained is %d",&credits);
return(0);
}
when I run the code, and I get a value of 230586 for the variable 'Credits'. Please help. I am a beginner in C
&x is like asking a question "What is the address of variable x?" , that's why you get the strange number. In order to print the variable value, please pass credits instead of &credits to the printf function.
printf("Credits obtained is %d", credits);

c float to int conversion acting weird [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 7 years ago.
Improve this question
I'm trying to convert a float value to an int in C. I'm using print statements to see what's happening and making sure I'm getting the desired results, but something is not working correctly. Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void){
float changeOwed = -1.00;
while(changeOwed < 0.00){
printf("How much change is owed?\n");
changeOwed = GetFloat();
}
printf("%f\n", changeOwed);
int centsOwed = roundf(changeOwed*100);
printf("%o\n", centsOwed);
If user input is, lets say 0.49, here is the output:
0.490000
61
I don't understand why the cast result is 61. I would expect normal errors to be a result of 0, 48 or 50, but I don't get this weird result and can't figure out the logic of it.
In case you don't get it yet ...
"061" is octal for "49".
Use printf("%d") instead of "%o" if you want to see a decimal "49".
Here is a good list of "printf" format options:
http://www.cplusplus.com/reference/cstdio/printf/

Resources