I need a program that repeatedly asks for a real number, and then rounds it and output the rounded number as an integer. and it stops asking for real numbers when input 3.14

Recommended Answers

All 13 Replies

That sounds like a good beginner's exercise. You should totally write a program like that. :)

That sounds like a good beginner's exercise. You should totally write a program like that. :)

hey I need help. I made one code but doesn't work!!!

I made one code but doesn't work!!!

If you do not post the code and ask a specific question about it, everyone is going to assume you want someone else to do your homework for you.

hey I need help. I made one code but doesn't work!!!

Can we see what you have done so far?

If you do not post the code and ask a specific question about it, everyone is going to assume you want someone else to do your homework for you.

this is my code:
#include <stdio.h>
Int main()
{
Double x;
Int b;

Printf(“Real: “);
Scanf(“%lf”, &x);

If (x>0)
{
Int b;
b= x+ .5;
printf( “%d”, %b);
}

else
{
Int b;
B=x-.5;
Printf(“%d”, &b);
}

If( x==3.14)
{
printf(“Pye Pye!\n”);
}

Return 0;
}

Most things in programming shouldn't start with an uppercase letter (at least in C anyway)
data types are lowercase and so is printf
Also you have redeclaration of b. And you use b and B interchangeable which you can't do.
This whole code is really a mess. You need to review the basics of declraing variables and issues relating to syntax.
Your not going to be able to write any programs if you can't understand the basics of syntax.

Most things in programming shouldn't start with an uppercase letter (at least in C anyway)
data types are lowercase and so is printf

yes I know that. but how can I make it work?

this program should input real numbers and output the number rounded. and also, it should stop input and output numbers when the input is 3.14.

for example, the output should be like:

Real: 2.3
2
Real: 3.7
4
Real: 2.5
3
Real: 3.14
Pye Pye!


code:
#include <stdio.h>
Int main()
{
double x;
int b;

printf(“Real: “);
scanf(“%lf”, &x);

if (x>0)
{
int b;
b= x+ .5;
printf( “%d”, &b);
}

else
{
int b;
b=x-.5;
printf(“%d”, &b);
}

if( x==3.14)
{
printf(“Pye Pye!\n”);
}

return 0;
}

yes I know that. but how can I make it work?

Hmm. I'm not so sure you do because you are still writing int main() with a capital 'I'. Try running the code and post the errors being reported by the compiler. Also, use code tags when posting code in the future.

You don't need to use the & in printf statements. It is only needed when scanning.
Also, when you try to print the variable b, you should use the format specifier for double values. The %d is used for ints. %f can be used for numbers with decimal points.

needhelpe> yes I know that. but how can I make it work?
A loop is needed to continuously repeat the question until the stop signal (3.14) is reached.

If I would want to ask for a digital until the number five is entered I could write it like this:

#include <stdio.h>

int main(void)
{
	int result = 0;

	/*
	 * continuously asks for input until 5 is entered
	 */
	while (result != 5) {
		int c;

		/* repeat the question each time */
		fputs("Enter a number: ", stdout);
		fflush(stdout);

		/* minimum error checking for entered input */
		if (scanf("%d", &result) == 1) {
			/* if correct, input is displayed */
			printf("You entered: %d\n", result);
		} else {
			/* input was wrong, clear standard input */
			while((c = getchar()) != '\n');
		}

	}
	return 0;
}

You don't need to use the & in printf statements. It is only needed when scanning.
Also, when you try to print the variable b, you should use the format specifier for double values. The %d is used for ints. %f can be used for numbers with decimal points.

the program works. now I just need something that makes the output ask me for a real number until I input 3.14.

example of output:
(~)$ round
Real: 3.2
3
Real: 4.6
5
Real: 2.8
3
Real: -3.4
-3
Real: 3.14
Pye Pye! (here it stops!)

Use a while loop. The stopping condition should be retail value = 3.14

while(retail_value != 3.14)
{
//put your code that executes the program here
}

Now when the user enters 3.14 as the retail value, the loop will stop and output will terminate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.