Wednesday, May 6, 2020

Java Mini projects | Easy Small Projects for beginners


Java programs for Beginner-  If you are beginner in Java and want to learn java with Fun way here are some projects/ java problem for beginner. You have given java problem, Solution with explanation and we have added solution video. 
Looping Java Problem
1. Print 1  to 100 by using For and While Loop.  
Solution: we have to to break down small parts. Here are three things 
 Printing and its solution is System.out.println, starting point which is 1 and Ending point which is 100. And we have to add number by 1 .

                                                Code

public class LoopC {

public static void main(String[] args) {

//   Print 1 to 100 ------


// ----------------------For Loop-------------------

for(int i=1;i<=100;i=i+1){
System.out.print(i);
}
// --- ---------------------with while loop-----------------------
int i= 1;
while(i<=100){
System.out.println(i);
i++;
}

}

}

                                      Solution Video For Problem 1


------------------------------------------------------------------------------------------------------------------------

2. Print 1  to 20  Odd Number by using For and While Loop.  

Solution: Since we are printing number from 1 to 20 , out starting number will be 1 and Ending would be 20. Only new thing is odd number so will use i=i+2 instead of i=i+1.  see below code. 
                                                     Code

public class LoopC {

public static void main(String[] args) {

// ------- Print 1 to 20  Even Number -----

// --------------------------For Loop-----------------------
for(int i=1;i<20;i=i+2){
System.out.println("This is odd "+i);

}

// ------------------While loop----------------------
int i= 1;
while(i<20){
System.out.println("odd number "+i);
i=i+2;
}

}


}

                                       Solution video
----------------------------------------------------------------------------------------------------------------------
3. Print 1  to 20  Even Number by using For and While Loop.  

Solution: Since we are printing number from 1 to 20 , out starting number will be 1 and Ending would be 20. Since we want Even number we will start with 2,  and will use i=i+2 .

public class LoopC {

public static void main(String[] args) {
// TODO 
// Print 1 to 20  Even Number 
//----------------For Loop 

for(int i =2;i<=20;i=i+2){
System.out.println("my number is Even "+i);
}
// -------While Loop

int i = 2;
while (i<=20){
System.out.println("my number is Even "+i);
i=i+2;
}


}


}
  
                                                       Solution Video


----------------------------------------------------------------------------------------------------------------------
4. Print 1 to 10  in Descending order. For Ex- 10, 9, 8.....1

Solution : Since it is Descending order Our starting point is 10 and Ending point will 1. Also due to Descending order , decr. will be i--(which is same as i = i-1)
Code


public class LoopC {

public static void main(String[] args) {


// Print 1 to 10  in Descending order. For Ex- 10, 9, 8.....1
//-------------------For Loop 
         for(int i=10;i>=1;i=i-1){

System.out.println("this is Des "+i);
}


// --------------While Loop
int i= 10;
while(i>=1){
System.out.println("this is Des "+i);
i--;

}



}


}
                                            Solution in video
-------------------------------------------------------------------------------------------------------------------------

5. Print Sum of 1 to 10  
Solution: For print 1 to 10 as we know how to print it. but if we want to add sum of all number . we declare sum as variable as zero, now as soon as I will increase we will add to sum and it will loop or repeat untill it will reach to 10;
                                                              Code
-------------------------------------------------------------------------------------------------------------------------
public class LoopC {

public static void main(String[] args) {



// ---------------For Loop
int sum= 0;
for(int i = 1;i<=10;i++){
sum= sum+i;
System.out.println("Sum is "+sum);
}

//-----------------While Loop
int wsum= 0;
int i =1;
while(i<=10){
wsum=wsum+i;
System.out.println("Sum is "+wsum);
i++;

}

}

}
                                                                     Solution video

----------------------------------------------------------------------------------------------------------------------
6. ---------Print Table of 5 by using For loop and while loop
Solution :; Here we are printing table of 5 so we declare variable and store 5 in it. rest of is easy, we loop through as multiply 5 by i ,soevery time i , increase we are doing multiplication .                                                 
                                                                    Code
public class LoopC {

public static void main(String[] args) {




int multi= 5;
for(int i= 1;i<=10;i++){
System.out.println("5  *"+ i +" = "+multi*i);
}

int mul= 5;
int i = 1;
while(i<=10){
System.out.println("5  *"+ i +" = "+mul*i);
i++;
}

}
}
                                                          Solution video


---------------------------------------------------------------------------------------------------------------------
7. Ask  name from User , print it , Ask Question to user, take input from user , validate and print

Solution: We ask question by System..out.println("What is your name?"),For user input we need scanner class also we have to import class. After taking value from user we store in variable and print it. we ask second question and again we take answer from user , and that answer we validate and provide response accordingly, Please see below code, and for more info please watch solution video.

                                                                    Code

import java.util.Scanner;

public class Input {

void newInput(){
System.out.println("What is your name?");
Scanner s = new Scanner(System.in);
String  ns= s.next();
System.out.println("Hello "+ns);
System.out.println("Longest River in the world?");
String ans= s.next();
System.out.println("Your answer "+ans);
if(ans.equals("Nile")){
System.out.println("Good Job!!! ");
}
else {
System.out.println("Sorry your answer is not correct ");
}

}

public static void main(String[] args) {
Input in= new Input();
in.newInput();


}

}

                                                 Solution Video
------------------------------------------------------------------------------------------------------------------
8. Given Array Find Number is Positive , Negative Number
here you need for loop and use if to check condition in loop
Code

public class PoNe {
//Given Array Find Number is Positive , Negative
void myArray(){
int [] num = new int[3];
num[0]=10;
num[1]=0;
num[2]=-10;
for(int i = 0;i<3;i++){
if(num[i]>0){
System.out.println(num[i]+" is Positve");
}
else if(num[i]<0){
System.out.println(num[i]+" is Negative");
}
else {
System.out.println(num[i]+" is Zero");

}
}
}


public static void main(String[] args) {

PoNe p = new PoNe();
p.myArray();


}

}


9. How to print all the odd number in descending order?
First printnumber in decending order, then add if statement and add odd number condition

public class Number { void oddNumberDescendingOrder(){ for(int i = 10; i>=1;i--){ if(i%2==1){ System.out.println(i); } } } public static void main(String[] args) { Number n= new Number(); n.oddNumberDescendingOrder(); } }

java switch statement | Java Tutorial For Beginners

 Switch statement is alternative and more cleaner way to write code if we have more than two options. we can use else if, this is better way...