473,666 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if-else statement

40 New Member
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 5444
ssharish
13 New Member
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 New Member
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 Top Contributor
In your scanf function you have an & instead of a %

it should be: scanf("%d",&nos );
Nov 29 '06 #4
bitong
40 New Member
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 New Member
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 Top Contributor
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 New Member
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 New Member
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
MariyaGel
7 New Member
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

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

Similar topics

4
2518
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 equivalent to >>> for x in : but is shorter and easier to read. basically, the if works as a filter for
2
11854
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 error message and stop the process every time someone loads the script after the table is created because that would mean the page could only ever run once which of course not the solution I was looking for. I simply want to know how I can check...
5
1748
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 obtain the relevant records and place them in a datagrid. elseif combox1 = yyy and textbox2 <> "" or textbox3 <> "" then
145
6263
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 programs: /*** a.c ***/
73
2725
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
32301
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 the playlist file is empty so that I can assign an integer value to a playlist id field if it is the first playlist being written to the file....can anyone help?? Thanks in advance wetherbean
2
4774
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 review the following specific error details and modify your source code appropriately. Compiler Error Message: BC30081: 'If' must end with a matching 'End If'. Source Error:
8
3162
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 dataset filled with info from a SQL Server database. All the subjects are displayed as hyperlinks, and when a user clicks a subject, it opens up a new browser with the website specific to that subject (ie the specific department website).
0
2630
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 any method or component to wrote registry) After I used "InstallUtil" to registry Enterprise Library (those DLLs) to the deployment server, the application runs smoothly. Now I wonder why i need to registry those Enterprise Library DLLs into
4
4039
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 service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: BC30081: 'If' must end with a matching 'End If'. <%
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8552
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.