473,387 Members | 1,779 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,387 software developers and data experts.

algorithm to find first n odd numbers

write algorith to display first n odd numbers ?
Jan 20 '13 #1
8 18451
weaknessforcats
9,208 Expert Mod 8TB
Forget the display, do you know how to test if a number is odd?
Jan 20 '13 #2
donbock
2,426 Expert 2GB
What exactly does first mean? I assume that you are only concerned with positive odd numbers, so the first odd number is 1, and you want to follow it with the smallest odd number that is greater than 1.

What exactly are the constraints on your assignment? There are two ways to proceed: step through the integers, testing each for odd; or start from a known first odd number and repeatedly construct the next odd number. The latter approach might be considered cheating ... depending on the precise rules (or constraints) on the assignment.
Jan 21 '13 #3
We use % operator to get reminder.. and if odd number is divided by 2 we get reminder 1

Algorithm
1 Start
2 Enter max
3 for(i=0;i<mx;i++)
if(i%2==1)
printf(i)
end if
end for
4 stop
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.       int max,i;
  7.       printf("Enter value of max");
  8.       scanf("%d",&max);
  9.       printf("Odd Numbers : \n");
  10.       for(i=0;i<=max;i++)
  11.        {
  12.            if(i%2==1)
  13.              printf("%d",i);
  14.        }
  15.  
  16. }
  17.  
Jan 24 '13 #4
whodgson
542 512MB
swapnali143 Why wouldn't you:

Expand|Select|Wrap|Line Numbers
  1. unsigned int a;
  2. for(a=1;a<=INT_MAX;a+=2)
  3. cout<<a<<" ";
...or would that be cheating as donbock suggests.
Jan 31 '13 #5
donbock
2,426 Expert 2GB
@whodgson, I don't let my programs go near limit values like INT_MAX because here there be dragons. The loop only terminates when a is larger than INT_MAX. If a is an int then that can't ever happen. If a is a long then it is implementation-dependent whether that will ever happen. Changing the termination condition to a<INT_MAX will work ... but only if INT_MAX is odd.

The OP wants the first n odd numbers. Safer to terminate the loop by counting down on n.
Jan 31 '13 #6
Dear whodgson
As a fresher to C or CPP language its better and clear for Anirudh to get logic and understand it.....
Jan 31 '13 #7
Banfa
9,065 Expert Mod 8TB
@whodgson and swapnali143 both algorithms are valid and reasonable ways to solve this problem. It is not possible to say which is better in this instance because the OP has not given enough detail in their description of the problem. It is entirely possible that for their purposes either would do but it is also possible that they were supposed to be making use of a particular operator (% for example).

@whodgson donbock has already highlighted the INT_MAX issue in your code and I will say little further except that this is not the only way loop end tests can go wrong and it is always worth ensuring that your loops can actually end having performed the correct number of iterations for all input values to the algorithm.

@swapnali143 you really should write main as returning int and avoid using conio.h. You do not know what platform the viewers of this forum use so you need to assume that it is a conforming platform with no extensions. main returning void is an extension in some C compilers and should be avoided conio.h is a extension header on some platforms and should also be avoided. Writing properly portable code is a skill that is worth developing.
Jan 31 '13 #8
1.start
2. Read n
3.intialize i=1 repet loop n times
4.inside loop just print i and increment i by 2
5.stop.
Feb 1 '13 #9

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

Similar topics

2
by: Neal D. Becker | last post by:
What is an efficient way to find the first element in a sequence meeting some condition? For example, the first number > x in a list of numbers.
5
by: Robert Bralic | last post by:
Dear, I weated a simple program, he is compiled with gcc, and generates prim numbers, with fastes method that I find.He is based on divide with generated prim numbers... At end he gives time...
16
by: a | last post by:
We are writing an app that assigns people to teams based on their curent score. Teams are 8 people, there are 2 teams. (i would like it to be flexible, but this is a start). I need an algorithm...
26
by: Martin R | last post by:
Hi, How to find first not null value in column whitout chacking whole table (if there is a not null value then show me it and stop searching, the table is quite big)? thx, Martin *** Sent...
11
by: gchiazx | last post by:
Hi, can someone send me the algorithm for finding the products of prime numbers from a given number? Thank You Gary.
6
by: tshad | last post by:
I have a filename that I want to extract the 1st set of numbers up to either a ".", "-", "_" or "~" and make that an int. Or I guess easier just take all the values that are 0-9 up to the 1st non...
3
by: pchaitanya | last post by:
I have selected some list of valid dates to a label. now i need to find first day among the given dates from label contrl i got dates from calender control by clicking for entire week.. ...
5
by: paragpdoke | last post by:
Hello All. I am new to SQL ... trying to make a query work untimately in Perl (v5.10.0 built for MSWin32-x86-multi-thread) against MySQL hosted remotely (which currently I don't know the version...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.