Printing odd numbers up-to a given number

 import java.util.*;


public class PrintOddNum{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no up to which odd no is to be printed");
int n = sc.nextInt();
System.out.print("List of odd is :");
for(int i=0; i<=n; i++){
if(i%2 != 0){
System.out.println(i);
}
}
}
}

Printing factor of two numbers up to 100

 import java.util.*;


public class PrintFactor{

public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number");
int a = sc.nextInt();
System.out.println("Enter the Second number");
int b = sc.nextInt();
System.out.println("No divisible by "+a);
for(int i=1; i<=100; i++){
if (i%a == 0){
System.out.print(i+",");
}

}
System.out.println();

System.out.println("No divisible by "+b);
for(int i=1; i<=100; i++){
if (i%b == 0){
System.out.print(i+",");
}
}
System.out.println();

System.out.println("No divisible by "+a +","+b);
for(int i=1; i<=100; i++){
if (i%a == 0 && i%b ==0){
System.out.print(i+",");
}
}
}
}

Printing current time and date

 public class PrintCurrentTime {

public static void main(String[] args){
System.out.format("\nCurrent Date time: %tc%n\n", System.currentTimeMillis());
}
}

Swapping

 import java.util.*;


public class swapping{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");

int a = sc.nextInt();
System.out.println("Enter Second number");
int b = sc.nextInt();
System.out.println("Before swapping : a, b = "+a+","+b);
//swapping
int temp = a;
a = b;
b= temp;
System.out.print("After swapping : a, b = "+a+","+b);
}
}

Spiral Matrix

 

import java.util.*;
public class ArraysSpiral {
public static void main(String args []){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();

int matrix[][] = new int[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
matrix[i][j] = sc.nextInt();
}
}

System.out.println("The spiral Order Matrix is:");

int rowStart = 0;
int rowEnd = n-1;

int colStart = 0;
int colEnd = m-1;
//to print spiral matrix
while(rowStart <= rowEnd && colStart <= colEnd){
//1
for(int col=colStart; col<=colEnd; col++){
System.out.print(matrix[rowStart][col] + " ");
}
rowStart++;

//2
for(int row=rowStart; row<=rowEnd; row++){
System.out.print(matrix[row][colEnd] + " ");
}
colEnd--;

//3
for(int col=colEnd; col>=colStart; col--){
System.out.print(matrix[rowEnd][col] + " ");
}
rowEnd--;

//4
for(int row=rowEnd; row>=rowStart; row--){
System.out.print(matrix[row][colStart] + " ");
}
colStart++;

System.out.println();
}
}
}

Searching the index of an element in a given array


 import java.util.*;
public class Arrays2 {

public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array");
int size = sc.nextInt();
System.out.println("Enter the element of array");
int numbers[] = new int[size];
//input
for(int i=0; i<size; i++){
numbers[i] = sc.nextInt();
}
System.out.println("Enter the number to be found");
int x = sc.nextInt();
//output
for(int i=0; i<numbers.length; i++){
if(numbers[i] == x){
System.out.print("x is found at index :"+i);
}
}
}
}

Printing sum of two numbers

 import java.util.*;

public class Sum2Num{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b;
System.out.print("The sum of a and b is :" + sum);
}
}

Printing the table of a given number

 import java.util.*;


public class Table{
public static void main(String args[]){

Scanner sc = new Scanner(System.in);
System.out.print("Enter the number");
int n = sc.nextInt();
for(int i=1; i<=10; i++){
System.out.println(n+"*"+i+"="+n*i);
}
}
}

Printing the division result of two numbers.

 import java.util.*;



public class printDiv{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int div = a / b;
System.out.print("The division of a by b is:" + div);
}
}

Calculating radius and area of a circle.

 import java.util.*;


public class printCir{
public static void main(String args []){

Scanner sc = new Scanner(System.in);
double Radius = sc.nextDouble();
//double perimeter = 2*Math.PI*Radius;
//double area = Math.PI*Radius*Radius;
System.out.println("Perimeter of circle is :"+2*Math.PI*Radius);
System.out.print("Area of Circle is :"+Math.PI*Radius*Radius);
}
}

Calculating area and perimeter of a Rectangle

 import java.util.*;


public class printRect{
public static void main(String args []){

Scanner sc = new Scanner(System.in);
double Width = sc.nextDouble();
double Height = sc.nextDouble();
System.out.println("Perimeter of Rectangle is :"+2*(Width+Height));
System.out.print("Area of Rectangle is :"+Width*Height);
}
}

Calculations using BODMAS

 import java.util.*;



public class printOper{
public static void main(String args[]){
System.out.println(-5+8*6);
System.out.println((55+9)%9);
System.out.println(20+(-3*5)/8);
System.out.println(5+15/3*2-8%3);
}
}

Multiplication of two numbers

 import java.util.*;



public class printMul{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number");
int a = sc.nextInt();
System.out.println("Enter the second number");
int b = sc.nextInt();
int mul = a * b;
System.out.print("The result of a x b is:" + mul);
}
}

Printing pattern Java

 public class printJava{

public static void main(String args[]){

System.out.println(" J a v v a ");
System.out.println(" J a a v v a a");
System.out.println("J J aaaaa V V aaaaa");
System.out.println(" JJ a a V a a");
}
}

Printing sum of user given input

 import java.util.*;



public class printSum{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b;
System.out.print("The sum of a + b is:" + sum);
}
}

Defining and Printing Array

 

import java.util.*;
public class Arrays {
public static void main(String args[]){
// int [] marks = new int[3];
// int marks [] = new int[3]; //This is also a valid syntax
int marks[] ={97, 98, 95};
// marks[0] = 97; //physics
// marks[1] = 98; //chemisty
//marks[2] = 95; // maths

//System.out.println(marks[0]);
//System.out.println(marks[1]);
//System.out.println(marks[2]);

for(int i=0; i<3; i++){
System.out.println(marks[i]);

}
}
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...