Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi , I'm beginner in C programming , i just want to know why do we code programmes that detect odd or even numbers and what is the use of that , give examples if possible , thanks !

What I have tried:

C++
#include<stdio.h>
#include<stdlib.h>

main()
{
	int N,i,sum,odd,even;
	system("color a ");
	printf("give a number : ");
	scanf("%d",&N);
	sum=0;
	even=0;
	odd=0;
	for(i=0;i<=N;i++)
	{
		if(i%3!=0&&i%4!=0)
		{
		sum=sum+i;
		if(i%2==0)
		even+=i;
		else
		odd+=i;
		}
	}
	printf("sum odd = %d \n sum even = %d \n sum all = %d",odd,even,sum);
	
	return 0;
}
Posted
Updated 20-Oct-17 12:00pm
v2
Comments
jeron1 20-Oct-17 12:34pm    
Off topic but what's the purpose of
if(i%3!=0&&i%4!=0)
?
Outh_Mane 20-Oct-17 15:35pm    
Yeah that was just a puzzle i found it on a website , i was learning loops ^^ .
jeron1 20-Oct-17 16:03pm    
Whew! so it's not fear of multiples of 12. ;)
Outh_Mane 20-Oct-17 18:11pm    
haha didn't think about that :o

Loads of things!
In the UK, odd number houses are often on one side of the street, even ones on the other.
16 bit values often have to to be stored on addresses starting with an even number (this is called word alignment) - if you use a pointer that addresses an odd number it can give problems.
If an integer is odd, it can't be divided by two - or if you do, you can't multiply it by two and get the same number you started with.

There are countless examples of why you might want to know is something is odd or even, but we aren't here to do your homework!
 
Share this answer
 
Only one example more.

Imaging a e.g. chemical process:
a.) A machine has an enter port "A" and an intermediate point "I".
b.) To process the material it will be moved from "A" to "I" and back.
c.) You can load/unload the machine only on the enter port "A"

Request : The material needs to be processed for a certain time T at a speed V
Question: What is the optimum no of cicles that at the end the material is on port "A" to unload it. Even counts here

Real (but most probably Abstract) example: Jigger Dyeing Machine | Dyeing Process with Jigger Dyeing Machine | Advantages of Jigger Dyeing Machine | Disadvantages of Jigger Dyeing Machine - Textile Learner[^]
 
Share this answer
 
Comments
[no name] 20-Oct-17 14:41pm    
So the downvoter did not understood the example ;P.
And yes the comment is that brave ;P ;P
I had to do that just the other day. We have a series of LASERs and cameras that looking at where they are aimed. We make their IP addresses correspond to which side of the target object they are on - odds are on the left, even on the right. I determine where to place their data based on which side they are on and I get that from a byte in the data stream that holds the last octet of their IP address.

This kind of thing is really quite common.
 
Share this answer
 
Comments
Rick York 20-Oct-17 17:42pm    
To expand on this is a little, given the last octet of the address, the camera's side is found from lastOctet % 2 and its position in the side is (lastOctet/2)-1 since we start at position 1.
It is also used in Haar Wavelets:
Haar wavelet - Wikipedia[^]
 
Share this answer
 
Quote:
I'm beginner in C programming , i just want to know why do we code programmes that detect odd or even numbers and what is the use of that

The primary reason of the toy problems you are solving, is to have you practice on programming and analyze.
The practical usage of a given problem is secondary.

Learn to indent properly your code, it show its structure and it helps reading and understanding.
C++
#include<stdio.h>
#include<stdlib.h>

main()
{
    int N,i,sum,odd,even;
    system("color a ");
    printf("give a number : ");
    scanf("%d",&N);
    sum=0;
    even=0;
    odd=0;
    for(i=0;i<=N;i++)
    {
        if(i%3!=0&&i%4!=0)
        {
            sum=sum+i;
            if(i%2==0)
                even+=i;
            else
                odd+=i;
        }
    }
    printf("sum odd = %d \n sum even = %d \n sum all = %d",odd,even,sum);

    return 0;
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900