Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1
- public static String reverseString(String s) {
- String reverse = "";
- for(int i = s.length() - 1; i >= 0; i--)
- {
- reverse = reverse + s.charAt(i);
- }
- return reverse;
- }
- // 2
- public static int sumDigits(long n){
- int temp = (int)Math.abs(n);
- int sum = 0;
- while(temp != 0){
- sum += (temp % 10);
- temp = temp / 10;
- }
- return sum;
- }
- // 3
- public static void runningSum(int[] arr) {
- for(int i=1;i<arr.length;i++) {
- arr[i]=arr[i-1]+arr[i];
- }
- }
- // 4
- public static int indexOfSmallest(int[] array){
- int value = 9999999;
- int index = 0;
- if(array.length > 0){
- for(int i = 0; i < array.length; i++){
- if(array[i] < value){
- value = array[i];
- index = i;
- }
- }
- return index;
- }
- else{
- return -1;
- }
- }
- // 5
- public static int countLarger(int [] array, int num){
- int count = 0;
- for(int i = 0; i < array.length; i++){
- if(array[i] > num){
- count++;
- }
- }
- return count;
- }
- // 6
- public static double average(double ... a){
- double ave = 0;
- double length = 0;
- for (double i : a){
- ave = ave + i;
- length++;
- }
- return ave/length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement