0% found this document useful (0 votes)
50 views16 pages

Java Programming Assignment Solutions

The document contains a programming assignment by Muhammad Abdullah Riaz for the subject Programming Fundamental, consisting of 14 tasks. Each task involves writing Java programs that perform various operations such as calculating quotients, swapping numbers, reversing digits, and converting units. The assignment showcases fundamental programming concepts and basic arithmetic operations using Java.

Uploaded by

85abdullahmalik
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)
50 views16 pages

Java Programming Assignment Solutions

The document contains a programming assignment by Muhammad Abdullah Riaz for the subject Programming Fundamental, consisting of 14 tasks. Each task involves writing Java programs that perform various operations such as calculating quotients, swapping numbers, reversing digits, and converting units. The assignment showcases fundamental programming concepts and basic arithmetic operations using Java.

Uploaded by

85abdullahmalik
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

ASSIGNMENT NO 1

NAME: MUHAMMAD ABDULLAH RIAZ

ROLL NO: FA24-BCS-045

SECTION: BCS-2-A

SUBJECT: PROGRAMMING FUNDAMENTAL

SUBMITTED TO: FAYEZ AFZAL


1-Develop a program that inputs dividend and divisor. It then calculates and displays the
quotient and remainder.

import [Link];
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter an dividend value");
int a=[Link]();
[Link]("Enter an divisor value");
int b=[Link]();
int quotient= a/b;
int remainder=a%b;
[Link]("Enter a quotient value"+quotient);
[Link]("Enter a remainder value"+remainder);

}
}
2-Construct a program that inputs two numbers swaps the values and then displays them.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter number 1");
int num1=[Link]();

[Link]("Enter an number 2");


int num2=[Link]();
int temp=num1;
num1=num2;
num2=temp;
[Link]("num1: "+num1+" num2: "+num2);
}
}
3-Solve a program that inputs two numbers swaps these values without using the third
variable and displays them.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter number 1");
int num1=[Link]();
[Link]("Enter an number 2");
int num2=[Link]();
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
[Link]("num1: "+num1+" num2: "+num2);
}
}
4-Develop a program that inputs a five-digit number as input and reverse the number

import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("Enter a five-digit number: ");
int number = [Link]();
if (number < 10000 || number > 99999) {
[Link]("Please enter a valid five-digit number.");
} else {
int reversedNumber = (number % 10) * 10000 +
((number / 10) % 10) * 1000 +
((number / 100) % 10) * 100 +
((number / 1000) % 10) * 10 +
(number / 10000);
[Link]("The reversed number is: " + reversedNumber);
}
}
}
5-Construct a program that inputs an even and odd number through keyboard, multiplies
even with 5 and odd with 3 and adds both results. It subtracts the result from 1000 and
finally prints the difference
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("Enter an even num");
int even = [Link]();
[Link]("Enter an odd num");
int odd = [Link]();
int result=(even*5) +(odd*3);
int difference=1000-result;
[Link]("difference is "+difference);
}
}
6- Solve a program that will prompt the user to enter the number of hours. It computes and
displays the number of weeks and days.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("Enter the number of hours: ");
int hours = [Link]();
int weeks = hours / (24 * 7);
int remainingHours = hours % (24 * 7);
int days = remainingHours / 24;
[Link]("Equivalent time: " + weeks + " weeks and " + days + " days.");
}
}
7- Develop a program that inputs miles from the user and convert miles into kilometres

import [Link];

public class Main {

public static void main(String[] args) {


Scanner scanner= new Scanner([Link]);

[Link]("Enter distance in miles");

double miles=[Link]();

double kilometers=miles*1.60934;

[Link](miles + " miles equal to " + kilometers + " kilometers");


}
}
8- Construct a program that inputs 4 numbers and calculates the sum, average and product
of all numbers
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner= new Scanner([Link]);
[Link]("Enter first num");
int num1=[Link]();
[Link]("Enter second num");
int num2=[Link]();
[Link]("Enter third num");
int num3=[Link]();
[Link]("Enter fourth num");
int num4=[Link]();
int sum=num1+num2+num3+num4;
int average=(num1+num2+num3+num4)/4;
int product=num1*num2*num3*num4;
[Link]("Sum is"+sum);
[Link]("Average is"+average);
[Link]("Product is"+product);
}
9-Solve a program that inputs age in years and displays age in days and months.

import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner= new Scanner([Link]);
[Link]("Enter Age in years");
int years=[Link]();
int months=years*12;
int days=years*365;

[Link](" Age in months equal to "+months );


[Link]("Age in days equal to"+days);

}
}
10-Develop a program that inputs petrol in litres and displays how much distance the car
can cover using the available petrol. A car can travel 1 miles in 1 litre.

import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("petrol in liters");
double petrol=[Link]();
double distance=petrol*1;
[Link]("distance covered is"+distance+"in given petrol");

}
}
11-Construct a program that inputs total number of student in a class and fee per student.
It displays total fee collected from the class

import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Total number of students in class");
int students = [Link]();
[Link]("Fee per student");
double fees = [Link]();
double totalfees=students*fees;
[Link]("total fee is: "+totalfees);

}
}
12-Construct a program that inputs total number of student in a class and fee per student. It
displays total fee collected from the class.

import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("Enter the temperature in Fahrenheit: ");
double fahrenheit = [Link]();
double celsius = (fahrenheit - 32) * 5 / 9;
[Link]("temperture in celsius: " +celsius);

}
}
13-Develop a program that inputs five-digit number through the keyboard and calculates the
sum of its digits.

import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("Enter a five-digit number: ");
int number = [Link]();
if (number < 10000 || number > 99999) {
[Link]("Please enter a valid five-digit number.");
} else {
int digit1 = number / 10000;
int digit2 = (number / 1000) % 10;
int digit3 = (number / 100) % 10;
int digit4 = (number / 10) % 10;
int digit5 = number % 10;

int sum = digit1 + digit2 + digit3 + digit4 + digit5;


[Link]("SUM IS: "+sum);
}
}
}
14-Construct a program that inputs marks obtained by a student in five subjects. It then
calculates and displays the total marks and percentage.
import [Link];s
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner([Link]);
[Link]("MARKS OF SUBJECT 1:");
int subject1=[Link]();
[Link]("MARKS OF SUBJECT 2:");
int subject2=[Link]();
[Link]("MARKS OF SUBJECT 3:");
int subject3=[Link]();
[Link]("MARKS OF SUBJECT 4:");
int subject4=[Link]();
[Link]("MARKS OF SUBJECT 5:");
int subject5=[Link]();
int totalmarks=subject1+subject2+subject3+subject4+subject5;
double percentage=totalmarks/5.0;
[Link]("Total Marks: " + totalmarks);
[Link]("Percentage: "+ percentage);
}
}

You might also like