Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Java Codes

The document contains code snippets for several Java programs that demonstrate different programming concepts: 1. A program that takes user input for exam marks and prints whether the user passed or failed based on a passing mark threshold. 2. Code to take integer, float, and string inputs from the user. 3. A program that prints the multiplication table for a number entered by the user. The document contains examples of programming concepts like conditional statements, loops, user input, string manipulation, and more.

Uploaded by

UTKARSH SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Java Codes

The document contains code snippets for several Java programs that demonstrate different programming concepts: 1. A program that takes user input for exam marks and prints whether the user passed or failed based on a passing mark threshold. 2. Code to take integer, float, and string inputs from the user. 3. A program that prints the multiplication table for a number entered by the user. The document contains examples of programming concepts like conditional statements, loops, user input, string manipulation, and more.

Uploaded by

UTKARSH SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Pass the exam

import java.util.Scanner;

class IfElse {
public static void main(String[] args) {
int marksObtained, passingMarks;

passingMarks = 40;

Scanner input = new Scanner(System.in);

System.out.println("Input marks scored by you");

marksObtained = input.nextInt();

if (marksObtained >= passingMarks) {


System.out.println("You passed the exam.");
}
else {
System.out.println("Unfortunately, you failed to pass the exam.");
}
}
}

2.string

import java.util.Scanner;

class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;

Scanner in = new Scanner(System.in);

System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);

System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);

System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);
}
}

3.multiplication table

import java.util.Scanner;

class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");

for ( c = 1 ; c <= 10 ; c++ )


System.out.println(n+"*"+c+" = "+(n*c));
}
}

4.odd or even

import java.util.Scanner;

class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();

if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
}
}

5.swapping using temperory value

import java.util.Scanner;

class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);

x = in.nextInt();
y = in.nextInt();

System.out.println("Before Swapping\nx = "+x+"\ny = "+y);

temp = x;
x = y;
y = temp;

System.out.println("After Swapping\nx = "+x+"\ny = "+y);


}
}

7.swapping using temperory value

import java.util.Scanner;

class SwapNumbers
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();

System.out.println("Before Swapping\nx = "+x+"\ny = "+y);

x = x + y;
y = x - y;
x = x - y;

System.out.println("After Swapping\nx = "+x+"\ny = "+y);


}
}

Factorial

import java.util.Scanner;

class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;

System.out.println("Enter an integer to calculate it's factorial");


Scanner in = new Scanner(System.in);

n = in.nextInt();

if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
fact = fact*c;

System.out.println("Factorial of "+n+" is = "+fact);


}
}
}
import java.util.Scanner;

class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;

Scanner in = new Scanner(System.in);


System.out.println("Input a number to check if it is an Armstrong number");
n = in.nextInt();

temp = n;

// Count number of digits

while (temp != 0) {
digits++;
temp = temp/10;
}

temp = n;

while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}

if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " isn't an Armstrong number.");
}

static int power(int n, int r) {


int c, p = 1;

for (c = 1; c <= r; c++)


p = p*n;

return p;
}
}

Reverse string

import java.util.*;

class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();

int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- )


reverse = reverse + original.charAt(i);

System.out.println("Reverse of entered string is: "+reverse);


}
}

Palindrome

import java.util.*;

class Palindrome
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1; i >= 0; i-- )


reverse = reverse + original.charAt(i);

if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string isn't a palindrome.");

}
}

Reverse number

import java.util.Scanner;

class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;

System.out.println("Enter a number to reverse");


Scanner in = new Scanner(System.in);
n = in.nextInt();

while(n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

System.out.println("Reverse of the number is " + reverse);


}
}
To find Strong Number
import java.util.scanner;
public class strongnumber
{
public static void
main(string args[])
{
strongnumber ss=new
strongnumber();
int a,b,r,s=0;
scanner sl=new scanner(system.in);
system.out.println(“Enter a number”);
b=sl.nextInt();
a=b;
while(b>0)
{
r=b%10;
s=s+ss.fact(r);
b=b/10;
}
if(a==s)
system.out.println(a+” is a stong number);
else
system.out.println(a+”is not a strong number);
}
int fact(int i)
{
int f,j;
f=1;
for(j=i;j>0;j--)
f=f*j;
return f;
}
}
GCD of Two Numbers:
import java.util.P;
public class gcdoftwonumbers
{
public static void
main(string args[])
{
int a,b,gcd;
a=b=gcd=0;
scanner s=new scanner(system.in);
system.out.println(“enter two num);
a=s.nextInt();
b=s.next.Int();
while(a!=0)
{
gcd=a;
a=b%a;
b=gcd;
}
system.out.println(“GCD”+gcd);
}
}

You might also like