Write a Program to Print the Pattern

___/\
__/    \
_/        \
_\        /
__\    /
___\/

 

class Barfi
{
void main()
{
for(int i=1;i<=3;i++)
{
for(int j=2;j>=i;j–)
{
System.out.print(” “);
}
System.out.print(“/”);
for(int k=i;k<=(i-1)*3;k++)
{
System.out.print(” “);
}
System.out.println(“\\”);
}
for(int i=3;i>=1;i–)
{
for(int j=2;j>=i;j–)
{
System.out.print(” “);
}
System.out.print(“\\”);
for(int k=i;k<=(i-1)*3;k++)
{
System.out.print(” “);
}
System.out.println(“/”);
}
}
}

Write a program to accept a String and count the occurrences of each alphabet in that String and print.

import java.util.*;

class Proj10
{
void main()
{
System.out.println(“Enter the String : “);
Scanner sc =new Scanner(System.in);
String s =sc.nextLine();
s=s.toUpperCase();
int l= s.length();
for(int i=65; i<=91; i++)
{
int c=0;
for(int j=0;j<l;j++)
{
if(s.charAt(j)== (char)i)
{
c++;
}
}
if(c>0)
{
System.out.println((char)i+” => “+c);
}
}
}
}

How to write a Variable Description Table?

Most of you are not 100% sure of how the Variable Description Table will contain or what is it’s uses. Firstly, the Variable Description Table is used to give a vivid idea of the variables that been used in your class and it’s purpose. A good programmer should be aware of the Variables used in the class and it’s type and purpose. There are many ways of writing a Variable Description Table. Here’s one example.

Consider the following program to find the Factorial of 5:

 

class Demo

{

public static void main(String args[])

{

int fact=1;

for(int i=1;i<=5;i++)

{

fact=fact*i;

}

System.out.println(“Factorial of 5 is “+fact);

}

}

 

The Variable Description Table for this program will be :

 

Variable Name

Variable Type

Purpose

args

String[] To accept Command-line Arguments
i int

To iterate the for-loop for Factorial calculation

fact

int

To Calculate and Store the value of the Factorial

WAP to find the sum of the Series S=a+(a/2)+(a/3)+…..+(a/n)

import java.io.*;
class Series1
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the value of A :”);
double a=Double.parseDouble(br.readLine());
System.out.println(“Enter the Range of the Series :”);
int n=Integer.parseInt(br.readLine());
double s=0.0;
for(int i=1;i<=n;i++)
{
s=s+(a/i);
}
System.out.println(“The Sum of the Series is “+s);
}
}

WAP to find the HIGHEST and LOWEST element from a 2D Array

import java.io.*;
class TwoDimeArray
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the Dimensions of the Array :”);
int n=Integer.parseInt(br.readLine());
int m=Integer.parseInt(br.readLine());
int arr[][]=new int[n][m];
int i,j;
System.out.println(“Enter the Array Elements :”);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
arr[i][j]=Integer.parseInt(br.readLine());
}
}
int min=arr[0][0],max=arr[0][0];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr[i][j]<min)
{
min=arr[i][j];
}

if(arr[i][j]>max)
{
max=arr[i][j];
}
}
}
System.out.println(“Lowest Element is “+min);
System.out.println(“Highest Element is “+max);
}
}

Pattern 5

    *

 $ $ $

* * * * *

 $ $ $

    *

 

class Pattern5
{
public static void main(String args[])
{
int i,j,k;
for(i=1;i<=3;i++)
{
for(j=2;j>=i;j–)
{
System.out.print(” “);
}

for(k=1;k<(i*2);k++)
{
if(i==2)
{
System.out.print(“$ “);
}
else
{
System.out.print(“* “);
}
}
System.out.println();
}

for(i=2;i>=1;i–)
{
for(j=(3-i);j>=1;j–)
{
System.out.print(” “);
}

for(k=1;k<(i*2);k++)
{
if(i==2)
{
System.out.print(“$ “);
}
else
{
System.out.print(“* “);
}
}
System.out.println();
}
}
}

WAP to find the Roots of a Quadratic Equation

import java.io.*;
class Quadratic
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the values of A, B and C of a Quadratic Equation : “);
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
int c=Integer.parseInt(br.readLine());
double s=(b*b)-(4*a*c);
if(s<0)
{
System.out.println(“The Roots are IMAGINARY”);
}
else
{
System.out.println(“The Roots are Real”);
}
double root1=((-b)+Math.sqrt(s))/(2*a);
double root2=((-b)-Math.sqrt(s))/(2*a);
System.out.println(“The Roots are “+root1+” and “+root2);
}
}

 

//Output

Enter the values of A, B and C of a Quadratic Equation :

1

-3

2

The Roots are Real
The Roots are 2.0 and 1.0

Binary to Decimal conversion

class BinToDeci
{
public static void main(String args[])
{
long bin=110101;
int d,deci=0,p=0;
while(bin>0)
{
d=(int)bin%10;
bin=bin/10;
deci=deci+(int)(d*Math.pow(2,p));
p++;
}
System.out.println(“The Decimal Equivalent of Binary 110101 is “+deci);
}
}

WAP to accept an Address and print only the Numeric characters

Sample Input : 42 Broadsway, Chicago 15023
Sample Output : 4 2 1 5 0 2 3

 

import java.io.*;

class Address
{
public static void main(String a[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter Your Address : “);
String ad=br.readLine();
for(int i=0;i<ad.length();i++)
{
char ch= ad.charAt(i);
if(Character.isDigit(ch))
{
System.out.print(ch+” “);
}
}
}
}

 

 

String Palindrome

import java.io.*;
class StringPalindrome
{
public static void main(String[] a)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a Word : “);
String str = br.readLine();
int l = str.length();
str=str.toUpperCase();
String nstr = “”;
for(int i=l-1;i>=0;i–)
{
nstr = nstr+str.charAt(i);
}

if(str.equals(nstr))
{
System.out.println(“It is Palindrome.”);
}
else
{
System.out.println(“It is NOT a Palindrome.”);
}
}
}