No output No error whats wrong

Thread Starter

mkbutan

Joined Sep 30, 2008
299
hi sir is this code correct? pl help.
Rich (BB code):
/*
  Name: mkbutan
  Copyright: 
  Author: 
  Date: 02/08/12 23:06
  Description: exec 1.5 pg21
*/
#include<stdio.h>
#include<conio.h>
void main()
{
     int r,x;
     float pi;
     printf("Enter the Radius : \n",r);
     scanf("%d",r);
     x = (2*pi*r)*(2*pi*r);
     printf("The Area of a Circle is : %d",x);
     getch();
     }
 

bretm

Joined Feb 6, 2012
152
You haven't assigned a value to the pi variable.

You're passing in "r" to the first printf, but you don't need to print r. I don't know why you're getting "no output" but it might be related to this.

The formula for circle area is pi * r * r. Your result will be 4*pi times too large.

x is int, not float, so the fractional portion of the answer will be truncated. If you change it to float be careful to also change the %d in the last printf.
 

WBahn

Joined Mar 31, 2012
30,071
What do you mean when you say "No output", do you mean that it isn't outputting anything at all, not even your "The Area of a Circle is : " string?

printf("Enter the Radius : \n",r);

Here you pass it an argument but there is no format specifier in the format string. The messes up the stack. In this case, you are probably okay. But don't pass 'r' if you aren't going to do anything with it and don't do anything with it prior to initializing it.

What is pi supposed to be? The compiler can't read your mind and make it what you would like it to be. You have to initialize it. If it is being initialized to a real small value (roughly a 50% change of this, since it is a float), you are probably ending up with a value in x that is getting truncated to zero (since it is an int).

Why are you squaring both the 2 and pi?

Why are you making the radius and the area integers?
 

Thread Starter

mkbutan

Joined Sep 30, 2008
299
its a Question in the book of
PROGRAMMING IN ANIC C
the question is :-

Given the radius of a circle, write a program to compute and display its area.
Use a symbolic constane to define the ∏(pi) value and assume a suitable value for radius.


my Answer

Rich (BB code):
/*
  Name: mkbutan
  Copyright: 
  Author: 
  Date: 03/08/12 13:50
  Description: exec 1.5 pg 21 New
*/
#include<stdio.h>
#include<conio.h>
void main()
{
     int r,x;
     float pi;
     /*printf("Enter the Radius : \n",r);
     scanf("%d",r);*/
     pi = (22/7);
     r = 5;
     x = (pi*r*r);
     printf("The Area of a Circle is : %d",x);
     getch();
     }
but i dont get any output and there is no error

Use a symbolic constane to define the ∏(pi) value
how to write the symbol of pi from the keyboard

pl help
 

Attachments

WBahn

Joined Mar 31, 2012
30,071
Your screen shots don't jive. Your Console in the upper right shows it printing out the prompt for the radius, yet you have that code commented out in the source code listing to the left.

We need to see the code and the result that is produced by that same code.

Please don't use big fonts and colors in your posts; most people find it annoying.

To put the pi symbol in a post, use Alt-227 (using the numbers on the keypad and NOT the numbers along the top row of the keyboard). It will look like: π, which is a pretty poor pi symbol. I usually just type pi or PI.

If you are asking how to enter that greek letter symbol into your source code, you can't. Source code is ASCII text.

You are asked to use a symbolic constant. The more correct term (at least in the C world) is a "non-parameterized macro", but they are also commonly called just a #define constant or even just a #define. Both are a bit sloppy as far as terminology goes, but they get the point across.

So you want to do something like the following:

#define FRED (4.3827317)

FRED is now a symbolic constant and you can have things like:

y = 2*FRED + 3;

When you go:

pi = (22/7);

The variable pi will be set equal to 3. That is because the expression (22/7) must be evaluated before the compiler considers the type of variable that it will eventually be stored in. For a variety of reasons, when given the option of using integer division, the compiler will do so. This means that 22/7 is truncated and the fractional part is discarded. To avoid this, get in the habit of including ".0" on all literal constants when you want them to be treated as reals (floating point values). So use

pi = (22.0/7.0);

You should use either

int main(void)

or

int main(int argc, char* argv)

for your main() declaration. Older texts taught the use of void main() but this is deprecated and can get you into trouble. So just get in the habit of always using one of the two recommended and have main() return a value of 0 (or, even better, use

return EXIT_SUCCESS;

to exit main. Note that I don't know if these recommendations apply for non-hosted environments, such as code targeted for a microcontroller.
 

djsfantasi

Joined Apr 11, 2010
9,163
Your screen shots don't jive. Your Console in the upper right shows it printing out the prompt for the radius, yet you have that code commented out in the source code listing to the left.
I think WBahn may have found the problem...

The "/*" just before your printf statement is the start of a comment. It looks like the remainder of your program is commented out, hence it is not doing anything.

Rich (BB code):
/*
Name: mkbutan
Copyright: 
Author: 
Date: 03/08/12 13:50
Description: exec 1.5 pg 21 New
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r,x;
float pi;
/*printf("Enter the Radius : \n",r);
scanf("%d",r);*/
pi = (22/7);
r = 5;
x = (pi*r*r);
printf("The Area of a Circle is : %d",x);
getch();
}
 

kubeek

Joined Sep 20, 2005
5,795
mkbutan said:
Rich (BB code):
/*
Name: mkbutan
Copyright: 
Author: 
Date: 03/08/12 13:50
Description: exec 1.5 pg 21 New
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r,x;
float pi;
/*printf("Enter the Radius : \n",r);
scanf("%d",r);*/
pi = (22/7);
r = 5;
x = (pi*r*r);
printf("The Area of a Circle is : %d",x);
getch();
}
This will set pi to 3, because division of integer numbers results in integer. You need to use pi=22.0/7 to get a (more) correct answer.
Best way is to #include <math.h> and use M_PI macro instead.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,821
Here's what's wrong:

Rich (BB code):
Date: 02/08/12 23:06
Is this 2002 Aug 12?
Feb 08, 2012?
02 Aug 2012?

Programmers should set the standard and write unambiguous code.
 

MrChips

Joined Oct 2, 2009
30,821
pi is not 22/7. This is a crude approximation for pi.
p = 4 *atan(1) is more precise.
If you wish to avoid using a transcendental function
355.0/113.0 is good to 6 decimal places.
Or just enter
pi = 3.14159265358;
but use #define pi 3.14159265358
 

kubeek

Joined Sep 20, 2005
5,795
Here's what's wrong:

Rich (BB code):
Date: 02/08/12 23:06
Is this 2002 Aug 12?
Feb 08, 2012?
02 Aug 2012?

Programmers should set the standard and write unambiguous code.
I am pretty sure that 02/08/12 meaning 02 Aug 2012 is the standard. Unfortunately not in the USA.
http://en.wikipedia.org/wiki/Date_format_by_country
And by the way the header looks like it is automatically generated, so don´t blame the programmer for this one.
 

Thread Starter

mkbutan

Joined Sep 30, 2008
299
hi
its working

Rich (BB code):
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
     int r;
     float a,pi;
     printf("Enter the Radius :\n");
     scanf("%d",&r);
     pi = 3.1416;
     a = (pi)*(r*r);
     printf("The Area of a Circle is :%f",a);
     getch();
     }
 

kubeek

Joined Sep 20, 2005
5,795
Since you´re using math.h you can change it to a= M_PI * r*r;
You don´t need the parenthesis around multiplication.
 
Top