Posts

Showing posts with the label Range

In Java, Write And Read 100 Random Integers From File

Image
import java.io.PrintStream; public class Ex3{ public static void main(String[]args){ try(PrintStream out = new PrintStream(new java.io.File("numbers.txt"))){ int i = 100; while(0 < i --) out.println((int)(1+Math.random()*10000)); }catch(java.io.IOException e){ System.err.println(e.getMessage()); } } } Download

In Java, Generate Random Number Excluding Given Array Of Integers

Image
class RandNum { public static void main(String[] args) { System.out.println("Random: " + getRandom(33, 43, 47, 7, 11, 54, 21)); } public static int getRandom(int... numbers){ int rand, len; do{ rand = (int)(1+Math.random()*54); len = numbers.length; while(0 < len && rand != numbers[--len]); if(0 == len && rand != numbers[0]) return rand; }while(true); } } Download

In Python, Implementing Own Range() To Return List

Image
def myRange(start, stop = None, step = None): nums = None if None == step: step = 1 if None == stop: stop = start start = 0 if None != start and None != step: nums = [] while start < stop: nums.append(start) start += step return nums if '__main__' == __name__: print(myRange(3)) print(myRange(1, 7)) print(myRange(1, 7, 2)) print(myRange(-5, 1)) print(myRange(-5, 1, 2)) Download

Find all even numbers in given range with C programming

Image
#include<stdio.h> void main(){ int a, b; char c; printf("Enter a smaller number: "); fflush(stdin); scanf("%d", &a); printf("Enter a bigger number: "); fflush(stdin); scanf("%d", &b); if(a > b){ printf("%d is not smaller than %d\n", a, b); return; } c = 0; do{ if(0 == b%2){ if(!c){ printf("All even numbers: "); } printf("%c%d",c,b); c = ','; } b--; }while(a <= b); printf("\n"); if(!c){ printf("No even numbers found\n"); } } Download