Skip to content

Instantly share code, notes, and snippets.

View gaurav1780's full-sized avatar

Gaurav Gupta gaurav1780

  • Macquarie University
  • Sydney
View GitHub Profile
public static int totalEven(int[] a) {
int result = 0;
for(int i=0; i < a.length; i++) {
if(a[i] % 2 == 0) { //curent number IS even
result+=a[i]; //add it to the total
}
}
return result;
}
public class NeedForSpeed {
public static void main(String[] args) {
double distance = 34;
double time = 21;
double speed = distance/time;
System.out.println(speed);
}
}
int result = -3;
for(int i=1; i <= 7; i++) {
if(i%2 == 1) { //is i an odd number?
result = result + i;
}
}
int a = 5, b = 10;
int result = 0;
if(a == b) { //check for equality
result = a;
}
else {
b = b - a;
if(a == b) {
result = b;
}
int a = 5;
int b = 10;
int result;
if(a < b && b % a == 0 && a % 2 == 0) { //b should be divisible by a and a should be even
result = a;
}
else {
result = b;
}
int a = 4, b = 12, c = 3;
boolean d = false;
int result = 4;
if(a > b || b % c == 0) {
if(d == true) {
result = result + 1;
}
else {
result = result - 1;
}
int a = 4, b = 12, c = 3;
boolean d = false;
int result = 4;
if(a < b || b % c == 1) {
//a < b: false, b%c==0: true, false || true: true
if(d == true) { //false == true: false
result = result + 1;
}
else { //else block executes
result = result - 1; //result becomes 4 - 1 = 3
public class Client {
public static int foo(int a) {
return a*a;
}
public static void main(String[] args) {
int result = foo(foo(3));
}
}
int a = 10, b = 20;
int temp = a;
a = b;
b = temp;
/*
change the values of fib1 and fib2 such
that fib1, fib2 occur in fibonacci sequence.
the number of "petals" you'll notice
is fib1+fib2
*/
float fib1 = 8.0;
float fib2 = 13.0;