VELLORE INSTITUTE OF
TECHNOLOGY
BCSE103E – JAVA PROGRAMMING
NAME: PRAVEEN KUMAR G
REGNO: 21BEC2181
FACULTY: KARTHIKEYAN B
1.Write a java program using for loop to print numbers
from 1 to 10
public class PrintNumbers
{
public static void main(String[] args)
{
for(int i=1; i<=10; i++)
{
System.out.println(i);
}
}
}
OUTPUT:
2. Write a java program to add all the numbers from 1
to 10 using for loop.
public class SumNumbers
{
public static void main(String[] args)
{
int sum = 0;
for(int i=1;i<=10; i++)
{
sum += i;
}
System.out.println("Sum: " + sum);
}
}
OUTPUT:
3. Write a java program to print multiplication tables
of any positive integer from 1 to 10 multiples.
import java.util.Scanner;
public class Tab
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int num;
System.out.print("Enter any positive integer: ");
num = console.nextInt();
System.out.println("Multiplication Table of " + num);
for(int i=1; i<=10; i++)
{
System.out.println(num +" x " + i + " = " + (num*i) );
}
}
}
OUTPUT:
import java.util.Scanner: The java scanner class is used to
get the input from the user. Scanner reads text from standard
input and returns it to a program.
console.nextInt() : reads a token of user input as an int.
4. Write a java program to print the factorial of a
number.
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int num; // To hold number
int fact = 1; // To hold factorial
System.out.print("Enter any positive integer: ");
num = console.nextInt();
for(int i=1; i<=num; i++)
{
fact *= i;
}
System.out.println("Factorial: "+ fact);
}
}
OUTPUT:
import java.util.Scanner: scanner is used to get the input from the user.
console.nextInt() : reads a token of user input as an int.
5. Write a java program to print the provided integer by
user in a reverse form (reversing all the digits provided).
import java.util.Scanner;
public class Number
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int number;
int reverse = 0;
System.out.print("Enter the number ");
number = console.nextInt();
int temp = number;
int remainder = 0; while(temp>0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp /= 10;
}
System.out.println("Reverse of " + number + " is " + reverse);
}
}
OUTPUT:
import java.util.Scanner: scanner is used to get the input from the user.
Scanner reads text from standard input and returns it to a program.
console.nextInt() : reads a token of user input as an int.
6. Write a java program to print the sum of any two
positive integers until user stops the execution
import java.util.Scanner;
public class Sum
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int number1, number2;
char choice;
do
{
System.out.print("Enter the first number ");
number1 = console.nextInt();
System.out.print("Enter the second number ");
number2 = console.nextInt();
int sum = number1 + number2;
System.out.println("Sum of numbers: " + sum);
System.out.print("Do you want to continue y/n? ");
choice = console.next().charAt(0);
System.out.println();
}while(choice=='y' || choice == 'Y');
}
}
OUTPUT:
console.nextInt() : In this program reads a token of user input as an int.
7. Write a java program to print the Fibonacci series of
any positive integer.
import java.util.Scanner;
public class FSeri
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int number; // To hold number of terms
int firstTerm = 0,
secondTerm = 1,
thirdTerm;
System.out.print("Enter number of terms: ");
number = console.nextInt();// explain why you need this??
System.out.print(firstTerm + " " + secondTerm + " ");
for(int i = 3; i <= number; i++)
{
thirdTerm = firstTerm + secondTerm;
System.out.print(thirdTerm + " ");
firstTerm = secondTerm;
secondTerm = thirdTerm;
}
}
}
OUTPUT:
console.nextInt() : In this program reads a token of user input as an int.
8. Write a java program to execute numbers from the range
of numbers 1 to 10, {particularly print numbers from 1 to 4}
using break function.
public class BreakP
{
public static void main(String[] args)
{
for(int i = 1; i <= 10; i++)
{
if(i == 5)
{
break;
}
System.out.print(i + " ");
}
System.out.println("Loop is over.");
}
}
OUTPUT:
9. Write a java program to execute numbers from the
range of numbers 1 to 10, print numbers odd numbers
from 1 to 10}using continue function.
public class ContinueP
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
System.out.println(i + " ");
}
}
}
OUTPUT: