Posts

Showing posts with the label Even

Fill 100 random integers in array and count even and odds in array with Java

Image
public class ArrayTest{ public static int[] createArray(){ int[]arr = new int[100]; for(int i = arr.length-1; 0 <= i; i --){ arr[i] = (int)(Math.random()*100); } return arr; } public static void statsDisplay(int[] intArray){ int evens = 0, odds = 0, sum = 0; for(int n: intArray){ sum += n; if(0 == n%2){ evens ++; } else { odds ++; } } double avg = sum/intArray.length*1.0; System.out.printf("evens: %d, odds: %d, avg: %.2f%n", evens, odds, avg); } public static void main(String[]args){ statsDisplay(createArray()); } } 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