Java Programming HelpDesk
Interview Q&A and OOP assignments on this site
Java Interview Questions
Core Java and OOP interview preparation.
What do you know about Java?
Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
What are the supported platforms by Java Programming Language?
Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.
List any five features of Java?
Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded
Why is Java Architectural Neutral?
It’s compiler generates an architecture-neutral object file format, which makes the compiled code to be executable on many processors, with the presence of Java runtime system.
How Java enabled High Performance?
Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor.
Why Java is considered dynamic?
It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.
List two Java IDE’s?
Netbeans, Eclipse, etc.
List some Java keywords(unlike C, C++ keywords)
Some Java keywords are import, super, finally, etc.
What do you mean by Object?
Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.
Define class?
A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.
What is a Local Variable?
Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.
What is a Instance Variable?
Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.
What is a Class Variable?
An array of single or one dimension is known as a single dimensional array or 1-D array. Single or one dimensional array can be of int,char,float etc.. type.
What is Two Dimensional Array ?
These are variables declared with in a class, outside any method, with the static keyword.
What is Singleton class?
Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.
What do you mean by Constructor?
Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.
List the three steps for creating an Object for a class?
An Object is first declared, then instantiated and then it is initialized.
What is the default value of byte datatype in Java?
Default value of byte datatype is 0.
What is the default value of float and double datatype in Java?
Default value of float and double datatype in different as compared to C/C++. For float its 0.0f and for double it’s 0.0d
When a byte datatype is used?
This data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
What is a static variable?
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
What do you mean by Access Modifier?
Java provides access modifiers to set access levels for classes, variables, methods and constructors. A member has package or default accessibility when no accessibility modifier is specified.
What is protected access modifier?
Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
Variables used in a switch statement can be used with which datatypes?
Variables used in a switch statement can only be a string, enum, byte, short, int, or char.
When parseInt() method can be used?
This method is used to get the primitive data type of a certain String.
Why is String class considered immutable?
The String class is immutable, so that once it is created a String object cannot be changed. Since String is immutable it can safely be shared between many threads ,which is considered very important for multithreaded programming.
Why is StringBuffer called mutable?
The String class is considered as immutable, so that once it is created a String object cannot be changed. If there is a necessity to make alot of modifications to Strings of characters then StringBuffer should be used.
What is the difference between StringBuffer and StringBuilder class?
Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.
Which package is used for pattern matching with regular expressions?
java.util.regex package
What is finalize() method?
It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.
What is an Exception?
An exception is a problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the thread's method invocation stack.
What do you mean by Checked Exceptions?
It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
Explain Runtime Exceptions?
It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compliation.
Which are the two subclasses under Exception class?
The Exception class has two main subclasses : IOException class and RuntimeException Class
When throws keyword is used?
If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature..
When throw keyword is used?
An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.
How finally used under Exception Handling?
The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.
What things should be kept in mind while creating your own exceptions in Java?
While creating your own exception − All exceptions must be a child of Throwable. If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class. You want to write a runtime exception, you need to extend the RuntimeException class.
Define Inheritance?Define Inheritance?
It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
When super keyword is used?
If the method overrides one of its superclass's methods, overridden method can be invoked through the use of the keyword super. It can be also used to refer to a hidden field.
What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
What is Abstraction?
It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.
What is What is Abstract class?
These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.
When Abstract methods are used?
If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.
What is an Interface?
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
What is an Interface?
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Give some features of Interface?
Interface cannot be instantiated An interface does not contain any constructors. All of the methods in an interface are abstract.
Java Assignments (OOP)
Solved Java programs using classes and objects.
Assignments of Strings in Java (14 programs)
import java.util.Scanner;
public class String_Length {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str;
int n;
System.out.println("Enter a string");
str=sc.nextLine();
n=str.length();
System.out.println("The length of the string is "+n);
}//end of main method
}//end of class String_Length// Enter a string "COMPUHELP" from the user and count the number of vowels in it.
import java.util.Scanner;
public class Count_Vowels {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str;
int count=0;
int n;
System.out.println("Enter a string");
str=sc.nextLine();
n=str.length(); //counting the length of the string.
for(int i=0;i<n;i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{
count++;
}
}
System.out.println("Number of vowels present in string are "+count);
}//end of main method
}//end of class Count_Vowels/*Enter a string from the user in lower case and change all the vowels of the entered
string in upper case.*/
import java.util.Scanner;
public class V_UpperCase {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str,str1="";
int n,i;
System.out.println("Enter a string in Lowercase");
str=sc.nextLine();
n=str.length();
for(i=0;i<n;i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{
str1=str1+str.substring(i,i+1).toUpperCase();
}//end of if block
else
{
str1=str1+str.substring(i,i+1);
} //end of else block
}//end of for loop
System.out.println(str1);
}//end of main method
}//end of class V_UpperCase//Enter a string from the user and count the number of words present in the entered string.
import java.util.Scanner;
public class Count_Words {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int n, i,space=0;
System.out.println("Enter a string");
str = sc.nextLine();
n = str.length();
for(i=0;i<n;i++)
{
if(str.charAt(i)==' ')
{
space++;
}
}// end of for loop
System.out.println("The number of words present in the string are "+(space+1));
} //end of main method
}//end of class Count_Words//Enter a String from the user and print those characters that they have space before them.
import java.util.Scanner;
public class Space_Before_Character {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int n, i;
System.out.println("Enter a string");
str = sc.nextLine();
n = str.length();
for(i=0;i<n;i++)
{
if(str.charAt(i)==' ')
{
System.out.println(str.charAt(i+1));
}
}//end of for loop
}//end of main method
}//end of class Space_Before_Character/*Enter a string from the user and count how many times only two vowels occur
together in the entered string.*/
import java.util.Scanner;
public class Two_Vowels_Together {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int n, i,count=0;
System.out.println("Enter a string");
str = sc.nextLine();
n = str.length();
for(i=0;i<n-1;i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{
if(str.charAt(i+1)=='a'||str.charAt(i+1)=='e'||str.charAt(i+1)=='i'||str.charAt(i+1)=='o'||str.charAt(i+1)=='u')
{
count++;
} //end of inner if
}//end of outer if
}//enf of for loop
System.out.println("The number of vowels together are "+count);
}//end of main method
}//end of class Two_Vowels_Together//Enter a string from the user and print the first and last word of the entered string.
import java.util.Scanner;
public class First_Last_Word {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int wl=0;
System.out.println("Enter a string");
str = sc.nextLine();
String str1[]=str.split(" ");
wl=str1.length;
System.out.println(str1[0]);
System.out.println(str1[wl-1]);
}//end of main method
}//end of class First_Last_Word/*Enter a string from the user in uppercase and change each vowel present at the end of
the words of the entered string in lowercase.*/
import java.util.Scanner;
public class Vowel_End_UpperCase {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str,strcat="";
int n,wl;
System.out.println("Enter a string in Uppercase");
str=sc.nextLine();
String str1[]=str.split(" ");
wl=str1.length;
for(int i=0;i<wl;i++)
{
n=str1[i].length();
if(str1[i].charAt(n-1)=='A'||str1[i].charAt(n-1)=='E'||str1[i].charAt(n-1)=='I'||str1[i].charAt(n-1)=='O'||str1[i].charAt(n-1)=='U')
{
strcat=strcat+str1[i].substring(0,n-1)+str1[i].substring(n-1).toLowerCase()+" ";
}
else
{
strcat=strcat+str1[i]+" ";
}
}
System.out.println(strcat);
}//end of main method
}//end of class Vowel_End_UpperCase/*Enter two strings from the user and check if both the strings are equal or not. If the two
strings are equal then print "strings are equal" otherwise print "strings are not equal". */
import java.util.Scanner;
public class Two_Strings_Equal {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str1,str2;
System.out.println("Enter first string");
str1=sc.nextLine();
System.out.println("Enter second string");
str2=sc.nextLine();
if(str1.equalsIgnoreCase(str2))
{
System.out.println("Strings are equal");
}
else
{
System.out.println("Strings are not equal");
}
}// end of main method
}//end of class Two_Strings_Equal//Enter a string from the user and concatenate the alternative words of the string together.
import java.util.Scanner;
public class Concat_Alternative_Words {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
System.out.println("Enter a string");
str=sc.nextLine();
int n,wl;
n=str.length();
String str1[]=str.split(" ");
wl=str1.length;
for(int i=0;i<wl;i++)
{
if(i%2==0)
{
System.out.print(str1[i]+" ");
}
}
}//end of main method
}//end of class Concat_Alternative_Wordsimport java.util.Scanner;
//Enter a string from the user in lowercase and capitalize each word of the string.
public class Capitalize_Each_Word {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str,strcat="";
System.out.println("Enter a string in lowercase");
str=sc.nextLine();
String str1[]=str.split(" ");
int wl,n;
wl=str1.length;
for(int i=0;i<wl;i++)
{
n=str1[i].length();
strcat=strcat+str1[i].substring(0,1).toUpperCase()+str1[i].substring(1,n)+" ";
}
System.out.println(strcat);
}//end of main method
}//end of class Capitalize_Each_Word//Enter a string "Compuhelp" from the user and print only "help" from the entered string.
import java.util.Scanner;
public class Print_Help {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str,strcat="";
System.out.println("Enter a string");
str=sc.nextLine();
int n=str.length();
System.out.println(str.substring(5,n));
}//end of main method
}//end of class Print_Help/*Enter a string from the user and find the first and last index number of the character
entered by the user.*/
import java.util.Scanner;
public class Index_First_Last {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str;
char ch;
System.out.println("Enter A string");
str=sc.nextLine();
System.out.println("Enter a character");
ch=sc.next().charAt(0);
System.out.println("The first index number of the entered character is "+str.indexOf(ch));
System.out.println("The last index number of the entered character is "+str.lastIndexOf(ch));
}//end of main method
}//end of class Index_First_Lastimport java.util.Scanner;
public class String_Replace {
public static void main(String[] args)
{
String str1=new String();
String str2=new String();
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string ");
str1=sc.nextLine();
str2=str1.replace("help","ter");
System.out.println(str2);
}//end of main method
}//end of class String_ReplaceAssignments of If and If..Else Statements (10 programs)
import java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int marks;
System.out.println("Enter marks ");
marks=scr.nextInt();
if(marks>=80 && marks<=100)
{
System.out.println("A Grade");
}
else if(marks>=60 && marks<80)
{
System.out.println("B Grade");
}
else if(marks>=40 && marks<60)
{
System.out.println("C Grade");
}
else if(marks>=0 && marks<40)
{
System.out.println("Fail");
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number ");
num=scr.nextInt();
if(num>0)
{
System.out.println("Number is Positive");
}
else if(num<0)
{
System.out.println("Number is Negative");
}
//else if(num==0)
else
{
System.out.println("Number is Zero");
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number ");
num=scr.nextInt();
if(num%2==0)
{
System.out.println("Number is even");
}
else
{
System.out.println("Number is odd");
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("Enter any character ");
ch=scr.next().charAt(0);
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
System.out.println("character is vowel");
}
else
{
System.out.println("character is consonent");
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("Enter any character ");
ch=scr.next().charAt(0); if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
System.out.println("character is vowel");
}
else if(ch=='@'||ch=='!'||ch=='#'||ch=='%'||ch=='&'||ch=='*')
{
System.out.println("character is special");
}
else
{
System.out.println("character is consonent");
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number ");
num=scr.nextInt();
if(num==1)
{
System.out.println("Day is Monday");
}
else if(num==2)
{
System.out.println("Day is Tuesday");
}
if(num==3)
{
System.out.println("Day is Wednesday");
}
if(num==4)
{
System.out.println("Day is Thursday");
}
if(num==5)
{
System.out.println("Day is Friday");
}
if(num==6)
{
System.out.println("Day is Saturday");
}
if(num==7)
{
System.out.println("Day is Sunday");
}
}//end of main
}//end of class
urn 0;
}import java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number between 0 to 9");
num=scr.nextInt();
if(num==0)
{
System.out.println("You entered Zero");
}
else if(num==1)
{
System.out.println("You entered One");
}
else if(num==2)
{
System.out.println("You entered Two");
}
else if(num==3)
{
System.out.println("You entered Three");
}
else if(num==4)
{
System.out.println("You entered Four");
}
else if(num==5)
{
System.out.println("You entered Five");
}
else if(num==6)
{
System.out.println("You entered Six");
}
else if(num==7)
{
System.out.println("You entered Seven");
}
else if(num==8)
{
System.out.println("You entered Eight");
}
else if(num==9)
{
System.out.println("You entered Nine");
}
else
{
System.out.println("wrong input");
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number between 1 and 12:");
num=scr.nextInt();
if(num==1)
{
System.out.println("You entered January");
}
else if(num==2)
{
System.out.println("You entered Feburary");
}
else if(num==3)
{
System.out.println("You entered March");
}
else if(num==4)
{
System.out.println("You entered April");
}
else if(num==5)
{
System.out.println("You entered May");
}
else if(num==6)
{
System.out.println("You entered June");
}
else if(num==7)
{
System.out.println("You entered July");
}
else if(num==8)
{
System.out.println("You entered August");
}
else if(num==9)
{
System.out.println("You entered September");
}
else if(num==10)
{
System.out.println("You entered October");
}
else if(num==11)
{
System.out.println("You entered November");
}
else if(num==12)
{
System.out.println("You entered December");
}
else
{
System.out.println("wrong input");
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int choice,num1,num2,res;
System.out.println("Press 1 for addition");
System.out.println("Press 2 for subtraction");
System.out.println("Press 3 for multiplication");
System.out.println("Press 4 for division");
System.out.println("Press 5 for exit");
choice=scr.nextInt();
if(choice<=4)
{
System.out.println("Enter first number:");
num1 = scr.nextInt();
System.out.println("Enter second number:");
num2 = scr.nextInt();
if (choice == 1)
{
res = num1 + num2;
System.out.println("The sum is: " + res);
}
else if (choice == 2)
{
res = num1 - num2;
System.out.println("The difference is: " + res);
}
else if (choice == 3)
{
res = num1 * num2;
System.out.println("The multiplication is: " + res);
}
else if (choice == 4)
{
res = num1 / num2;
System.out.println("The division is: " + res);
}
else if (choice == 5)
{
System.exit(0);
}
}
}//end of main
}//end of classimport java.util.Scanner;
public class if_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int a, b ,c;
System.out.println("Enter first number: ");
a= scr.nextInt();
System.out.println("Enter second number: ");
b= scr.nextInt();
System.out.println("Enter third number: ");
c= scr.nextInt();
if(a>b && a>c)
{
System.out.println("First number is greater");
}
else if(b>a && b>c)
{
System.out.println("Second number is greater");
}
else
{
System.out.println("Third number is greater");
}
}//end of main
}//end of classAssignments of Switch Statement (7 programs)
import java.util.Scanner;
public class switch_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number between 0 and 9:");
num=scr.nextInt();
switch(num)
{
case 0:
System.out.println("You entered ZERO");
break;
case 1:
System.out.println("You entered ONE");
break;
case 2:
System.out.println("You entered TWO");
break;
case 3:
System.out.println("You entered THREE");
break;
case 4:
System.out.println("You entered FOUR");
break;
case 5:
System.out.println("You entered FIVE");
break;
case 6:
System.out.println("You entered SIX");
break;
case 7:
System.out.println("You entered SEVEN");
break;
case 8:
System.out.println("You entered EIGHT");
break;
case 9:
System.out.println("You entered NINE");
break;
default:
System.out.println("wrong input");
}
}//end of main
}//end of classimport java.util.Scanner;
public class switch_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number between 1 and 12:");
num=scr.nextInt();
switch(num)
{
case 1:
System.out.println("You entered JANUARY");
break;
case 2:
System.out.println("You entered FEBURARY");
break;
case 3:
System.out.println("You entered MARCH");
break;
case 4:
System.out.println("You entered APRIL");
break;
case 5:
System.out.println("You entered MAY");
break;
case 6:
System.out.println("You entered JUNE");
break;
case 7:
System.out.println("You entered JULY");
break;
case 8:
System.out.println("You entered AUGUST");
break;
case 9:
System.out.println("You entered SEPTEMBER");
break;
case 10:
System.out.println("You entered OCTOBER");
break;
case 11:
System.out.println("You entered NOVEMBER");
break;
case 12:
System.out.println("You entered DECEMBER");
break;
default:
System.out.println("wrong input");
}
}//end of main
}//end of classimport java.util.Scanner;
public class switch_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num;
System.out.println("Enter any number between 1 and 7:");
num=scr.nextInt();
switch(num)
{
case 1:
System.out.println("You entered SUNDAY");
break;
case 2:
System.out.println("You entered MONDAY");
break;
case 3:
System.out.println("You entered TUESDAY");
break;
case 4:
System.out.println("You entered WEDNESDAY");
break;
case 5:
System.out.println("You entered THURSDAY");
break;
case 6:
System.out.println("You entered FRIDAY");
break;
case 7:
System.out.println("You entered SATURDAY");
break;
default:
System.out.println("wrong input");
}
}//end of main
}//end of classimport java.util.Scanner;
public class switch_statements
{
public static void main(String[] args)
{
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("Enter any character ");
ch=scr.next().charAt(0);
switch(ch)
{
case 'a':
System.out.println("Character is vowel");
break;
case 'e':
System.out.println("Character is vowel");
break;
case 'i':
System.out.println("Character is vowel");
break;
case 'o':
System.out.println("Character is vowel");
break;
case 'u':
System.out.println("Character is vowel");
break;
default:
System.out.println("character is consonent");
}
}//end of main
}//end of classimport java.util.Scanner;
public class switch_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num1,num2,res;
int value;
System.out.println("Enter first number: ");
num1=scr.nextInt();
System.out.println("Enter second number: ");
num2=scr.nextInt();
System.out.println("Press 1 for addition");
System.out.println("Press 2 for subtraction");
System.out.println("Press 3 for division");
System.out.println("Press 4 for multilication");
System.out.println("Press 5 for EXIT");
value=scr.nextInt();
switch(value)
{
case 1:
res=num1+num2;
System.out.println("The sum is: "+res);
break;
case 2:
res=num1-num2;
System.out.println("The subtraction is: "+res);
break;
case 3:
res=num1/num2;
System.out.println("The division is: "+res);
break;
case 4:
res=num1*num2;
System.out.println("The multiplication is: "+res);
break;
default:
System.exit(0);
}
}//end of main
}//end of classimport java.util.Scanner;
public class switch_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int x=0,y=1,z,i,fac=1;
int value;
System.out.println("Press 1 for fibbonacci series");
System.out.println("Press 2 for factorial");
System.out.println("Press 3 for printing even series");
System.out.println("Press 4 for printing odd series");
System.out.println("Press 5 for EXIT");
value=scr.nextInt();
switch(value)
{
case 1:
System.out.print("Fibbonacci series is:");
System.out.print(x+" "+y);
for(i=1;i<=8;i++)
{
z=x+y;
x=y;
y=z;
System.out.print(" "+z);
}
break;
case 2:
for(i=1;i<=5;i++)
{
fac=fac*i;
}
System.out.print("factorial of 5 is: "+fac);
break;
case 3:
System.out.print("even series is: ");
for(i=0;i<=10;i=i+2)
{
System.out.print(i+" ");
}
break;
case 4:
System.out.print("odd series is: ");
for(i=1;i<=10;i=i+2)
{
System.out.print(i+" ");
}
case 5:
System.exit(0);
break;
default:
System.out.print("wrong input,Please try again");
}
}//end of main
}//end of classimport java.util.Scanner;
public class switch_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int value,n;
char a;
int x=0,y=1,z,i,fac=1;
System.out.println("Press 1 for even numbers");
System.out.println("Press 2 for odd numbers");
System.out.println("Press 3 for square of the number");
System.out.println("Press 4 for character series");
System.out.println("Press 5 for EXIT");
value=scr.nextInt();
switch(value)
{
case 1:
System.out.println("Enter nth number: ");
n=scr.nextInt();
System.out.println("even series is: ");
for(i=0;i<=n;i=i+2)
{
System.out.print(i+" ");
}
break;
case 2:
System.out.println("Enter nth number: ");
n=scr.nextInt();
System.out.println("odd series is: ");
for(i=1;i<=n;i=i+2)
{
System.out.print(i+" ");
}
break;
case 3:
System.out.println("Enter nth number: ");
n=scr.nextInt();
System.out.print((n*n));
break;
case 4:
System.out.println("Enter any character in lowercase: ");
a=scr.next().charAt(0);
for(i=97;a<=122;a++)
{
System.out.print(a+" ");
}
break;
case 5:
System.exit(0);
break;
default:
{
System.out.println("wrong input");
}
}
}//end of main
}//end of classAssignments of For Loop (17 programs)
public class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=1;i<=10;i++)
{
System.out.print("*");;
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=1;i<=10;i++)
{
System.out.println("*");;
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=1;i<=6;i++)
{
System.out.println("t*");
System.out.println("COMPUHELPn");
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=0;i<=9;i++)
{
System.out.print(i+" ");
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=9;i>=0;i--)
{
System.out.print(i+" ");
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=5;i<=20;i++)
{
System.out.print(i+" ");
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=45;i>=7;i--)
{
System.out.print(i+" ");
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=1;i<=20;i+=2)
{
System.out.print(i+" ");
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i;
for(i=0;i<=20;i+=2)
{
System.out.print(i+" ");
}
}//end of main
}//end of classimport java.util.Scanner;
public class loop_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,num;
System.out.println("Enter the limit ");
num=scr.nextInt();
for(i=1;i<=num;i+=2)
{
System.out.print(i+" ");
}
}//end of main
}//end of classimport java.util.Scanner;
public class loop_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,num;
System.out.println("Enter the limit ");
num=scr.nextInt();
for(i=0;i<=num;i+=2)
{
System.out.print(i+" ");
}
}//end of main
}//end of classpublic class loop_statements
{
public static void main(String[] args)
{
int i,num;
for(i=0;i<=50;i+=3)
{
System.out.print(i+" ");
}
}//end of main
}//end of classimport java.util.Scanner;
public class loop_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,num,a=0,b=1,res=0;
System.out.println("Enter the limit ");
num=scr.nextInt();
System.out.println(a+" "+b);
for(i=0;res<=num;i++)
{
res=a+b;
a=b;
b=res;
System.out.print(" "+res);
}
}//end of main
}//end of classimport java.util.Scanner;
public class loop_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,num,prod=1;
System.out.println("Enter the limit ");
num=scr.nextInt();
for(i=0;prod<=num;i++)
{
System.out.print(prod+" ");
prod=prod*2;
}
}//end of main
}//end of classimport java.util.Scanner;
public class loop_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,num,fact=1;
System.out.println("Enter the number whose factorial you want to print: ");
num=scr.nextInt();
for(i=1;i<=num;i++)
{
fact=fact*i;
System.out.print(i+"*");
}
System.out.print("b="+fact);
}//end of main
}//end of classimport java.util.Scanner;
public class loop_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,num,sum=0;
System.out.println("Enter the number whose sum of series you want to print: ");
num=scr.nextInt();
for(i=1;i<=num;i++)
{
sum=sum+i;
System.out.print(i+"+");
}
System.out.print("b="+sum);
}//end of main
}//end of classimport java.util.Scanner;
public class loop_statements
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int m,n,i,res=1;
System.out.println("Enter the value for m ");
m=scr.nextInt();
System.out.println("Enter the value for n ");
n=scr.nextInt();
for(i=1;i<=n;i++)
{
res=res*m;
}
System.out.print(res);
}//end of main
}//end of classAssignments of Nested For Loop (24 programs)
public class NestedForLoop
{
public static void main(String[] args) {
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
System.out.print("*");
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args) {
int row,col,k=1;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
System.out.print(k);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args) {
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
System.out.print(col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args) {
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
System.out.print(row);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args) {
int row,col,k=1;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
System.out.print(k);
k++;
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args) {
int row,col;
for(row=3;row>=1;row--)
{
for(col=1;col<=3;col++)
{
System.out.print(row);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col,k=9;
for(row=3;row>=1;row--)
{
for(col=1;col<=3;col++)
{
System.out.print(k);
k--;
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=3;col>=1;col--)
{
System.out.print(col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
System.out.print("*");
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=1;col<=row;col++)
{
System.out.print("*");
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
System.out.print(col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
System.out.print(row);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=3;col>=row;col--)
{
System.out.print(row);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=3;col>=row;col--)
{
System.out.print(row);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=row;col<=3;col++)
{
System.out.print(col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=3;col>=row;col--)
{
System.out.print(col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col,k;
for(row=1;row<=3;row++)
{
for(k=row;k>=2;k--)
{
System.out.print(" ");
}
for(col=3;col>=row;col--)
{
System.out.print("*");
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col,k;
for(row=3;row>=1;row--)
{
for(k=2;k>=row;k--)
{
System.out.print(" ");
}
for(col=1;col<=row;col++)
{
System.out.print(col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col,k;
for(row=1;row=2;k--)
{
System.out.print(" ");
}
for(col=row;col<=3;col++)
{
System.out.print(row);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
System.out.print("* ");
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int i,j,k;
for(i=1;i=i;k--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print("* ");
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
//65 is ASCII value of A
for(row=65;row<=67;row++)
{
for(col=65;col<=row;col++)
{
System.out.print((char)col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int row,col;
//65 is ASCII value of A
for(row=65;row=row;col--)
{
System.out.print((char)col);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classpublic class NestedForLoop
{
public static void main(String[] args)
{
int i,j,k;
//65 is ASCII value of A
for(i=65;i=i;k--)
{
System.out.print(" ");
}
for(j=65;j<=i;j++)
{
System.out.print((char)j);
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of main
}//end of classAssignments of While and Do While Loops (8 programs)
import java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i, m=0,n;
System.out.println("Enter nth number: ");
n=scr.nextInt();
while(m<=n)
{
System.out.print(m+" ");
m++;
}
}//end of main
}//end of classimport java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,m;
System.out.println("Enter starting number: ");
m=scr.nextInt();
while(m>=0)
{
System.out.print(m+" ");
m--;
}
}//end of main
}//end of classimport java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,n,cnt=1;
System.out.println("Enter any number: ");
n=scr.nextInt();
while(cnt<=10)
{
System.out.println(n+"*"+cnt+"="+(n*cnt));
cnt++;
}
}//end of main
}//end of classimport java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,num,x=0,y=1,z=1;
System.out.println("Enter any number: ");
num=scr.nextInt();
System.out.print(x+" "+y);
do
{
z=x+y;
System.out.print(" "+z);
x=y;
y=z;
}
while(z<=(num-x));
}//end of main
}//end of classimport java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num=1,m1,m2,m3;
float avg=0;
while(num<=5)
{
System.out.println("Enter your marks in maths:");
m1=scr.nextInt();
System.out.println("Enter your marks in english:");
m2=scr.nextInt();
System.out.println("Enter your marks in hindi:");
m3=scr.nextInt();
avg=(m1+m2+m3)/3;
System.out.println("The average marks is: "+avg);
num++;
}
}//end of main
}//end of classimport java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num=0,rev=0;
System.out.println("Enter any number: ");
num=scr.nextInt();
System.out.println("The reverse of the number is: ");
while(num>=1)
{
rev=num%10;
num=num/10;
System.out.print(rev);
}
}//end of main
}//end of classimport java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int i,m,n,res=1,count=1;
System.out.println("Enter the value for m: ");
m=scr.nextInt();
System.out.println("Enter the value for n: ");
n=scr.nextInt();
while(count<=n)
{
res=res*m;
count++;
}
System.out.println("the result is: +res);
}//end of main
}//end of classimport java.util.Scanner;
public class WhileLoop
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int num=0, fac=1;
System.out.println("Enter any number: ");
num=scr.nextInt();
System.out.println("The factorial of the number is: ");
while(num>=1)
{
fac=fac*num;
System.out.print(num+"*");
num--;
}
System.out.println("b="+fac);
}//end of main
}//end of classAssignments of Character (8 programs)
//program to accept a character from the user
import java.util.Scanner;
public class AcceptCharacter
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
char ch;
System.out.println("Enter any character: ");
ch=scr.next().charAt(0);
System.out.println("The character is "+ch);
}//end of main
}//end of class//Program to accept a character in lower case and display it in uppercase.
import java.util.Scanner;
public class AcceptCharacter
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
char ch;
int x;
System.out.println("Enter any character: ");
ch=scr.next().charAt(0);
x=ch;
x=x-32;
ch=(char)x;
System.out.println("The character is "+ch);
}//end of main
}//end of class//program to accept a character from the user and display its ASCII value.
import java.util.Scanner;
public class AcceptCharacter
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
char ch;
System.out.println("Enter any character: ");
ch=scr.next().charAt(0);
System.out.println("The ASCII value of "+ch+" is "+(int)ch);
}//end of main
}//end of class// program to accept a character in uppercase and display it in lowercase.
import java.util.Scanner;
public class AcceptCharacter
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
char ch;
int x;
System.out.println("Enter any character: ");
ch=scr.next().charAt(0);
x=ch;
x=x+32;
ch=(char)x;
System.out.println("The character is "+ch);
}//end of main
}//end of class//program to display the ASCII values of 0 and 9.
public class AcceptCharacter
{
public static void main(String[] args)
{
char ch1,ch2;
ch1='0';
ch2='9';
System.out.println("The ASCII value of 0 is "+(int)ch1);
System.out.println("The ASCII value of 9 is "+(int)ch2);
}//end of main
}//end of class//program to display the ASCII values fron 0 to 9.
public class AcceptCharacter
{
public static void main(String[] args)
{
char ch;
ch='0';
int i;
for(i=0;i<=9;i++)
{
System.out.println("The ASCII of "+ch+" is "+(int)ch);
ch++;
}
}//end of main
}//end of class/*program to display the ASCII values of all characters from A to Z.*/
public class AcceptCharacter
{
public static void main(String[] args)
{
char ch;
ch='A';
int i;
for(i=1;i<=26;i++)
{
System.out.println("The ASCII of "+ch+" is "+(int)ch);
ch++;
}
}//end of main
}//end of class/* program to let the user press any key on the keyboard until x
but count only how many upper case character keys are pressed on the keyboard?*/
import java.util.Scanner;
public class AcceptCharacter
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int keys=0;
char ch;
System.out.println("Press any key including upper case alphabets: ");
while(true)
{
ch=scr.next().charAt(0);
if((ch>=65)&&(ch<=90))
{
keys++;
}
if((ch=='x')||(ch=='X'))
{
break;
}
}//end of while loop
System.out.println("The uppercase character keys are pressed ("+keys+") Times");
}//end of main
}//end of classAssignments of Single Dimensional Array (12 programs)
import java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[]=new int[5];
int i;
for(i=0;i<5;i++)
{
System.out.println("Enter array elements ");
arr[i]= scr.nextInt();
}
for(i=0;i<arr.length;i++)
{
System.out.println("Array elements are: "+arr[i]);
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[]=new int[5];
int i,len;
len=arr.length;
for(i=0;i<len;i++)
{
System.out.println("Enter array elements ");
arr[i]= scr.nextInt();
}
System.out.println("Array elements in Reverse order are: ");
for(i=len-1;i>=0;i--)
{
System.out.print(" "+arr[i]);
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[]=new int[5];
int arr1[]=new int[5];
int i,len;
len=arr.length;
for(i=0;i<len;i++)
{
System.out.println("Enter array elements ");
arr[i]= scr.nextInt();
}
System.out.println("Copied elements are : ");
for(i=0;i<len;i++)
{
arr1[i]=arr[i];
System.out.print(" "+arr1[i]);
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[]=new int[5];
int arr1[]=new int[5];
int i,len,j;
len=arr.length;
for(i=0;i<len;i++)
{
System.out.println("Enter array elements ");
arr[i]= scr.nextInt();
}
System.out.println("Copied elements are : ");
j=0;
for(i=4;i>=0;i--)
{
arr1[j]=arr[i];
System.out.println("Element is "+arr1[j]);
j++;
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[]=new int[5];
int i,sum=0;
for(i=0;i<arr.length;i++)
{
System.out.println("Enter array elements ");
arr[i]= scr.nextInt();
}
System.out.println("Sum of Array elements is : ");
for(i=0;i<arr.length;i++)
{
sum=sum+arr[i];
System.out.print(arr[i]+"+");
}
System.out.print("b="+sum);
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr1[]=new int[5];
int arr2[]=new int[5];
int sum[]=new int[5];
int i;
System.out.println("Enter elements into first array: ");
for(i=0;i<arr1.length;i++)
{
arr1[i]= scr.nextInt();
}
System.out.println("Enter elements into second array: ");
for(i=0;i<arr1.length;i++)
{
arr2[i]= scr.nextInt();
}
System.out.println("Sum of Two Array elements is : ");
for(i=0;i<arr1.length;i++)
{
sum[i] =arr1[i]+arr2[i];
System.out.print(sum[i]+" ");
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr1[]=new int[5];
int arr2[]=new int[5];
int sum[]=new int[5];
int i;
System.out.println("Enter elements into first array: ");
for(i=0;i<arr1.length;i++)
{
arr1[i]= scr.nextInt();
}
System.out.println("Enter elements into second array: ");
for(i=0;i<arr1.length;i++)
{
arr2[i]= scr.nextInt();
}
System.out.println("Sum of Two Array elements is : ");
for(i=0;i<arr1.length;i++)
{
sum[i] =arr1[i]-arr2[i];
System.out.print(sum[i]+" ");
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr1[]=new int[5];
int i,large=0,loc=0;
System.out.println("Enter elements into first array: ");
for(i=0;i<arr1.length;i++)
{
arr1[i]= scr.nextInt();
}
System.out.println("Array elements are : ");
for(i=0;i<arr1.length;i++)
{
System.out.print(arr1[i]+" ");
if(arr1[i]>large)
{
large=arr1[i];
loc=i+1;
}
}
System.out.println("The greatest element is "+large+" at location "+loc);
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr1[]=new int[5];
int i,small,loc=0;
System.out.println("Enter elements into an array: ");
for(i=0;i<arr1.length;i++)
{
arr1[i]= scr.nextInt();
}
small=arr1[0];
System.out.println("Array elements are : ");
for(i=1;i<arr1.length;i++)
{
System.out.print(arr1[i]+" ");
if(arr1[i]<small)
{
small=arr1[i];
loc=i+1;
}
}
System.out.println();
System.out.println("The smallest element is "+small+" at location "+loc);
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr1[]=new int[5];
int i,j,temp;
System.out.println("Enter elements into an array: ");
for(i=0;i<arr1.length;i++)
{
arr1[i]= scr.nextInt();
}
for(i=0;i<=4;i++)
{
for(j=i+1;j<5;j++)
{
if(arr1[i]>arr1[j])
{
temp=arr1[i];
arr1[i]=arr1[j];
arr1[j]=temp;
}
}
}
System.out.println("Sorted Array Elements are(Ascending Order) : ");
for(i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]);
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr1[]=new int[5];
int i,j,temp;
System.out.println("Enter elements into an array: ");
for(i=0;i<arr1.length;i++)
{
arr1[i]= scr.nextInt();
}
for(i=0;i<=4;i++)
{
for(j=i+1;j<5;j++)
{
if(arr1[i]<arr1[j])
{
temp=arr1[i];
arr1[i]=arr1[j];
arr1[j]=temp;
}
}
}
System.out.println("Sorted Array Elements are(Descending Order) : ");
for(i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]);
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr1[]=new int[5];
int i,n,flag=0;
System.out.println("Enter elements into an array: ");
for(i=0;i<arr1.length;i++)
{
arr1[i]= scr.nextInt();
}
System.out.println("Array Elements are : ");
for(i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]);
}
System.out.println("Enter any number which you want to search: ");
n= scr.nextInt();
for(i=0;i<=4;i++)
{
if(arr1[i]==n)
{
flag=1;
System.out.println("The number is "+arr1[i]+" found at "+(i+1)+" location");
}
}
if(flag==0)
{
System.out.println("Number is not found");
}
}//end of main
}//end of classAssignments of Two Dimensional Array (15 programs)
import java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][3];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Array Elements are : ");
for(i=0;i<row;i++)
{
for (j = 0; j < col; j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int arr1[][]=new int[2][2];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Copied Array Elements are : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr1[i][j]=arr[i][j];
System.out.print(arr1[i][j]+" ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int arr1[][]=new int[2][2];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Reverse copy of Array Elements are : ");
for(i=row-1;i>=0;i--) {
for (j = col - 1; j >= 0; j--) {
arr1[i][j] = arr[i][j];
System.out.print(arr1[i][j] + " ");
}
System.out.println();
}
/* int x=1;
int y=1;
System.out.println("Reverse copy of Array Elements are : ");
for(i=0;i<row;i++)
{
y++;
for (j=0;j<col;j++)
{
arr1[i][j]=arr[x][y];
System.out.print(arr1[i][j]+" ");
y--;
}
System.out.println();
x=0;
}*/
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int arr1[][]=new int[2][2];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Copy of Array Elements are : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr1[i][j] = arr[i][j];
System.out.print(arr1[i][j] + " ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Reverse of Array Elements are : ");
for(i=row-1;i>=0;i--) {
for (j = col - 1; j >= 0; j--) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int i,j,row,col,sum=0;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Sum of Array Elements are : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
sum = sum+arr[i][j];
System.out.print(arr[i][j]+"+");
}
}
System.out.println("b="+sum);
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int arr1[][]=new int[2][2];
int sum[][]=new int[2][2];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into first Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Enter elements into Second Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr1[i][j] = scr.nextInt();
}
}
System.out.println("Sum of two matrices is : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
sum[i][j] = arr[i][j]+arr1[i][j];
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int arr1[][]=new int[2][2];
int sub[][]=new int[2][2];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into first Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Enter elements into Second Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr1[i][j] = scr.nextInt();
}
}
System.out.println("Diffrence between two matrices is : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
sub[i][j] = arr[i][j]-arr1[i][j];
System.out.print(sub[i][j]+" ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int arr1[][]=new int[2][2];
int div[][]=new int[2][2];
int i,j,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into first Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Enter elements into Second Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr1[i][j] = scr.nextInt();
}
}
System.out.println("Division of two matrices is : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
div[i][j] = arr[i][j]/arr1[i][j];
System.out.print(div[i][j]+" ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[3][3];
int arr1[][]=new int[3][3];
int ans[][]=new int[3][3];
int i,j,k,row,col;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into first Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Enter elements into Second Matrix: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr1[i][j] = scr.nextInt();
}
}
System.out.println("Multiplication of two matrices is : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
ans[i][j]=0;
for(k=0;k<3;k++)
{
ans[i][j]+=arr[i][k]*arr1[k][j];
}//end of k loop
System.out.print(ans[i][j]+" ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int i,j,row,col,large=0,rloc=0,cloc=0;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Array Elements are : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
System.out.print(arr[i][j] +" ");
}
System.out.println();
}
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
if(arr[i][j]>large)
{
large=arr[i][j];
rloc=i+1;
cloc=j+1;
}
}
}
System.out.println("The greatest element is "+large+" and present at ["+rloc+"] row and ["+cloc+"] column");
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int i,j,row,col,small=0,rloc=0,cloc=0;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Array Elements are : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
System.out.print(arr[i][j] +" ");
}
System.out.println();
}
small=arr[0][0];
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(arr[i][j]<small)
{
small=arr[i][j];
rloc=i+1;
cloc=j+1;
}
}
}
System.out.println("The smallest element is "+small+" and present at ["+rloc+"] row and ["+cloc+"] column");
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int i,j,row,col,small=0,rloc=0,cloc=0;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Matrix is : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
System.out.print(arr[i][j] +" ");
}
System.out.println();
}
System.out.println("Tranpose of Matrix is : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
System.out.print(arr[j][i] +" ");
}
System.out.println();
}
}//end of main
}//end of classimport java.util.Scanner;
public class MyArray
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int arr[][]=new int[2][2];
int i,j,row,col,n,flag=0;
row=arr.length;
col=arr[0].length;
System.out.println("Enter elements into an array: ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
arr[i][j] = scr.nextInt();
}
}
System.out.println("Matrix is : ");
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
System.out.print(arr[i][j] +" ");
}
System.out.println();
}
System.out.println("Enter the number which you want to search : ");
n=scr.nextInt();
for(i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
if (arr[i][j] == n)
{
flag = 1;
System.out.println("Number is found at " + (i + 1) + " row and " + (j + 1) + " col");
}//end of if
} //end of j loop
}//end of i loop
if(flag==0)
{
System.out.println("Number is not found");
}
}//end of main
}//end of class#include<iostream> using namespace std;
int main()
{
int arr[3][3],i,j,n,flag=1;
cout<<"Enter the array elements: n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j]);
}}
cout<<"n The elements of array is: ");
cout<<"n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"%d",arr[i][j]);
}
cout<<"n");
}
cout<<"n Enter any no.which you want to search: ");
cin>>n);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(arr[i][j]==n)
{
flag=0;
cout<<"Number is found at %d row and %d column",i+1,j+1);
}}}
if(flag==1)
{
cout<<"number is not found");
}
return 0;
}Assignments of Character Array (10 programs)
#include<iostream>
using namespace std;
int main()
{
char name[30];
cout<<"Enter a string ";
//cin>>name;
//cin cannot read spaces
//gets(name);//read the string with spaces
//or
cin.getline(name,30);// read the string with spaces
cout<<"You entered: "<<name;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20],i,j;
cout<<"Enter a string";
gets(name);
for(i=0;name[i]!='';i++);
//Loop with i will count the length of a string
//So, i will store the length of a string
for(j=i-1;j>=0;j--)
{
cout<<name[j];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i;
cout<<"Enter a string ";
gets(name);
for(i=0;name[i]!='';i++);
cout<<"The length of the string is: "<<i;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i;
cout<<"Enter a string in uppercase: ";
gets(name);
cout<<"nThe string in lowercase: ";
for(i=0;name[i]!='';i++)
{
if((name[i]>='A')&&(name[i]<='Z'))
{
name[i]=name[i]+32;
}
}
cout<<name;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i;
cout<<"Enter a string in lowercase: ";
gets(name);
cout<<"nThe string in uppercase: ";
for(i=0;name[i]!='';i++)
{
if((name[i]>='a')&&(name[i]<='z'))
{
name[i]=name[i]-32;
}
}
cout<<name;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i,cnt=0;
cout<<"Enter a string in lowercase:";
gets(name);
cout<<"nThe vowels are: ";
for(i=0;name[i]!='';i++)
{
if((name[i]=='a')||(name[i]=='e')||(name[i]=='i')||(name[i]=='o')||(name[i]=='u')||(name[i]=='A')||(name[i]=='E')||(name[i]=='I')||(name[i]=='O')||(name[i]=='U'))
{
cout<<name[i];
cnt++;
}
}
cout<<"n The number of vowel is: "<<cnt;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i,cnt=0;
cout<<"Enter a string:";
gets(name);
cout<<"nThe consonents are: ";
for(i=0;name[i]!='';i++)
{
if((name[i]!='a')&&(name[i]!='e')&&(name[i]!='i')&&(name[i]!='o')&&(name[i]!='u')&&(name[i]!='A')&&(name[i]!='E')&&(name[i]!='I')&&(name[i]!='O')&&(name[i]!='U'))
{
cout<<name[i];
cnt++;
}
}
cout<<"n The number of consonents are: "<<cnt;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20],name1[20];
int i,cnt=0;
cout<<"Enter a string ";
gets(name);
cout<<"nThe copy of string: ";
for(i=0;name[i]!='';i++)
{
name1[i]=name[i];
cout<<name[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[50];
int i,wrd=0;
cout<<"Enter a sentence: ";
gets(name);
cout<<"The number of words are: ";
for(i=0;name[i]!='';i++)
{
if(name[i]==' ')
{
wrd++;
}
}
cout<<wrd+1;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char str1[20],str2[20],str3[50];
int i,j;
cout<<"Enter first string: ";
gets(str1);
cout<<"Enter second string: ";
gets(str2);
for(i=0;str1[i]!='';i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='';j++)
{
str3[i]=str2[j];
i++;
}
str3[i]='';
cout<<endl<<str3;
return 0;
}Assignments of Function (14 programs)
import java.util.Scanner;
public class FunctionPractice
{
void sum()
{
int num1,num2,num3,num4,res;
Scanner scr=new Scanner(System.in);
System.out.println("Enter first number");
num1=scr.nextInt();
System.out.println("Enter second number");
num2=scr.nextInt();
System.out.println("Enter third number");
num3=scr.nextInt();
System.out.println("Enter fourth number");
num4=scr.nextInt();
res=num1+num2+num3+num4;
System.out.println("Sum is "+res);
}//end of sum
public static void main(String[] args)
{
FunctionPractice fp=new FunctionPractice();
fp.sum();
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
int num1,num2,res;
static Scanner scr=new Scanner(System.in);
void input()
{
System.out.println("Enter first number");
num1=scr.nextInt();
System.out.println("Enter second number");
num2=scr.nextInt();
} //end of input
void sum()
{
res=num1+num2;
System.out.println("Sum is "+res);
}//end of sum
void subtract()
{
if(num1>num2)
{
res = num1 - num2;
}
else
{
res=num2-num1;
}
System.out.println("Subtraction is "+res);
}//end of subtract
void multiply()
{
res=num1*num2;
System.out.println("Multiplication is "+res);
}//end of multiply
void divide()
{
res=num1/num2;
System.out.println("Division is "+res);
}//end of divide
public static void main(String[] args)
{
FunctionPractice fp=new FunctionPractice();
char ch;
System.out.println("Press + for addition");
System.out.println("Press - for subtraction");
System.out.println("Press * for multiplication");
System.out.println("Press / for division");
System.out.println("Enter your choice ");
ch=scr.next().charAt(0);
fp.input();
switch (ch)
{
case '+':
fp.sum();
break;
case '-':
fp.subtract();
break;
case '*':
fp.multiply();
break;
case '/':
fp.divide();
break;
default:
System.out.println("Wrong input! Try again.");
}
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
static void odd(int n1)
{
int i;
System.out.println("The Odd Series is: ");
for(i=1;i<=n1;i+=2)
{
System.out.print(i+" ");
}
}
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int n;
System.out.println("Enter the nth number ");
n=scr.nextInt();
//no need to create the object for static function
odd(n);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
static void even(int n1)
{
int i;
System.out.println("The Even Series is: ");
for(i=0;i<=n1;i+=2)
{
System.out.print(i+" ");
}
}
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
int n;
System.out.println("Enter the nth number ");
n=scr.nextInt();
//no need to create the object for static function
even(n);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
void display(char ch1,int n1)
{
int i;
System.out.println("Series is: ");
for(i=1;i<=n1;i++)
{
System.out.print(ch1);
}//end of loop
}//end of display function
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
FunctionPractice fp=new FunctionPractice();
char ch;
int n;
System.out.println("Enter the character");
ch=scr.next().charAt(0);
System.out.println("Enter the nth number ");
n=scr.nextInt();
fp.display(ch,n);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
void series(int n1,int n2)
{
int i;
System.out.println("The Series is: ");
for(i=n1;i<=n2;i++)
{
System.out.print(i+" ");
}//end of loop
}//end of display function
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
FunctionPractice fp=new FunctionPractice();
int start,stop;
System.out.println("Enter the starting number of the series");
start=scr.nextInt();
System.out.println("Enter the ending number of the series");
stop=scr.nextInt();
fp.series(start,stop);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
void pos_neg(int n)
{
if(n>0)
{
System.out.println("Number is positive");
}//end of if
else if(n<0)
{
System.out.println("Number is negative");
}
else
{
System.out.println("Number is zero");
}
}//end of display function
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
FunctionPractice fp=new FunctionPractice();
int num;
System.out.println("Enter the number : ");
num=scr.nextInt();
fp.pos_neg(num);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
void fibonacci(int n)
{
int i,j=0,k=1,m;
System.out.print(j+" "+k);
for(i=1;i<=n;i++)
{
m=j+k;
j=k;
k=m;
if(m<=n)
System.out.print(" "+m);
}//end of loop
}//end of function
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
FunctionPractice fp=new FunctionPractice();
int num;
System.out.println("Enter any number for ending value: ");
num=scr.nextInt();
fp.fibonacci(num);
}//end of main
}//end of class#include<iostream>
using namespace std;
void to_upper(char a)
{
int i;
cout<<"The alphabet in uppercase is: ";
if((a>='a')&&(a<='z'))
{
a=a-32;
}//end of if
cout<<a;
}//end of function
int main()
{
char ch;
cout<<"Enter an alphabet in lowercase: ";
cin>>ch;
to_upper(ch);
return 0;
}//end of main#include<iostream>
using namespace std;
void to_lower(char a)
{
int i;
cout<<"The alphabet in lowercase is: ";
if((a>='A')&&(a<='Z'))
{
a=a+32;
}//end of if
cout<<a;
}//end of function
int main()
{
char ch;
cout<<"Enter an alphabet in Uppercase: ";
cin>>ch;
to_lower(ch);
return 0;
}//end of mainimport java.util.Scanner;
public class FunctionPractice
{
void seriesSum(int n) {
int j = 1;
int sum = 0;
System.out.print("SERIES = ");
for (int i = 1; i <= n; i++) {
j = i * i;
sum = sum + j;
System.out.print(j);
if(i<n)
{
System.out.print(" + ");
}
}
System.out.print(" = " + sum);
}
public static void main(String[] args) {
int num;
System.out.print("Enter the number whos series sum you want ");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
FunctionPractice fp=new FunctionPractice();
fp.seriesSum(num);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
int factorialFunction(int num) {
int fact = 1;
for (int i = num; i >= 1; i--) {
fact = fact * i;
}
return fact;
}
public static void main(String[] args) {
System.out.println("Enter the number whose factorial you want :");
Scanner sc = new Scanner(System.in);
int number;
number = sc.nextInt();
FunctionPractice fpf = new FunctionPractice();
int result = fpf.factorialFunction(number);
System.out.println("The factorial is : " + result);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
void swappingNumber(int num1, int num2)
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("The numbers after swapping are : " + num1 +" "+ num2);
}
public static void main(String[] args)
{
System.out.println("Enter two numbers ");
Scanner sc = new Scanner(System.in);
int number1, number2;
number1 = sc.nextInt();
number2 = sc.nextInt();
System.out.println("The numbers before swapping are : " + number1 +" "+ number2);
FunctionPractice fp = new FunctionPractice();
fp.swappingNumber(number1, number2);
}//end of main
}//end of classimport java.util.Scanner;
public class FunctionPractice
{
int factorialFunction(int num) {
int fact = 1;
if (num == 0 || num == 1)
return 1;
else
fact = num * factorialFunction(num - 1);
return fact;
}
public static void main(String[] args) {
System.out.println("Enter the number whose factorial you want :");
Scanner sc = new Scanner(System.in);
int number;
number = sc.nextInt();
FunctionPractice fp = new FunctionPractice();
int result = fp.factorialFunction(number);
System.out.println("The factorial is : " + result);
}//end of main
}//end of classAssignments of Classes and Objects (5 programs)
import java.util.Scanner;
class Maths
{
private int num1,num2,res;
Scanner scr=new Scanner(System.in);
public void input()
{
System.out.println("Enter first number: ");
num1=scr.nextInt();
System.out.println("Enter second number: ");
num2=scr.nextInt();
}
public void sum()
{
res=num1+num2;
System.out.println("Sum is "+res);
}
};//end of class
public class OopPracticeAssignments
{
public static void main(String[] args)
{
Maths mt=new Maths();
mt.input();
mt.sum();
}//end of main
}//end of classimport java.util.Scanner;
class Student
{
private int rollno;
private float marks[]=new float[2],per,sum;
Scanner scr=new Scanner(System.in);
public void get_data()
{
int i;
sum=0;
System.out.println("Enter rollno ");
rollno=scr.nextInt();
for(i=0;i<2;i++)
{
System.out.println("Enter marks ");
marks[i]=scr.nextInt();
sum=sum+marks[i];
}
}
public void percent()
{
per=sum/2;
System.out.println("Percentage is "+per);
}
};//end of class
public class OopPracticeAssignments
{
public static void main(String[] args)
{
Student st[]=new Student[2];
int i;
st[0]=new Student();
st[1]=new Student();
for(i=0;i<2;i++)
{
st[i].get_data();
System.out.println("Percentage of student "+(i+1));
st[i].percent();
System.out.println("----------------------------------n");
}
}//end of main
}//end of classimport java.util.Scanner;
class Demo2
{
private int num;
Scanner scr=new Scanner(System.in);
public void get_data()
{
System.out.println("Enter number ");
num=scr.nextInt();
}
public void sum(Demo2 dm)
{
int res;
res=num+dm.num;
System.out.println("Sum is "+res);
}
};//end of class
public class OopPracticeAssignments
{
public static void main(String[] args)
{
Demo2 d1=new Demo2();
Demo2 d2=new Demo2();
d1.get_data();
d2.get_data();
d1.sum(d2);
}//end of main
}//end of classimport java.util.Scanner;
class Demo2
{
int num;
Scanner scr=new Scanner(System.in);
void get_data()
{
System.out.println("Enter number ");
num=scr.nextInt();
}
void sum(Demo2 dm)
{
int res;
res=num+dm.num;
System.out.println("Sum is "+res);
}
};//end of class
public class OopPracticeAssignments
{
public static void main(String[] args)
{
Demo2 d1=new Demo2();
Demo2 d2=new Demo2();
d1.get_data();
d2.get_data();
d1.sum(d2);
}//end of main
}//end of classimport java.util.Scanner;
class Demo
{
Scanner scr=new Scanner(System.in);
private int num1,num2,num3;
public void max()
{
System.out.println("Enter first number ");
num1=scr.nextInt();
System.out.println("Enter second number ");
num2=scr.nextInt();
System.out.println("Enter third number ");
num3=scr.nextInt();
if(num1>num2 && num1>num3)
{
System.out.println("num1 is greater ");
}
else if(num2>num1 && num2>num3)
{
System.out.println("num2 is greater ");
}
else
{
System.out.println("num3 is greater ");
}
}
}//end of class
public class OopPracticeAssignments
{
public static void main(String[] args)
{
Demo dm=new Demo();
dm.max();
}//end of main
}//end of classAssignments of Constructor (4 programs)
class MyRectangle {
int length;
int breath;
int areaOfRectangle;
public MyRectangle() {
length = 10;
breath = 20;
}
public void area() {
System.out.println("The length is : " + length);
System.out.println("The breath is : " + breath);
areaOfRectangle = length * breath;
System.out.println("The area is : " + areaOfRectangle);
}
}//end of class
public class OopPracticeAssignments
{
public static void main(String[] args)
{
MyRectangle myRect = new MyRectangle();
myRect.area();
}//end of main
}//end of classclass MyRectangle
{
int length;
int breath;
int areaOfRectangle;
public MyRectangle(int l, int b)
{
length = l;
breath = b;
}
public void area()
{
System.out.println("The length is : " + length);
System.out.println("The breath is : " + breath);
areaOfRectangle = length * breath;
System.out.println("The area is : " + areaOfRectangle);
}//end of function
}//end of class
public class MyJavaProgram
{
public static void main(String[] args)
{
MyRectangle myRect = new MyRectangle(10, 20);
myRect.area();
MyRectangle myRect2 = new MyRectangle(56, 19);
myRect2.area();
}//end of function
}//end of classclass MyRectangle
{
int length;
int breath;
public MyRectangle()
{
length = 10;
breath = 20;
System.out.println("The length using default constructor is : " + length);
System.out.println("The breath usin default constructor is : " + breath);
}
public MyRectangle(int l, int b)
{
length = l;
breath = b;
System.out.println("The length using parametrised constructor is : " + length);
System.out.println("The breath using parametrised constructor is : " + breath);
}//end of function
}//end of class
public class MyJavaProgram
{
public static void main(String[] args)
{
MyRectangle myRect = new MyRectangle();
MyRectangle myRect2 = new MyRectangle(56, 19);
}//end of function
}//end of classclass MyRectangle
{
int length;
int breath;
int areaOfRectangle;
public MyRectangle()
{
length = 23;
breath = 56;
}
public void copyValue(MyRectangle my)
{
length = my.length;
breath = my.breath;
}
public void area()
{
System.out.println("The length is : " + length);
System.out.println("The breath is : " + breath);
areaOfRectangle = length * breath;
System.out.println("The area is : " + areaOfRectangle);
}//end of function
}//end of class
public class MyJavaProgram
{
public static void main(String[] args)
{
MyRectangle myRect = new MyRectangle();
myRect.area();
MyRectangle myRect2 = new MyRectangle();
myRect.copyValue(myRect2);
myRect2.area();
}//end of main
}//end of classAssignments of Function Overloading (2 programs)
class Maths
{
int num1;
int num2;
float num3;
float num4;
/*
public void sum(int n1, int n2)
{
num1 = n1;
num2 = n2;
int sumOfInt = num1 + num2;
System.out.println("The sum of integers values is : " + sumOfInt);
}
*/
public void sum(float n3, float n4)
{
num3 = n3;
num4 = n4;
float sumOfFloat = num3 + num4;
System.out.println("The sum of integers values is : " + sumOfFloat);
}//end of sum
}//end of class
public class MyFunctionOverloading
{
public static void main(String[] args)
{
Maths mt = new Maths();
mt.sum(10, 20);
mt.sum(34.5f, 78.6f);
}//end of main
}//end of classclass Shape
{
int length;
int breath;
int side;
public void area(int l, int b)
{
length = l;
breath = b;
int areaOfRect = length * breath;
System.out.println("The area of the reactangle is : " + areaOfRect);
}
public void area(int s)
{
side = s;
int areaOfSquare = side*side;
System.out.println("The area of the reactangle is : " + areaOfSquare);
}//end of function
}//end of class
public class MyFunctionOverloading
{
public static void main(String[] args) {
Shape s1 = new Shape();
s1.area(10, 20);
s1.area(5);
}//end of main
}//ecd of classAssignments of Static Data Member and Static Member Function (3 programs)
class StaticDemo
{
static int n = 0;
public StaticDemo()
{
n++;
System.out.println("Object " + n + " created");
}//end of function
}//end of class
public class staticFunction
{
public static void main(String[] args)
{
StaticDemo sd1 = new StaticDemo();
StaticDemo sd2 = new StaticDemo();
StaticDemo sd3 = new StaticDemo();
}//end of main
}//end of classclass Rectangle
{
int l;
int b;
static int n = 0;
public Rectangle()
{
n++;
System.out.println("Rectangle " + n + " is created");
}
public void area(int length, int breath)
{
l = length;
b = breath;
int area;
area = l * b;
System.out.println("The area of rectangle is :" + area);
}//end of function
}//end of class
public class staticFunction
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
rect.area(10, 40);
rect1.area(89, 56);
rect2.area(13, 59);
}//end of main
}//end of clasclass MyRectangle
{
static int length;
static int breath;
int areaOfRectangle;
public static void init()
{
length=10;
breath=20;
}
public void area()
{
System.out.println("The length is : " + length);
System.out.println("The breath is : " + breath);
areaOfRectangle = length * breath;
System.out.println("The area is : " + areaOfRectangle);
}//end of function
}//end of class
public class MyJavaProgram
{
public static void main(String[] args)
{
MyRectangle myRect = new MyRectangle();
MyRectangle.init();
myRect.area();
}//end of main
}//end of class