Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Created December 14, 2017 08:32
Show Gist options
  • Save Kishy-nivas/6b2616976deb377d0edb31d75d2fa85a to your computer and use it in GitHub Desktop.
Save Kishy-nivas/6b2616976deb377d0edb31d75d2fa85a to your computer and use it in GitHub Desktop.
basic java program(for college purpose, please don't read this code )
import java.io.*;
import java.util.*;
class Ex1
{
private String container;
private int a,b,temp;
private int[] arr;
Scanner sc;
private void print_str (String val)
{
System.out.print(val);
}
private void print_num(int num)
{
System.out.print(num);
}
private void print_ln(){ System.out.println();}
Ex1()
{
}
Ex1(int a,int b)
{
this.a =a;
this.b= b;
}
Ex1(int size)
{
this.arr = new int[size];
}
public void init_array()
{
sc= new Scanner(System.in);
System.out.println("Enter the array numbers: ");
for(int i=0;i<this.arr.length;i++)
{
this.arr[i]=sc.nextInt();
}
}
private void swap(int i,int j)
{
int temp;
temp =j;
j=i;
i=j;
}
public int gcd(int a,int b)
{
int r;
if (b>a)
swap(a,b);
while(b>0)
{
r= a%b;
a=b;
b=r;
}
return a;
}
public void to_upper_method(String upper)
{
System.out.println(upper.toUpperCase());
}
public int lcm(int a, int b)
{
return (a * b)/this.gcd(a,b);
}
public void armstrong(int start,int end )
{
int temp,ans=0,a,val;
for (int i=start;i<=end;i++)
{
temp=i;
val =i;
while(val>0)
{
a= val%10;
val=val/10;
ans+=(a*a*a);
}
if(ans==temp)
System.out.println(temp +"is a armstrong number");
ans=0;
}
}
public void is_leap_year(int year)
{
String ans;
if (year % 4==0)
{
ans = "Leap year";
}
else
{
ans= "Not a leap year";
}
print_str(ans);
}
public int sum_of_arrays()
{
int sum =0;
for(int i=0;i<arr.length;i++)
sum+= arr[i];
return sum;
}
public void count_pos_neg()
{
int pos =0;
int neg=0;
for(int i=0;i<arr.length;i++)
{
if (arr[i]<0)
neg++;
else
pos++;
}
System.out.println("positive numbers : "+pos);
System.out.println("Negative numbers :" +neg);
}
public void max_of_three(int val1,int val2,int val3)
{
System.out.println("max_of_three");
System.out.println( "max value is " + Integer.max(Integer.max(val1,val2),val3));
}
public void sum_and_average()
{
print_str("sum : ");
print_num(this.a + this.b );
print_ln();
print_str("average: ");
print_num((this.a + this.b)/2);
print_ln() ;
}
public void is_even_or_odd(int a)
{
if (a %2 ==0)
System.out.println("Even ");
else
System.out.println("Odd");
}
}
public class Main{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
Ex1 G = new Ex1();
int choice ;
String menu = "0.Quit \n 1.Hello world \n 2.different types \n 3.sum and average of two numbers numbers \n 4.Even or odd \n 5.Leap year or not \n 6.largest of three numbers \n 7. swap two numbers \n 8.while loop \n 9.do loop \n 10.sum of all digits \n 11.armstrong \n 12. square,cube,square root of a number \n 13. uppercase/lowercase \n 14.permutation \n 15.positive and neagtive numbers \n 16.mean \n 17. compound interest \n 18.flyod triangle \n 19.Gcd \n 20.Lcm";
int val1,val2,val3,temp;
do
{
System.out.println(menu);
choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Hello world ");
break;
case 2:
break;
case 3:
System.out.println("Enter two numbers");
val1 = sc.nextInt();
val2 = sc.nextInt();
Ex1 e3= new Ex1(val1,val2 );
e3.sum_and_average();
break ;
case 4:
System.out.println("Enter a number: ");
val1 = sc.nextInt();
G.is_even_or_odd(val1);
break;
case 5:
System.out.println("Enter the year :");
int year = sc.nextInt();
G.is_leap_year(year);
break;
case 6:
System.out.println("Enter three numbers");
val1 =sc.nextInt();
val2 = sc.nextInt();
val3 = sc.nextInt();
G.max_of_three(val1,val2,val3);
break;
case 10:
System.out.println("Enter the size of the array");
temp = sc.nextInt();
Ex1 e10 = new Ex1(temp);
e10.init_array();
System.out.println(e10.sum_of_arrays());
break;
case 11:
System.out.println("Enter start and end range ");
val1 = sc.nextInt();
val2 = sc.nextInt();
G.armstrong(val1,val2);
break;
case 15:
System.out.println("Enter the size of the array : ");
temp = sc.nextInt();
Ex1 e15= new Ex1(temp);
e15.init_array();
e15.count_pos_neg();
break;
case 19:
System.out.println("Enter two numbers : ");
val1 = sc.nextInt();
val2 = sc.nextInt();
System.out.println("Gcd: " +G.gcd(val1,val2));
break;
case 20:
System.out.println("Enter two numbers : ");
val1 = sc.nextInt();
val2 = sc.nextInt();
System.out.println("Lcm: "+ G.lcm(val1,val2));
break;
default:
System.out.println("Not yet implemented ");
break;
}
}while(choice!=0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment