0% found this document useful (0 votes)
8 views

Programs

The document contains 35 code snippets showing Java programs that demonstrate various programming concepts like input/output, loops, conditionals, methods, classes, objects, strings, arrays and more. Each program is self-contained and focuses on a specific concept.

Uploaded by

Tanish Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programs

The document contains 35 code snippets showing Java programs that demonstrate various programming concepts like input/output, loops, conditionals, methods, classes, objects, strings, arrays and more. Each program is self-contained and focuses on a specific concept.

Uploaded by

Tanish Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Program 28:

import java.util.Scanner;

public class Num

public static void main(String args[])

Scanner in = new Scanner(System.in);

int number, even = 0, odd = 0;

System.out.println("Enter a 5 Digit Number and end with -99 to count even and odd digits:");

while (true)

number = in.nextInt();

// Check if the user entered -99 to exit the loop

if (number == -99)

break;

// Check if the input is a 5-digit number

if (number < 10000 || number > 99999)

System.out.println("Please enter a 5-digit number.");

continue; // Continue the loop for valid input

// Extract and count even and odd digits

while (number != 0)

int digit = number % 10;

if (digit % 2 == 0)

even++;

}
else

odd++;

number /= 10;

System.out.println("Even digits = " + even);

System.out.println("Odd digits = " + odd);

Program 29:

class Credintials

public void Venue()//data member of class Credential

System.out.println("The Venue is the Park");//printing the Venue

public void place()//data member of class Credential

System.out.println("The Reporting place is the picnic area");//printing the area of reporting

public void time()//data member of class Credential

System.out.println("The Reporting Time is 8 am in the morining");//printing the time

public void students()//data member of class Credential

System.out.println("The number of Students are 32");//printing the number of students

}
public void teacher()//data member of class Credential

System.out.println("The Teacher accompanying Students is Ms. Tina Singh");//printing the name of teacher in-charge

public void bus()//data member of class Credential

System.out.println("The Bus number is 39");//printing the bus number

public class Picnic//main class

public static void main(String args[])

Credintials PN=new Credintials();//instantiation of objects

PN.Venue(); //Data Member of class Credential printing the venue

PN.place(); //Data Member of class Credential printing the place in the venue

PN.time(); //Data Member of class Credential printing the time

PN.students(); //Data Member of class Credential printing the number of the students

PN.teacher(); //Data Member of class Credential printing the name of teacher in-charge

PN.bus(); //Data Member of class Credential printing the bus number

Program 30:

class FB

public void Ball()

System.out.println("Ball manufacturing company is Adidas");

public void Goalkeeper()

{
System.out.println("Goalkeeper is Messi");

public void Defender()

System.out.println("Defender is Ronaldo");

public class Football

public static void main(String[] args)

FB game1 = new FB();

game1.Ball();

game1.Goalkeeper();

game1.Defender();

Program 31:

import java.util.*;

public class Display

public static void main(String args[])

Scanner in=new Scanner(System.in);

String st;

System.out.println("Enter A String ending with a Space and a fullstop'.'");

while(true)

st=in.next();

if(st.equals("."))

break;

System.out.println(st);
}

Program 32:

import java.util.*;

public class the

public static void main(String args[])

Scanner in=new Scanner(System.in);

String st;

int c=0;

System.out.println("Enter a string in lowercase letters only");

while(true)

st=in.next();

if(st.equals("the"))

c=c+1;

//System.out.println("while loop => " + st);

continue;

System.out.print(st+" ");

//System.out.println("");

if(st.equals("."))

break;

System.out.println("");

System.out.println("Frequency of token 'the' = "+c);

Program 33:
public class Half

public static void main(String args[])

int a=5,b=4,c=3;

double d;

d=(Math.pow(a,2)+Math.pow(b,2)+Math.pow(c,2))/a*b*c;

System.out.println("Answer = "+d);

Program 34:

import java.util.*;

public class token

public static void main(String args[])

Scanner in= new Scanner(System.in);

String st1,st2;

System.out.println("Enter Two Words");

st1=in.next();

st2=in.next();

if(st1.compareTo(st2)<0)

System.out.println(st1+" , "+st2);

else if(st1.compareTo(st2)>0)

System.out.println(st2+" , "+st1);

else

System.out.println(st2+" is equal to "+st1);


}

Program 35:

public class Interest

public static void main (String args[])

int p = 6000, r = 10, t = 2; //Variables declared and initialized

float amt; // Variable declared but not initialized

amt = ((p * r * t)/100); // Calculates the SI

System.out.println ("Rate of Interest is Rupees = " + amt); // Prints the total amount

You might also like