I am writing a program that needs to do a bit of arithmetic.
Here's what I have so far:
#include <stdio.h>
int main(void){
double h, x, n;
printf("enter h > ");
scanf("%1f", &h);
printf("enter x > ");
scanf("%1f", &x);
/* here's where I believe I'm encountering an error */
n = x/h;
return 0;
}
so let's say I put in h = 0.01 with x = 0.25, I should get n = 0.25/0.01 = 25 right?
I've tried typecasting:
n = (float)x/(float)h;
but it doesn't work...
You have written %1f (that's a digit one) in your scanf calls, but the right format for a double is %lf (that's a lower case L).
Consequently, your numbers aren't being converted properly to doubles, and you can't expect proper results.
The problem is probably due to scanf.
I suggest using fgets instead.
But try:
int main(void){
float h, x, n;
printf("enter h > ");
scanf("%f", &h);
getchar();
printf("enter x > ");
getchar();
scanf("%f", &x);
n = x/h;
return 0;
}
See my comment. You need to specify the l modifier when attempting to read double input.
#include <stdio.h>
inline int flush() {
return fflush(stdout);
}
int main() {
double x, h, n;
printf("enter h> ");
flush();
scanf("%lf", &h);
printf("enter x> ");
flush();
scanf("%lf", &x);
n = x / h;
printf("n: %lf\n", n);
}
The reason is that you have declared h,x,n to be a double and then assigned it to a float.try using float in the variable declaration
#include <stdio.h>
int main(void){
float h, x, n;
printf("enter h > ");
scanf("%f", &h);
printf("enter x > ");
scanf("%f", &x);
n = x/h;
printf("%f",n);
return 0;
}
Related
Program 1
int main() {
int p, t;
float r;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
printf("The simple interest is %f", prt / 100);
return 0;
}
Program 2
#include<stdio.h>
int main() {
int p, t;
float r, interest;
interest = prt / 100;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
printf("The simple interest is %f", interest);
return 0;
}
Let's break down the task of program 2.
You're trying to find out the value of "interest" where interest = ( p * r * t ) / 100.
First, you need the value of 3 variables which are 'p', 'r', 't'.
You're taking those values from user (using scanf).
You'll be able to get the value of "interest" after taking all those inputs.
So you'll have to assign values in the variable "interest" according to the equations right after you're done with taking those inputs.
That means at the end of the program.
Another thing, when you're writing an equation, you'll have to write down corresponding operators too.
#include<stdio.h>
int main() {
int p, t;
float r, interest;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
interest = (p * r *t) / 100.0;
printf("The simple interest is %f", interest);
return 0;
}
After entering any values of p,n,r; the value of i is only returning 0.0000.
int main()
{
int p,n;
float r,i;
i=p*n*r/100;
printf("enter principle=\n");
scanf("%d",&p);
printf("enter rate=\n");
scanf("%f",&r);
printf("enter no.of years=\n");
scanf("%d",&n);
printf("value of i=%f",i);
return 0;
}
The variable p, n, r are not initialized at the line i=p*n*r/100;. You have to do the calculation after reading the values.
int p,n;
float r,i;
printf("enter principle=\n");
scanf("%d",&p);
printf("enter rate=\n");
scanf("%f",&r);
printf("enter no.of years=\n");
scanf("%d",&n);
i=p*n*r/100; /* do calculation after reading values */
printf("value of i=%f",i);
I'm very new to programming (so I apologize in advance), and I am having trouble figuring out how to make a for loop that will do the following:
I'm asking the user to input two variables (i'll call them x & y), which I then am calculating x/y = z. I want to pose this two variable input question 3 times, and then add up the 3 z to find the average. (The later part about accumulating/averaging I can figure out, but getting a for loop to repeat and give z three times is stumping my extremely novice mind. So far I can only get the for loop to ask for the two variable inputs one time, spit out z, and then terminate (I haven't attempted the averaging of z yet, because I don't have more than one z at this time).
To make things clearer, here's what I've got:
#include <stdio.h>
int main(void)
{
float x, y, z;
int c;
printf ("Enter x: ");
scanf ("%f", &x);
while ( (c = getchar() != '\n') && c != EOF);
printf ("Enter y: ");
scanf ("%f", &y);
while ( (c = getchar() != '\n') && c != EOF);
for (; x <3; x++)
{
z = x / y;
printf("Your average is %f\n", z);
}
printf("Thank you for using the program. Goodbye\n" );
getchar();
return 0;
}
Thanks for your help!!
#include <stdio.h>
int main(void)
{
float z[3];
for (int i = 0; i < 3; ++i)
{
float x, y;
printf ("Enter x: ");
scanf ("%f", &x);
printf ("Enter y: ");
scanf ("%f", &y);
z[i] = x / y;
printf("Your average is %f\n", z[i]);
}
printf("Your overall average is %f\n", (z[0] + z[1] + z[2]) / 3);
printf("Thank you for using the program. Goodbye\n" );
getchar();
return 0;
}
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
Hey I am writing a simple program that adds fractions.
but whenever i run the program it will not perform the operations.
It only scans the input, but no output.
please spot my error! >>>>>>CANNOT USE FLOATS<<<<<<<<
#include <stdio.h>
int main ( void )
{
int lcd, d1, d2, num1, num2, sum;
printf("Enter the first number:");
printf("Enter the numerator:");
scanf("%d", num1);
printf("Enter the denominator:");
scanf("%d", &d1);
printf("Enter the second number:");
printf("Enter the numerator:");
scanf("%d", &num2);
printf("Enter the denominator:");
scanf("%d", &d2);
for (lcd = d1; lcd % d2 != 0; lcd+= d1);
num1 *= lcd / d1;
num2 *= lcd / d2;
sum = num1 + num2;
printf("%d", sum);
return 0;
}
scanf must receive pointers
scanf("%d", num1) -> scanf("%d", &num1)
Because scanf use call by reference. What is that? please see the following code.
#include <stdio.h>
void foo(int i)
{
i = 1;
}
void bar(int *pi)
{
*pi = 1;
}
int main()
{
int a = 2;
foo(a);
printf("%d", a); /* output is 2 */
bar(&a);
printf("%d", a); /* output is 1 */
}
In foo(a), we're using call by value. That means a is copied when we call foo. the code, i = 1, of foo changes only the copy of a, and does NOT change real value of a.
In bar(&a), we're using call by reference. Can you find the difference? Yes, it's bar(&a), not bar(a). "&" operator gets the pointer of a, and we call bar with the pointer. So pi refers to a, and *pi = 1 changes real value of a successfully.
A real-life example is here: printf and scanf.
int i = 0;
printf("output number : %d\n", i);
scanf("%d", &i); /* input number */
printf doesn't need to change its arguments, so it use call-by-value.
But, scanf receives user's input and change its arguments into user's input. so it use call-by-reference. thus, we should use &i, not i.
Does it need to be that complex? Why not just divide the numerator by the denominator?
Also, your working with integers. I think you want floats.
#include <stdio.h>
int main ( void )
{
int d1, d2, num1, num2;
printf("Enter the first number:");
printf("Enter the numerator:");
scanf("%d", &num1);
printf("Enter the denominator:");
scanf("%d", &d1);
printf("Enter the second number:");
printf("Enter the numerator:");
scanf("%d", &num2);
printf("Enter the denominator:");
scanf("%d", &d2);
printf("%f",
((float) num1 / (float) d1) + ((float) num2 / (float) d2)
);
return 0;
}
If you want to compute the non simplified sum of two fractions, just using the following formula
a/b + c/d = (a*d + c*b)/(b*d)
Therefore, the following code could be used to achieve your goal
#include <stdio.h>
int main ( void )
{
int den1, den2, num1, num2, sum_num, sum_den;
printf("Enter the first number:\n");
printf("\tEnter the numerator:");
scanf("%d", &num1);
printf("\tEnter the denominator:");
scanf("%d", &den1);
printf("Enter the second number:\n");
printf("\tEnter the numerator:");
scanf("%d", &num2);
printf("\tEnter the denominator:");
scanf("%d", &den2);
sum_num = num1 * den2 + num2 * den1;
sum_den = den1 * den2;
printf("sum = %d/%d\n", sum_num, sum_den);
return 0;
}
void main ()
{
int sayi;
int ikisayi;
printf("first number");
scanf("%d",&sayi);
printf("second number");
scanf ("%d", &ikisayi);
snc = ikisayi + sayi;
printf(\n Total= %f \n , snc );
}
What is wrong with that can you help me?
You need to declare int snc;, and the format specifier in printf should be %d. In addition, the return type of main should be int, you should include stdio.h and you should have double quotes around your format string in printf.
#include <stdio.h>
int main() {
int a, b, c;
printf("first number: ");
scanf("%d", &a);
printf("second number: ");
scanf("%d", &b);
c = a + b;
printf("total: %d\n", c);
return 0;
}
Also, printf("\n Total= %d \n" , snc );