Assignment
Assignment
Program 01
number. Find which are divisible by 3, 5, 7, (3 & 5 both), and (3 & 7 both). Also
package task6;
import java.util.Scanner;
int n,i;
n=s.nextInt();
{ System.out.println("Divisible by 3:");
for(i=1;i<=n;i++)
{ if(i%3==0)
{ System.out.println(i+", ");
} }
System.out.println("Divisible by 5:");
for(i=1;i<=n;i++)
{ if(i%5==0)
{ System.out.println(i+", ");
} }
System.out.println("Divisible by 7:");
for(i=1;i<=n;i++)
{ if(i%7==0)
{ System.out.println(i+", ");
} }
for(i=1;i<=n;i++)
{ System.out.println(i+", ");
} }
for(i=1;i<=n;i++)
{ System.out.println(i+", ");
} }
} else
{System.out.println("Invalid Number! Number must be
between 10 & 100");}
} }
Program 02
Write a Java program to convert your age into years, month, weeks, days, hour,
minute, and seconds.
package task7;
import java.util.Scanner;
int bday,bmonth,byear,tday,tmonth,tyear;
int days,months,years,weeks,hours,min,sec;
bday=s.nextInt();
bmonth=s.nextInt();
byear=s.nextInt();
tmonth=s.nextInt();
tyear=s.nextInt();
if(tday-bday>=0)
{days=tday-bday;}
else{
days=30+(tday-bday); }
if(tmonth-bmonth>=0)
{months=tmonth-bmonth;}
else{
months=12+(tmonth-bmonth); }
years=tyear-byear;
months=months+(years*12);
weeks=months*4;
days=days+(months*30);
System.out.println("Total Days: "+days);
hours=days*24;
min=hours*60;
sec=min*60;
} }
Program 03
package task1;
import java.util.Scanner;
int
Money,Notes5000,Notes1000,Notes500,Notes100,Notes50,N
otes20,Notes10,Coins5,Coins2,Coins1;
Money=s.nextInt();
Money-=Notes5000*5000;
Notes1000=Money/1000;
Money-=Notes1000*1000;
Notes500=Money/500;
Money-=Notes500*500;
Notes100=Money/100;
Money-=Notes100*100;
Notes50=Money/50;
Money-=Notes50*50;
Money-=Notes20*20;
Notes10=Money/10;
Money-=Notes10*10;
Coins5=Money/5;
Money-=Coins5*5;
Coins2=Money/2;
Money-=Coins2*2;
Coins1=Money/1;
Money-=Coins1*1;
} }
Program 04
Write a Java program to mimic the procedure of a bank using class. Create
Account with name, NIC, and Opening balance. Have functionalities of deposit
or withdrawal of cash.
package task9;
import java.util.Scanner;
obj.create_account();
obj.withdrawal_amount();
obj.deposit_amount();
} }
class Bank
void create_account()
Name=s.next();
CNIC=s.nextInt();
Balance=s.nextInt();
void withdrawal_amount()
Amount=s.nextInt();
if(Amount<=Balance)
{ Balance=Balance-Amount;}
else{System.out.println("Insufficient Amount!");}
void deposit_amount()
Amount=s.nextInt();
Balance=Balance+Amount;
} }
Program 05
Write a Java program to mimic the Calculator using class. Program will provide
package task10;
import java.util.Scanner;
Calculator obj;
System.out.println("Starting Operations...");
obj.power();
obj.factorial();
class Calculator
{ int x,y,r;
void power()
x=s.nextInt();
r=(int) Math.pow(2,x);
System.out.println("2^"+x+" is "+r+"\n");
x=s.nextInt();
r=(int) Math.pow(x,2);
System.out.println(x+"^2"+" is "+r+"\n");
x=s.nextInt();
y=s.nextInt();
r=(int) Math.pow(x,y);
System.out.println(x+"^"+y+" is "+r+"\n");
} void factorial()
{ int i,f=1;
x=s.nextInt();
for(i=x;i>0;i--)
f=f*i; }
System.out.println(x+"! is "+f);
} }
Lab task
Exercise 1: Write a program that specifies whether a given number (x) falls in
one of the following categories (give x a value from the code, don't read from
user): • 0 to 9 • 10 to 19 • 20 to 29 • None of the categories
package labtask1;
import java.util.Scanner;
System.out.print("Enter a number:");
int x=s.nextInt();
else
} }
Exercise 2: Write a program that reads two integers and prints their sum like the
code below.
package labtask2;
import java.util.Scanner;
int x,y,sum;
x=s.nextInt();
y=s.nextInt();
sum=x+y;
} }
Exercise 3: Write a program that asks the user to enter two numerical values
(integers) and then select an operation (addition, subtraction,
multiplication and division) then prints the result based on operation
selected. The code below shows examples of the output
package labtask3;
import java.util.Scanner;
int m=04;
do
{ int a,b,x;
a=s.nextInt();
b=s.nextInt();
x=s.nextInt();
switch(x)
{
case 1:
int sum=a+b;
break;
case 2:
int sub=a-b;
break;
case 3:
int mul=a*b;
break;
case 4:
int div=a/b;
break;
default:
break;
}
System.out.println("Do you want to make another
calculation?");
System.out.println("1. Yes");
System.out.println("2. No");
m=s.nextInt();
} while(m==1);
} }
Exercise 4: Write a program that reads 10 numbers from the user then prints out
how many positive numbers and negative numbers user has entered
(consider 0 a positive number).
package task4;
import java.util.Scanner;
int v,p=0,n=0,i;
System.out.println("Enter 10 values");
for(i=0;i<10;i++)
v=s.nextInt();
if(v>=0)
p++;
else if(v<0)
n++; }
} }
Exercise 5: Write a program that asks the user to enter certain number, after that
asks him to enter another 20 numbers, after entering them all, it prints
out the number of occurrences of the first number. See example below
package task5;
import java.util.Scanner;
int v,n,i,o=0;
System.out.print("Enter number to search for: ");
v=s.nextInt();
for(i=0;i<20;i++)
{ n=s.nextInt();
if(v==n)
{ o++;} }
} }