Question:
whats is the logical error in this program made in turbo C ?
?
2012-05-22 02:42:21 UTC
include
#include
void main()
{ int a;
clrscr();
printf("******This Program Check Whether The Given No. Is EVEN/ODD********* \n");
printf("Pls Enter The last Digit Of The Number You Wish To Check \n");
scanf("%d",&a);
if(a=='0')
if(a=='2')
if(a=='4')
if(a=='6')
if(a=='8')
{
printf("the given number is even");
}
else
{
printf("the given number is odd");
}
getch();

}
Six answers:
AnalProgrammer
2012-05-22 02:57:33 UTC
The logical error is

if(a=='0')

if(a=='2')

if(a=='4')

if(a=='6')

if(a=='8')



That all these if's together are and's. So because a cannot be 0 and 2 and 4 and 6 etc at the same time the number will always fail the even test.

Also the else only applies to the last if so no message will ever be displayed.



Have fun.
roger
2012-05-22 07:53:46 UTC
two errors

scanf("%d",&a);

if(a=='0')

if(a=='2')

if(a=='4')

if(a=='6')

if(a=='8')

to get here a has to be equal to the ascii codes for 0 and 2 and 4 and 6 and 8

it cannot be equal to all of them.

second you are comparing a to the ASCII codes for 0 2 ...

not the value 0 or 2 ...



drop the '

if(a==0 || a==2|| a==4||a==6|| a==8)



a better way is to say

if(a%2==0)



or

if((a&1)==0)
Dbz fan
2012-05-22 03:50:42 UTC
First it wont compile

Notice this if(a=='0'),if(a=='1'),if(a=='2') hv written numbers in single quotes . use single quotes only for character not integer.

And the logical error is than if u r last digit is suppose '0' then it will check if it is '2' '4' etc. which is clearly wrong.u r program should check each condition independent of others.

U can use 'switch' if u dont want to change the basic logic of u r program

If u r ready to change the basic logic use OR example:if(a==0 || a==1 || ...etc)
ankit s
2012-05-26 04:29:40 UTC
logical error is that u have used teo many if in this program you should use if else statement or use

if(a=='0'||a=='2'||a=='4'||a=='6'||a=='8')

or use can use

if a%2==0 for even nos

like

if(a%2==0)

{

printf("Number is even");

}

else

printf("no is odd");
Forgotten Warrior
2012-05-22 02:47:37 UTC
you can use all the conditions in one If like following

if(a==0 || a==2 || a==4 || a==6 || a==8)

{

printf("the given number is even");

}





Also when ever comparing Numbers dont use commas just use the number itself..

If u need any more help please contact me i m online yahya_takamul@yahoo.com
trevathan
2016-10-24 12:48:40 UTC
Runtime Vs assemble time blunders in C# the version between assemble time and run time is an celebration of what pointy-headed theorists call thephase huge huge difference. it really is between the hardest recommendations to study, particularly for people without a lot history in programming languages. To mind-set this problem, i stumble upon it effective to ask What invariants does this technique fulfill? What can flow incorrect in this section? If the section succeeds, what are the postconditions (what do all of us recognize)? What are the inputs and outputs, if any? assemble time this technique choose no longer fulfill any invariants. in reality, it needn’t be a properly-formed software in any respect. you need to feed this HTML to the compiler and watch it barf… What can flow incorrect at assemble time: Syntax blunders Typechecking blunders (hardly ever) compiler crashes If the compiler succeeds, what do all of us recognize? this technique became properly formed—a significant software in even with language. It’s a chance to commence operating this technique. (this technique may fail in the present day, yet a minimum of we may be able to attempt.) What are the inputs and outputs? enter became this technique being compiled, plus any header files, interfaces, libraries, or different voodoo that it had to import as a way to get compiled. Output is with any success assembly code or relocatable merchandise code or perhaps an executable software. Of if something is going incorrect, output is a bunch of blunders messages. Run time all of us recognize no longer something about this technique’s invariants—they are even with the programmer put in. Run-time invariants are hardly ever enforced by utilizing the compiler on my own; it desires help from the programmer. What can flow incorrect are run-time blunders: branch by utilizing 0 Deferencing a null pointer operating out of memory also there must be blunders that are detected by utilizing this technique itself: attempting to open a document that isn’t there attempting come across a internet web site and learning that an alleged URL isn't properly formed If run-time succeeds, this technique finishes (or keeps going) without crashing. Inputs and outputs are completely as a lot because the programmer. files, living house windows on the demonstrate screen, community packets, jobs despatched to the printer, you call it. If this technique launches missiles, that’s an output, and it takes position only at run time


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...