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();
}
16 5444
this is the simple way of finding odd or even -
-
if(num % 2 == 0)
-
printf("Even\n");
-
else
-
printf("Odd\n");
-
-
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 -
int main()
-
{
-
---
-
---
-
return 0;
-
}
-
by the way use code tags
ssharish
this is the simple way of finding odd or even -
-
if(num % 2 == 0)
-
printf("Even\n");
-
else
-
printf("Odd\n");
-
-
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 -
int main()
-
{
-
---
-
---
-
return 0;
-
}
-
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;
}
DeMan 1,806
Top Contributor
In your scanf function you have an & instead of a %
it should be: scanf("%d",&nos );
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....
=)
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. -
-
#include<stdio.h>
-
int main()
-
{
-
int
-
nos = 0;
-
-
printf("Enter number:");
-
scanf("%d",&nos);
-
-
if(nos % 2 == 0)
-
printf("Even number\n");
-
else
-
printf("Odd number\n");
-
getch();
-
return 0;
-
}
-
-
Regards,
M.Sivadhas.
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. - 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.
Hi,
This code is working well. -
-
#include<stdio.h>
-
int main()
-
{
-
int
-
nos = 0;
-
-
printf("Enter number:");
-
scanf("%d",&nos);
-
-
if(nos % 2 == 0)
-
printf("Even number\n");
-
else
-
printf("Odd number\n");
-
getch();
-
return 0;
-
}
-
-
Regards,
M.Sivadhas.
Tnx M.Sivadhas...it worked!! =)
Hi,
This code is working well. -
-
#include<stdio.h>
-
int main()
-
{
-
int
-
nos = 0;
-
-
printf("Enter number:");
-
scanf("%d",&nos);
-
-
if(nos % 2 == 0)
-
printf("Even number\n");
-
else
-
printf("Odd number\n");
-
getch();
-
return 0;
-
}
-
-
Regards,
M.Sivadhas.
just want to ask whats the function or the purpose of placing % in the "if" statement ---> if(nos % 2 == 0)?
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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
|
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 ***/
|
by: Martin Johansen |
last post by:
if(a);
In this code, towhat type does the if statement cast the variable "a" to on
comparizon?
| |
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
|
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:
|
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).
|
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
|
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'.
<%
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |