0% found this document useful (0 votes)
127 views5 pages

Java Programs for Number Manipulation

The document contains code snippets from multiple Java packages that demonstrate various programming concepts like: 1) Reading user input, inverting 2-digit numbers, and checking number of digits. 2) Printing odd numbers from 1 to 100 using a for loop. 3) Prompting for exponent value, and using a for loop and incrementation. 4) Inverting 5-digit numbers by extracting digits using modulo and division. 5) Implementing Fibonacci series using a for loop to calculate next value. 6) Getting user input for name and last name and printing full name.

Uploaded by

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

Java Programs for Number Manipulation

The document contains code snippets from multiple Java packages that demonstrate various programming concepts like: 1) Reading user input, inverting 2-digit numbers, and checking number of digits. 2) Printing odd numbers from 1 to 100 using a for loop. 3) Prompting for exponent value, and using a for loop and incrementation. 4) Inverting 5-digit numbers by extracting digits using modulo and division. 5) Implementing Fibonacci series using a for loop to calculate next value. 6) Getting user input for name and last name and printing full name.

Uploaded by

andres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

package class0612;

import java.util.Scanner;

/**
* This program reads a number, and if it have two digits, the program
show the
* number inverted. Example, if the number is 12, the program should
output 21.
*
* @author Rafael Niquefa
*/
public class Class0612 {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
System.out.print("Type a number: ");
int n = sn.nextInt();
if (n >= 10 && n <= 99) {
int u = n % 10;
int d = n / 10;
int x = u * 10 + d;
System.out.println("The inverted 2-digit number is " +
x);
} else {
System.out.println("Error, the number does not have 2
digits");
}

sn.close();
}
}

package cycles;

public class cycles {

public static void main(String[] args) {


int x;
for (x=1; x<100; x = x+1){
if ((x%2)!=0)
System.out.print( x +",");
}

package exercice1Homework;

import java.util.Scanner;

public class exercice1Homework {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
int n;
int x;
System.out.println("valor del exponente o n");
n = teclado.nextInt();
if (n>= 0)
for (x = 1; x <= n; x++) {
x = x+ 1;
System.out.println("igual a");
}
else {
System.out.println("etc");

}
}

package exercice2;

import java.util.Scanner;

public class exercice2 {

public static void main(String[] args) {


Scanner sn = new Scanner(System.in);
System.out.println("enter number");
int n = sn.nextInt();
int number = 0;
number = number*-1;
if (n >= 10000 && n <= 99999) {

int u = n % 100;
System.out.println("u = " + u);

int a = u % 10;
System.out.println("a = " + a);

int b = u / 10;
System.out.println("b = " + b);

int d = n / 100;
System.out.println("d = " + d);

int c = d % 10;
System.out.println("c = " + c);

int p = d / 10;
System.out.println("p = " + p);

int q = p % 10;
System.out.println("q = " + q);

int t = p / 10;
System.out.println("t=" + t);

int x = a * 10000 + b * 1000 + c * 100 + q * 10 + t;


System.out.println("The inverted 5-digit number is " +
x);

} else {
System.out.println("Error, the number does not have 5
digits");
}

sn.close();
}
}

package fibonacci1;
class fibonacci1 {

public static void main(String args[]) {

int n1 = 0, n2 = 1, n3, i, count = 10;


System.out.print(n1 + " " + n2);

for (i = 2; i < count; i++)


{
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}

}
}

package fibonacci1;
class fibonacci1 {

public static void main(String args[]) {

int n1 = 0, n2 = 1, n3, i, count = 10;


System.out.print(n1 + " " + n2);

for (i = 2; i < count; i++)


{
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}

}
}

package names;

import java.util.Scanner;
public class names {

public static void main(String[] args) {


Scanner teclado = new Scanner(System.in);
String A;
String B;
System.out.println("enter your name ");
A = teclado.nextLine();
System.out.println("enter your lastname ");
B = teclado.nextLine();
System.out.println( " your name is " + A + " " +B);

package secondExample;

import java.util.Scanner;

public class secondExample


{
public static void main(String[] args)
{
Scanner sn = new Scanner(System.in);
System.out.print("Type a number: ");
int number = sn.nextInt();
boolean isNegative = number < 0;
if( isNegative )
{
number = number*-1;
}
if( number >= 10 && number <= 99 )
{
int result = 0;
int u = number % 10;
int d = number / 10;
if( isEven(u) )
{
result = result + 1;
}
if( isEven(d) == true )
{
result = result + 1;
}
System.out.println("The amound of even digits is:
"+result);
}
else
{
System.out.println("Error, the number does not have 2
digits");
}

sn.close();
}
static boolean isEven( int number )
{
boolean isEven;
int remainder;
remainder = number % 2;
isEven = remainder == 0;
if( isEven == true )
{
return true;
}
else
{
return false;
}
}
static boolean isEvenShort( int n )
{
return n%2 == 0;
}
}

package seriesPrimera;
import java.util.Scanner;

class seriesPrimera {

public static void main( String args[] ) {

Scanner teclado = new Scanner(System.in);


System.out.println( "enter the evalue of a" );
int a = teclado.nextInt();
System.out.println( "Introduzca el valor de b " );
int b = teclado.nextInt();

System.out.print( " iagual a : " );


System.out.print( a==b );
}
}

You might also like