0% found this document useful (0 votes)
899 views

Ushtrime Programim JAVA

The document contains examples of Java code using while, do-while, and for loops to calculate the sum of odd numbers less than a given number n. It also contains examples of finding the minimum and maximum of input numbers using Math methods, calculating the average of array elements, and calculating the sum of an array.

Uploaded by

SaiDj Dulevic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
899 views

Ushtrime Programim JAVA

The document contains examples of Java code using while, do-while, and for loops to calculate the sum of odd numbers less than a given number n. It also contains examples of finding the minimum and maximum of input numbers using Math methods, calculating the average of array elements, and calculating the sum of an array.

Uploaded by

SaiDj Dulevic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

public class duwhile {

public static void main(String[] args) {

int i=1;

int s=0;
while(i<10) {
if(i%2==0) {
System.out.println("nr eshte cift");
}
else {
s=s+i;
i=i+1;
}

System.out.println("Shuma e n numrave tek"+s);

}
}

1. Me do-while
public class duwhile {

public static void main(String[] args) {

int i=1;

int s=0;

do{

s=s+i;
i=i+2;

}while(i<10) ;

System.out.println("Shuma e n numrave tek"+s);

}
}
1. For
public class duwhile {

public static void main(String[] args) {

int s=0,i;

for(i=1;i<10;i++) {
if(i%2==0) {
System.out.println("nr eshte cift");
}
else {
s=s+i;
i=i+1;
}

System.out.println("Shuma e n numrave tek"+s);

}
}

Ushtrimi 2:

import java.util.Scanner;

public class duwhile {

public static void main(String[] args) {

Scanner dr=new Scanner(System.in);


System.out.println("Ju lutem vendosni nr");
int n=dr.nextInt();

int s=0,i;

for(i=1;i<n;i++) {
if(i%2==0) {
System.out.println("nr eshte cift");
}
else {
s=s+i;
i=i+1;
}

}
System.out.println("Shuma e n numrave tek"+s);
}
}

Ushtrimi 2 me while:
import java.util.Scanner;
public class duwhile {

public static void main(String[] args) {

Scanner dr=new Scanner(System.in);


System.out.println("Ju lutem vendosni nr");
int n=dr.nextInt();
int s=0,i=1;

while(i<n) {

s=s+i;
i=i+2;
}
System.out.println("Shuma e n numrave tek"+s);
}}

Ushtrimi 3:
import java.util.Scanner;
public class minmax {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Vendos nr 1: ");
double x = in.nextDouble();
System.out.print("Vendos nr 2: ");
double y = in.nextDouble();
System.out.print("Vendos nr 3: ");
double z = in.nextDouble();
System.out.print("Min eshte " + gjenmin(x, y, z)+"\n" );
System.out.print("Max eshte " + gjenmax(x, y, z)+"\n" );
}

public static double gjenmin(double x, double y, double z)


{
return Math.min(Math.min(x, y), z);
}
public static double gjenmax(double x, double y, double z)
{
return Math.max(Math.min(x, y), z);
}
}

Ushtrimi 4:
import java.util.Scanner;
public class minmax {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Vendos nr 1: ");
double x = in.nextDouble();
System.out.print("Vendos nr 2: ");
double y = in.nextDouble();
System.out.print("Vendos nr 3: ");
double z = in.nextDouble();
System.out.print("Vendos nr 4: ");
double l = in.nextDouble();
System.out.print("Min eshte " + gjenmin(x, y, z)+"\n" );
System.out.print("Max eshte " + gjenmax(x, y, z)+"\n" );
}

public static double gjenmin(double x, double y, double z)


{
return Math.min(Math.min(x, y), z);
}
public static double gjenmax(double x, double y, double z)
{
return Math.max(Math.min(x, y), z);
}
}

Ushtrimi 5 : me overloading
import java.util.Scanner;
public class minmax {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Vendos nr 1: ");
double x = in.nextDouble();
System.out.print("Vendos nr 2: ");
double y = in.nextDouble();
System.out.print("Vendos nr 3: ");
double z = in.nextDouble();
System.out.print("Vendos nr 4: ");
double l = in.nextDouble();

System.out.print("Max eshte " + gjenmax(x, y, z)+"\n" );


System.out.print("Max eshte " + gjenmax(x, y, z,l)+"\n" );
}
public static double gjenmax(double x, double y, double z)
{
return Math.max(Math.min(x, y), z);
}
public static double gjenmax(double x, double y, double z,double l)
{
return Math.max(Math.min(x, y),l);
}
}

Ushtrimi 6: tek webi


Ushtrimi 7:
import java.util.Scanner;
class minmax{
public static void main(String args[]){
Scanner dr = new Scanner(System.in);
int[] array = new int[10];
System.out.println("Vendos gjatesine e array:");
int n=dr.nextInt();
int shuma = 0;
System.out.println("Vendos nr:");
for (int i=0; i<n; i++)
{
array[i] = dr.nextInt();
}
for( int i=0; i<n; i++) {
shuma = shuma+array[i];
}
System.out.println("Shuma e elementeve te array eshte:"+shuma);
}
}

Ushtrimi 8:

public class Funx_mesatarja {


public static void main(String[] args) {
int notat[]= {1,4,7,10};
int n=notat.length;
int shuma=0;
for(int i=0;i<n;i++) {
shuma=shuma+notat[i];
}
double mes;
mes=shuma/n;
int x=7;
int y=6;
int z=4;
System.out.println(mes +"\n");
if(mes>x) {
System.out.println("urime fituat shkollen");
}
if(mes<y) {
System.out.println("urime keni kaluar");
}
if (mes==z) {
System.out.println("Keni ngelur");
}

Ushtrimi me Shumen e n numrave te vargut:

public class Funx_mesatarja {


public static void main(String[] args) {
int notat[]= {2,6,10};
int n=notat.length;
int shuma=0;
for(int i=0;i<n;i++) {
shuma=shuma+notat[i];
}
System.out.println("Shuma eshte:"+shuma);
}

You might also like