write a program that will accept ten positive integers and output the lowest even integer.
come on are you trying get your homework done? or do u jus need idea to do it? in what language do u need it
This sounds like Homework.... but telling us the language your programming it in would be help
i'm writing dis in C programming langauage,hope this code works main() { Int a[10],b[10],min=0,I,j; Printf(“\nEnter 10 integers;only positive”); For(i=0;i<n;i++) Scanf(“%d”,&a[i]); for(i=0;i<n;i++) { if(a[i]%2==0) { for(i=0;i<n;i++) a[i]=b[i]; } } b[0]=min; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(min>b[j]) min=b[j]; } } printf(“\nThe least even integer of the array is %d”,min); }
is it not working?
what ever language your using the idea will be the same. set each input to a variable then modulus divide each variable by 2, whichever one returns 0 is even. than take the even ones and use if then to see which one is the smallest. (ie: if x < y and x < z than print x else ...)
thats wt i have written in c programming code?!
sorry didnt mean to offend. i dont really know C yet.
i'm just asking whether its right or not....
oh i have no idea.
in python: >>from sys import argv >>print min(filter(lambda x: not x & 1, [int(x) for x in argv])) this is a kinda fun version: the filter is a built in function for filtering lists. lambda is an anonymous function the & is doing a bitwise binary 'and' with <31,0s>1, basically looking at the last bit which determines even or odd, or more specifically the state of the 2^0 bit. int() converts a string to an int and argv is a vector of the program arguments
@sriramkumar, no, your code will not work, you leave n and j undefined and write some function names incorrectly (starting with a capital letter).
And for the actual question, there are several ways, depending on what language you use and what kind of functions it offers. The way sriramkumar did it in C is ok, the code might not work as it is, but logic-wise it's fine. Here's a semi-smart way that tries to minimize the amount of work done by the computer: Set a variable to some unique value (maybe null, depending on the language) that designates that we haven't got an even number from the user yet. This variable will store our final answer. Loop: read a value from the user, make sure it's valid (a positive integer) and increment a counter that keeps track of the total number of inputs. For every value given by the user, check if the number is even. If it's odd, discard it and ask for the next one. When you get an even number, check if the variable I talked about earlier is still unset (or null or whatever you have chosen). If it is, this is our first even number, so set it to the number you got from the user and move on. If the variable is set, meaning that we've already got at least one even number from the user, compare the new number to the older one. If the new one is smaller, assign it to the variable and move on to the next one, until the user has supplied 10 numbers (or any count you choose). This is a design that does not require looping through all the values or using arrays.
I would use an array to store the 10 integers, a separate integer instance, and a boolean to store detection of positive numbers. Then I would write a loop that would bop through the array, grab the first even number in the list and set my integer equal to that number. In Java the subroutine contents would look like this: int intList[] = new int[]; int lowNum; boolean foundPositive = false; for (int i =0; i<10; i++) { if(intList.get(i)%2 == 0) // detect an even number, ignores odds { if(foundPositive == false) // detects first positive number { foundPositive = true; lowNum = intList[i]; } else // for second and subsequent positive numbers { if(intList[i]<lowNum) // current even int < the lowest even int found { lowNum = intList[i]; //reset lowNum to lowest even detected } } } if(foundPositive == false) // If no evens were found throw an exception { throw new IllegalArgumentException ("No even integers found"); } return lowNum; To implement in C and C++ the only things you should have to change would be the array declaration syntax and the exception handling at the end. Good luck.
Powershell: function lei($x) { ($x | sort | where {$_ % 2 -eq 0})[0] }
Join our real-time social learning platform and learn together with your friends!