Can anyone explain whats wrong with this program why this is not working.

void main(void)
{
long a,b=1;
clrscr ();
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a=0,1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
getch ();
}

Recommended Answers

All 18 Replies

line 7 is the problem.

a=0 is an assignment. a==0 is a conditional statement. you need to understand the difference. also, you can't test multiple conditions by throwing a comma between values. you need to fully express each condition, separated by an AND (&&) or an OR (||) as the logic you're trying to acheive requires.

so, what you are trying to say in line 7 should be if (a == 0 || a == 1) this is fundamental stuff. you need to review your early lessons on IF statements, conditional and assignment operators.

Can anyone explain whats wrong with this program

void main(void) - see this clrscr (); - very old non-standard function that hasn't existed since 1986 getch (); - non-standard function that is very non-portable, works in very few compilers
and no formatting making the program hard to follow.

@jephthah, thanks alot :)
but there is no such thing in my lesson how to give multiple values for IF

@WaltP , Our teacher teaching us from the basic and they told us to use such functions, i don't know any other function to use then these void main(void) and clrscr () & getch () , can you tell me which functions should i use istead of the functions above ?

@WaltP , Our teacher teaching us from the basic and they told us to use such functions, i don't know any other function to use then these void main(void) and clrscr () & getch () , can you tell me which functions should i use istead of the functions above ?

Then you are being seriously mistaught. The last version of Turbo C was ver 4 in 1993. That's 17 years ago. Many compilers have been following the changes in the standards and the industry since then. Maybe you should ask your instructors why they insist on using such old tools when they have been surpassed by many free compilers since then.

commented: u'r great :) +1

@jephthah, thanks alot :)
but there is no such thing in my lesson how to give multiple values for IF

well, you were certainly trying to do so! but you were doing it wrong. IF takes one conditional statement. the statement may have one or more values. you need to be able to deal with ANDs and ORs in your conditional statement, as you can see by your need to find the condition when 'a' equals either 0 or 1. to try and do this with only one value at a time, would be ridiculous

@WaltP , Our teacher teaching us from the basic and they told us to use such functions, i don't know any other function to use then these void main(void) and clrscr () & getch () , can you tell me which functions should i use istead of the functions above ?

i wasn't going to bother correcting this, since you've got more fundamental problems... but since Walt brought it up:

(1) always use int main (void) ... you will need to add a return 0; at the end of your main block. "void main" is wrong. it's undefined and can cause all sorts of problems in larger projects. any instructor that tells his student to do so, is immediately suspect of incompetence.

(2) you should never use "clrscr()". it's entirely unnecessary and obnoxious.

(3) use getchar() instead of getch().

(4) stop using the conio.h library altogether. use stdio.h and stdlib.h functions for what you need to do as far as printing text and getting command line input. fancy formatting in command terminals has been out of style since the mid 1990's. we should all stop pretending that the command terminal is a GUI.

Thanks..i too am an instructor(junior) i did not know this..getch() in particular

^ you mean you're a junior, as in a third-year undergraduate?


.

i am in my second year doing part time teaching..thanks for asking

Thanks alot Jephthah & WaltP i've learned alot today !!
though

We're learning Turbo C from the book,

The write Group's Trubo C Programming for PC and Turbo C++ Revised Edition by Robert Lafore

can you suggest me a better b0ok to learn modern C language instead of these all functions ?

and here is my program now,

void main(void)
{
long a,b=1;
clrscr ();
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a==0 || a==1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
getch ();
}

but there is still some problem, when i am writing 0 or 1 in the output , it is showing both the statements You entered 0 or 1 and Answer is 1 ..why is this happening so ? i want it to show only first statement when i enter 0 or 1

Thanks alot Jephthah & WaltP i've learned alot today !!
though

We're learning Turbo C from the book,


can you suggest me a better b0ok to learn modern C language instead of these all functions ?

When was that published? Looking for anything published in the last 5-8 years would be better.

i am in my second year doing part time teaching..thanks for asking

Then you need to relearn C/C++ and stop using ancient compilers that are not standard. Teach your students on current compilers (there are many free ones) and each them Standard C/C++, not all the old Borland/Turbo crap that's worthless in today's industry.

and here is my program now,

void main(void)
{
long a,b=1;
clrscr ();
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a==0 || a==1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
getch ();
}

but there is still some problem, when i am writing 0 or 1 in the output , it is showing both the statements You entered 0 or 1 and Answer is 1 ..why is this happening so ? i want it to show only first statement when i enter 0 or 1

No more help until you get rid if void main() , clrscr() , and you format your code. No longer wasting my time.

this is written on the book
1990 by The Waite Group, Inc.

so here is the code now without void main(void)

int main(void)
{
long a,b=1;
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a==0 || a==1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
return 0;
}

this is written on the book
1990 by The Waite Group, Inc.

20 years old? Never learn from a computer book older than yourself! Jeez! :icon_rolleyes:

so can you suggest me a book ??
and what about the program now ??
can you solve the problem now ?

When you don't use braces to delineate your if/else groupings only the statement immediately after the if or else gets executed:

if(condition)
   This would get executed when condition was true
A statement here would cause an error when an else is present
else
   This would get executed if condition is false
   {
       The stuff in these braces would be executed if the condition was false because it is with 
        the "This would get executed" statement 3 lines before
   }
This statement would be totally on its own, not part of the if or the else

So now you see why your output on line 13 was executing regardless.

I don't have a good text recommendation for you. I used to like Kelley/Pohl "A Book on C" but it's getting a little outdated and might be a bit too dense for folks just starting out. Their book "C By Dissection" (meant for beginners) is not very effective IMO.

I'll chide you one more time about the formatting too. Read the article WaltP linked in Post #3 (it's written by some guy WaltP at another site, probably just a coincidence). It's definitely hard to match your braces up and what goes with what when everything is crammed up against the left hand side like that. If it's easier to read it's easier to help you.

commented: great - xufyan +1

hey..! thanks alot :)

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.