Project Euler #1: Multiples of 3 and 5

Hi Friends ,

From today onwards I will be solving problems based on the Project Euler.

You can access the site from here. Today is the First Problem of Euler.

Find the sum of all the multiples of 3 or 5 .

Input Format

First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer,  N

Output Format

For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 below N .

So here is the code of the first problem :

 

Feel free to comment.

 

 

 

 

Send Emails through JSP(Java server Page)

Hi , Every one Today we are going to learn a simple jsp code to send email .

There might me n no. of situations where in you may require to a page where in you need to send email.

Following code might save you

To achieve this we require two libraries

  1. mail-1.4.1.jar
  2. activation-1.1.1.jar

I’ll leave the links below so you download them.

So the code

The code itself is self explanatory.
Just insert the

  • authAddress with the sender email id
  • authPassword with the sender password
  • smtpServer with your smtp server address
  • smtpPort    with your smtp server port
  • subject   with subject for your email
  • email    with the receiver email address

And run , it will start shooting emails as you click on Send

You can download both the JAR from the GitHub Link.

Else you can clone the Project

git clone https://github.com/ShubhamS32/JSPMailer.git

Feel free to comment 🙂

Pre & Post increment operator example Java

Hi,

Today we will be Finding the difference between pre- and post- increment of variables.

Pre -increment Operator:

int x=1;
System.out.println("x:"+x);
System.out.println("x:"+(++x));
System.out.println("x:"+x);

Post -increment Operator:

int x=1;
System.out.println("x:"+x);
System.out.println("x:"+x++);
System.out.println("x:"+x);

So what will be output you think??

For Post – increment Operator:

Output:


x:1
x:1
x:2

For Pre -increment Operator:
Output:

x:1
x:2
x:2

Explanation:

For pre – increment operator :

System.out.println(“x:”+x);  //Display value of x i.e 1

System.out.println(“x:”+(++x));  //First increase the value of x and then display i.e 2

System.out.println(“”x:”+x);  //Display value of x i.e 2

For post -increment operator :

System.out.println(“x:”+x);  //Display value of x i.e 1

System.out.println(“x:”+(x++));  //First display the value of x and then increment i.e 1

System.out.println(“”x:”+x);  //Display the incremented value of x i.e 2

 

That’s it so simple of Pre increment and Post increment Operation in Java.

Note: The operator (++) is called as Increment Operator and (–) is called as Decrement operator and these both Operator fall under the category of Unary Operator.

Feel free to comment 🙂

Differences between C++ & Java

By the way , C++ is also an object oriented programming language , just like Java. But there are some important features wise differences , between C++ and Java.

Let’s have a glance at them :

C++ Jva
1 C++ is not a purely object-oriented programming language
, since its possible to write C++ programs without using a class or an object.
Java is purely an object – oriented programming language ,
since it’s not possible to write Java program without construction of class.
2 Pointers are available in C++. We cannot create and use pointers in Java
3 Allocation and deallocation memory is the responsibility of the programmer. Here its taken care by JVM
4 Automatic casting is available in C++. In some cases , implicit casting is available , but one should use casting when required
5 C++ has goto statements. Its forbidden in java.
6 Multiple inheritance feature is available in C++. No Multiple inheritance in Java , but one can achieve in using Interface 🙂
7 There are 3 access specifiers in C++:
1)private
2)protected
3)public
Java supports 4 access specifiers:
1)private
2)protected
3)public
4)default
8 There are constructors and destructors in C++ . Only Constructors are available.

These where some of the key points that differentiate between C++ and Java.

Feel free to comments.

Java Loops I

Objective
In this challenge, we’re going to use loops to help us do some simple math.

Task
Given an integer N print its first 10 multiples. Each multiple N x i  (where 1<= i <=10 ) should be printed on a new line in the form: N x i = result.

Input Format

A single integer.

Output Format

Print 10  lines of output; each line i contains the  of in the form:
N x i = result.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=1;i<=10;i++)
{
System.out.println(n + ” x ” +i+” = “+(n*i));
}
}
}

The another way one can do it is

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i=1;

while(i<=10)

{
System.out.println(n + ” x ” +i+” = “+(n*i));

i+=1;
}
}
}

So basically there are 4 loops in java

  1. for loop
  2. while loop
  3. do while loop
  4. for each

In this post you’ve seen both the for loop and while loop , next post will share the remaining loop structures

Feel free to comment.

Java Output Formatting

Java’s System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf.

To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.

Input Format

Every line of input will contain a String followed by an integer.

Output Format

In each line of output there should be two columns:
The first column contains the String and is left justified using exactly 15 characters.
The second column contains the integer, expressed in exactly 3 digits; if the original input has less than three digits, you must pad your output’s leading digits with zeroes.

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);

for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
//Complete this line
System.out.printf(“%-15s%03d%n”,s1,x);
}

}
}

Feel free to comment .

Java If-Else

Lets understand java’s if else programs which will help  you understand if else in java

1)Java program to check whether you are eligble for voting or not.

public class checkAge

{

public static void main(String arg[])

{

Scanner sc = new Scanner(System.in);

int age=sc.nextInt(); //Taking input from the user

if(age>=18)

{System.out.println(“You are elgible for voting in India”);}

else

{System.out.println(“You are not eligble”);}

}

}

Here if else so easy as you can see.

2)If -else if ladder

Java program to

Given an integer n perform the following conditional actions:

  • If is odd, print Weird
  • If is even and in the inclusive range of 2 to 5 , print Not Weird
  • If is even and in the inclusive range of 6 to 20 , print Weird
  • If is even and greater than 20 , print Not Weird

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String ans=””;
if(n%2==1){
ans = “Weird”;
}
else {
if(n>=2 && n<=5) { ans= “Not Weird”; } else if(n>=6 && n<=20)
{
ans=”Weird”;
}
else
{
ans =”Not Weird”;
}
//Complete the code

}
System.out.println(ans);

}
}

Feel free to comment to you have doubt regarding the program.

Java program of taking Input.

Hi,

Today we are going to learn to take input in Java.

import java.lang.*

class Solution

{

public static void main(String arg[])

{

Scanner sc = new Scanner (System.in);

String a =sc.nextLine();

int b =sc.nextInt();

Double d = sc.nextDouble();

System.out.println(“Value of String is :”+a);

System.out.println(“Value of Integer is:”+b);

System.out.println(“Value of Double is:”+d);

}

So in the Above program we take input in Java using the Scanner Class Utility.

Inorder to take input for various Datatype we need to use scanner object as

For Integer :

nextInt()

For Double:

nextDouble()

For Boolean:

nextBoolean()

For String

nextLine() or Simply next()

Compile the program and see the output