473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

if-else statement

40
Just cant figure out how to formulate the correct syntax..I can do it in looping statement but my professor said that use only the simple if-else statement. The problem goes like this..Write a program that will determine if the number that you entered is either odd or even number..

I tried writing this code..

#include<stdio.h>
main()
{
int nos;
clrscr();
printf("Enter number:");
scanf("%d",&nos);
if(nos++){
printf("Odd number");
}
else {
printf("Even number");
}
getch();
}
Nov 28 '06 #1
16 5421
this is the simple way of finding odd or even

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(num % 2 == 0)
  3.     printf("Even\n");
  4. else
  5.   printf("Odd\n");
  6.  
  7.  
there are many different ways of finding odd and even. Depends upon the complexity of your program

main should return value, Have a look at this main

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    ---
  4.    ---
  5.    return 0;
  6. }
  7.  
by the way use code tags

ssharish
Nov 29 '06 #2
bitong
40
this is the simple way of finding odd or even

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(num % 2 == 0)
  3.     printf("Even\n");
  4. else
  5.   printf("Odd\n");
  6.  
  7.  
there are many different ways of finding odd and even. Depends upon the complexity of your program

main should return value, Have a look at this main

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    ---
  4.    ---
  5.    return 0;
  6. }
  7.  
by the way use code tags

ssharish
[quote=bitong]hello...i tried the code that you gave but still it didn't work..

[code]

#include<stdio.h>
int main()
{
int nos;
printf("Enter number:");
scanf("&d",&nos);
if(nos % 2 == 0)
printf("Even number\n");
else
printf("Odd number\n");
getch();
return 0;
}
Nov 29 '06 #3
DeMan
1,806 1GB
In your scanf function you have an & instead of a %

it should be: scanf("%d",&nos);
Nov 29 '06 #4
bitong
40
In your scanf function you have an & instead of a %

it should be: scanf("%d",&nos);
sori...typo error... =)

#include<stdio.h>
int main()
{
int nos;
printf("Enter number:");
scanf("%d",&nos);
if(nos % 2 == 0)
printf("Even number\n");
else
printf("Odd number\n");
getch();
return 0;
}

even though the code goes like that, still any number that I input it brings back the "Even Number" output...even if i entered 3 or 2....

=)
Nov 29 '06 #5
sivadhas2006
142 100+
sori...typo error... =)

#include<stdio.h>
int main()
{
int nos;
printf("Enter number:");
scanf("%d",&nos);
if(nos % 2 == 0)
printf("Even number\n");
else
printf("Odd number\n");
getch();
return 0;
}

even though the code goes like that, still any number that I input it brings back the "Even Number" output...even if i entered 3 or 2....

=)

Hi,

This code is working well.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. int main()
  4. {
  5.    int 
  6.       nos = 0;
  7.  
  8.    printf("Enter number:");
  9.    scanf("%d",&nos);
  10.  
  11.    if(nos % 2 == 0)
  12.       printf("Even number\n");
  13.    else
  14.       printf("Odd number\n");
  15.    getch();
  16.    return 0;
  17. }
  18.  
  19.  
Regards,
M.Sivadhas.
Nov 29 '06 #6
DeMan
1,806 1GB
In your print satement, you could add (temporarily of course) a printout of what the number is which might give a clue.
Expand|Select|Wrap|Line Numbers
  1. printf("Even Number %d", nos);
Given that it is coming even I would suspect that it is coming through 0, however I must admit the code worked for me.
Nov 29 '06 #7
bitong
40
Hi,

This code is working well.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. int main()
  4. {
  5.    int 
  6.       nos = 0;
  7.  
  8.    printf("Enter number:");
  9.    scanf("%d",&nos);
  10.  
  11.    if(nos % 2 == 0)
  12.       printf("Even number\n");
  13.    else
  14.       printf("Odd number\n");
  15.    getch();
  16.    return 0;
  17. }
  18.  
  19.  
Regards,
M.Sivadhas.
Tnx M.Sivadhas...it worked!! =)
Nov 30 '06 #8
bitong
40
Hi,

This code is working well.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. int main()
  4. {
  5.    int 
  6.       nos = 0;
  7.  
  8.    printf("Enter number:");
  9.    scanf("%d",&nos);
  10.  
  11.    if(nos % 2 == 0)
  12.       printf("Even number\n");
  13.    else
  14.       printf("Odd number\n");
  15.    getch();
  16.    return 0;
  17. }
  18.  
  19.  
Regards,
M.Sivadhas.
just want to ask whats the function or the purpose of placing % in the "if" statement ---> if(nos % 2 == 0)?
Nov 30 '06 #9
just want to ask whats the function or the purpose of placing % in the "if" statement ---> if(nos % 2 == 0)?
The purpose of placing the % is that ur asking for the remainder after the number is divided by 2. If the remainder is 0 then the number is even. e.g if you do 10 % 6 than that would give you 4.
Hope it helped
Nov 30 '06 #10
bitong
40
The purpose of placing the % is that ur asking for the remainder after the number is divided by 2. If the remainder is 0 then the number is even. e.g if you do 10 % 6 than that would give you 4.
Hope it helped
got it.. =) so it means that putting % in the conditional statement means that you're asking for the remainder of the problem? or its purpose varies depending on how you use it....
Nov 30 '06 #11
got it.. =) so it means that putting % in the conditional statement means that you're asking for the remainder of the problem? or its purpose varies depending on how you use it....
From what I know its the mod operator and so far I only know that you can use it for finding the reminder. Im not sure if it has any other uses. Maybe someone more knowledgeable can answer that question better.
= )
Nov 30 '06 #12
Banfa
9,065 Expert Mod 8TB
From what I know its the mod operator and so far I only know that you can use it for finding the reminder. Im not sure if it has any other uses. Maybe someone more knowledgeable can answer that question better.
= )
As an operator that is it's only use.

If I understand bitong correctly they are asking if % can only be used in an if

remembering that the part in the brackets of an if statement is just an expression % can be used anywhere you can validly put an expression.


Another often used method of finding if a number is even is to use the AND bitwise operator &

if (number & 1)
{
// It's odd
}
else
{
// It's even
}

This is not as portable as using the % operator but is usually more efficient as the % operator is one of the less efficient ones.
Nov 30 '06 #13
hi,

% in C++ means modulus, it means the remainder of the number you are asking for, so in your program he's asking for the remainder of zero to get an EVEN number so if your number divided by 2 is not zero then the answer should be ODD.

hope it might help??

regards
Nov 30 '06 #14
bitong
40
Hi,

This code is working well.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. int main()
  4. {
  5.    int 
  6.       nos = 0;
  7.  
  8.    printf("Enter number:");
  9.    scanf("%d",&nos);
  10.  
  11.    if(nos % 2 == 0)
  12.       printf("Even number\n");
  13.    else
  14.       printf("Odd number\n");
  15.    getch();
  16.    return 0;
  17. }
  18.  
  19.  
Regards,
M.Sivadhas.
hello...just want to ask another question...what is the purpose of putting int in the main() ----> int main()...and putting 0 in variable declaration? ---> int nos=0;
Dec 2 '06 #15
hi there,

i hope this might be of help.....

the function "main" like all function must state what kind of value it will return, it is not necessary to use int as always, if not appropriate, you can use " void main () " but mostly depending on your application,

When declaring Variables, C++ provides several different basic data types. The most important ones are:
double: decimal numbers between a smallest, negative one and a largest, positive one
int: a whole number between a smallest, negative one and a largest, positive one
char: a single letter or special symbol, anything that is on your keyboard


ok

regards,

mike
Dec 2 '06 #16
bitong
40
hi there,

i hope this might be of help.....

the function "main" like all function must state what kind of value it will return, it is not necessary to use int as always, if not appropriate, you can use " void main () " but mostly depending on your application,

When declaring Variables, C++ provides several different basic data types. The most important ones are:
double: decimal numbers between a smallest, negative one and a largest, positive one
int: a whole number between a smallest, negative one and a largest, positive one
char: a single letter or special symbol, anything that is on your keyboard


ok

regards,

mike
ok..thnx a lot =)
Dec 2 '06 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ryan Lowe | last post by:
i thought id ask here before wirting a PEP, if people thought it would be a good enhancement to allow if clauses in regular for-statements like so: >>> for x in y if x < 10 : is semantically...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
5
by: Angelina | last post by:
Hi, I need some help in writing a 'if-then-else' statement. I currently have wrote one that looks something like this.. If Combobox1 = xxx and textbox1 <> "" then 'run stored procedure 1 to...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
73
by: Martin Johansen | last post by:
if(a); In this code, towhat type does the if statement cast the variable "a" to on comparizon?
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
2
by: William | last post by:
The script below runs correctly in ASP but not ASPX. Im not sure why? Please help. Description: An error occurred during the compilation of a resource required to service this request. Please...
8
by: fredda054 | last post by:
Hi everybody ! I have a little repeater/hyperlink issue I'm not sure how to solve. I use a repeater to list subjects available at a specific school. The datasource of this repeater is a...
0
by: Benny Ng | last post by:
Hi,All, When i deploy Enterprise library with my application ,i used XCOPY to deploy it into my test server. But when application runs, shown some error related registry. (But actually I haven't...
4
by: William | last post by:
Does anyone know what is causing the error below. The following scipt runs in .asp but not .aspx??? Please help. Description: An error occurred during the compilation of a resource required to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.