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

Beginner's Computer Programs

The document outlines 20 computer programming projects in Java including calculating Armstrong and special numbers, finding the greatest of three numbers, temperature conversion, and classes for students, employees, and max/min values. It provides the problem statement, sample code and descriptions of the variables used in each program. The projects cover a range of programming concepts including conditionals, loops, methods, classes and object-oriented programming.

Uploaded by

cabbage
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)
55 views

Beginner's Computer Programs

The document outlines 20 computer programming projects in Java including calculating Armstrong and special numbers, finding the greatest of three numbers, temperature conversion, and classes for students, employees, and max/min values. It provides the problem statement, sample code and descriptions of the variables used in each program. The projects cover a range of programming concepts including conditionals, loops, methods, classes and object-oriented programming.

Uploaded by

cabbage
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
You are on page 1/ 26

Computer Project

Programming in Java
Index
No. Topic
1 Armstrong Numbers
2 Special Numbers
3 Greatest of Three Numbers
4 Sum of Two and Three Numbers
5 Temperature Conversion
6 Prime and Automorphic Numbers
7 Student Class
8 Employee Class
9 MaxMin Class
10 Average Marks Calculator
11 Linear Array Search
12 ASCII Code Calculator
13 Vowel and Consonant Calculator
14 Water Tax Calculator
15 Employee Salary Calculator
16 Square Calculator
17 Ascending Order Calculator
18 Initials and Title of a Name
19 Pyramid Pattern
20 First Letter Capitalizer
Armstrong Numbers
import java.util.Scanner;

public class Armstrong {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();
int temp = num;
int sum = 0;

while(temp != 0) {
sum += (temp % 10) * (temp % 10) * (temp % 10);
temp /= 10;
}

if(sum == num)
System.out.println(num + " is an Armstrong number.");
else
System.out.println(num + " is not an Armstrong number.");
}
}

Variable Datatype Description


num int User input
temp int Copy of user input
sum int Stores the sum of the cubes
Special Numbers
import java.util.Scanner;

public class SpecialNum {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();

special(num);
}

public static void special(int num) {


int temp = num;
int sum = 0;
while(temp != 0) {
int digit = temp % 10;
temp /= 10;
int fact = 1;
while(digit != 0) {
fact *= digit;
digit--;
}
sum += fact;
}

if(sum == num)
System.out.println(num + " is a special number.");
else
System.out.println(num + " is not a special number.");
}
}

Variable Datatype Description


num int User input
temp int Copy of user input
sum int Stores the sum of the factorials
fact int Stores the factorial of the current digit
digit int Stores the digit of which the factorial is being calculated
Greatest of Three Numbers
import java.util.Scanner;

public class Largest {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = in.nextInt();
System.out.print("Enter the second number: ");
int num2 = in.nextInt();
System.out.print("Enter the third number: ");
int num3 = in.nextInt();

System.out.println("The largest of the three numbers is: " +


largest(num1, num2, num3));
}

public static int largest(int num1, int num2, int num3) {


return Math.max(Math.max(num1, num2), num3);
}
}

Variable Datatype Description


num1 int User input
num2 int User input
num3 int User input
Sum of Two and Three Numbers
import java.util.Scanner;

public class SumOverload {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = in.nextInt();
System.out.print("Enter the second number: ");
int num2 = in.nextInt();
System.out.print("Enter the third number: ");
int num3 = in.nextInt();

System.out.println("The sum of the first two numbers is: " +


sum(num1, num2));
System.out.println("The sum of all three numbers is: " + sum(num1,
num2, num3));
}

public static int sum(int num1, int num2) {


return num1 + num2;
}

public static int sum(int num1, int num2, int num3) {


return num1 + num2 + num3;
}
}

Variable Datatype Description


num1 int User input
num2 int User input
num3 int User input
Temperature Conversion
import java.util.Scanner;

public class Temperature {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a temperature in celsius: " );
double c = in.nextDouble();
System.out.println("The value in farenheit is: " + convert(c));
}

public static double convert(double celsius) {


return (celsius * 9 / 5) + 32;
}
}

Variable Datatype Description


c double User input
Prime and Automorphic Numbers
import java.util.Scanner;

public class PrimeAutomorphic {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("1) Prime Number\n2) Automorphic Number\nEnter
your choice: ");
char choice = in.nextLine().charAt(0);
System.out.print("Enter a number: ");
int num = in.nextInt();

if(choice == '1') {
if (primeNum(num))
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
} else if(choice == '2') {
if (automorphicNum(num))
System.out.println(num + " is an automorphic number.");
else
System.out.println(num + " is not an automorphic number.");
} else {
System.out.println("Invalid choice.");
}
}

public static boolean primeNum(int num) {


boolean prime = true;
for(int i = 2; i < num; i++) {
if(num % i == 0)
prime = false;
}
return prime;
}

public static boolean automorphicNum(int num) {


int square = num * num;
String squareString = Integer.toString(square);
String numString = Integer.toString(num);
return squareString.substring(squareString.length() -
numString.length()).equals(numString);
}
}

Variable Datatype Description


choice char User input
num int User input
prime boolean Stores whether the number is prime or not
i int Loop variable
square Int Stores the square of the number
squareString String Stores the square of the number as a string
numString String Stores the number as a string
Student Class
import java.util.Scanner;

public class Student {


String name;
int age, m1, m2, m3, maximum;
double average;

public Student() {
name = "";
age = m1 = m2 = m3 = maximum = 0;
average = 0.0;
}

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter the name of the student: ");
name = in.nextLine();
System.out.print("Enter the age of the student: ");
age = in.nextInt();
System.out.print("Enter the marks of the first subject: ");
m1 = in.nextInt();
System.out.print("Enter the marks of the second subject: ");
m2 = in.nextInt();
System.out.print("Enter the marks of the third subject: ");
m3 = in.nextInt();
}

public void calculate() {


maximum = Math.max(Math.max(m1, m2), m3);
average = (m1 + m2 + m3) / 3;
}

public void display() {


System.out.println("Name of the student: " + name);
System.out.println("Age of the student: " + age);
System.out.println("Marks in three subjects: " + m1 + ", " + m2 +
", " + m3);
System.out.println("Maximum marks of the student: " + maximum);
System.out.println("Average marks of the student: " + average);
}

public static void main(String[] args) {


Student student = new Student();
student.input();
student.calculate();
student.display();
}
}

Variable Datatype Description


student Student Object variable
Employee Class
import java.util.Scanner;

public class Employee {


double basic, dearness, rent, provident, net, gross;

public Employee(double basic) {


this.basic = basic;
this.dearness = this.rent = this.provident = this.net = this.gross
= 0.0;
}

public void Calc() {


dearness = basic * 0.25;
rent = basic * 0.15;
provident = basic * 0.0833;
net = basic + dearness + rent;
gross = net - provident;
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.print("Enter the basic salary of the employee: ");
double basic = in.nextDouble();
Employee employee = new Employee(basic);
employee.Calc();
System.out.println("Net salary: " + employee.net + "\nGross salary:
" + employee.gross);
}
}

Variable Datatype Description


basic double Class variable
dearness double Class variable
rent double Class variable
provident double Class variable
net double Class variable
gross double Class variable
employee Employee Object variable
MaxMin Class
import java.util.Scanner;

public class MaxMin {


int arr[], max , min;

public MaxMin() {
arr = new int[10];
max = min = 0;
}

public void Input() {


Scanner in = new Scanner(System.in);
for(int i = 0; i < 10; i++) {
System.out.print("Enter element no " + (i + 1) + ": ");
arr[i] = in.nextInt();
}
}

public void Max() {


max = arr[0];
for(int i = 0; i < 10; i++)
max = Math.max(max, arr[i]);
}

public void Min() {


min = arr[0];
for(int i = 0; i < 10; i++)
min = Math.min(min, arr[i]);
}

public void Disp() {


System.out.println("Largest number: " + max + "\nSmallest number:
"+ min);
}

public static void main(String[] args) {


MaxMin maxMin = new MaxMin();
maxMin.Input();
maxMin.Max();
maxMin.Min();
maxMin.Disp();
}
}

Variable Datatype Description


arr double Class variable
max double Class variable
min double Class variable
i int Loop variable
maxMin MaxMin Object variable
Average Marks Calculator
import java.util.Scanner;

public class AverageMarks {


public static void main(String[] args) {
String[] names = new String[10];
int[] rollNo = new int[10];
int[] marks1 = new int[10];
int[] marks2 = new int[10];
int[] marks3 = new int[10];

Scanner in = new Scanner(System.in);


for (int i = 0; i < 10; i++) {
System.out.print("Enter the roll no of student " + (i + 1) + ":
");
rollNo[i] = in.nextInt();
System.out.print("Enter the name of student " + (i + 1) + ":
");
names[i] = in.nextLine();
System.out.print("Enter the marks of subject 1 of student " +
(i + 1) + ": ");
marks1[i] = in.nextInt();
System.out.print("Enter the marks of subject 2 of student " +
(i + 1) + ": ");
marks2[i] = in.nextInt();
System.out.print("Enter the marks of subject 3 of student " +
(i + 1) + ": ");
marks3[i] = in.nextInt();
}

System.out.println("Roll no\t\tName\t\tAverage\t\tGrade");
for (int i = 0; i < 10; i++) {
int avg = (marks1[i] + marks2[i] + marks3[i]) / 3;
String grade = "";
if(avg >= 85)
grade = "A";
else if(avg >= 60)
grade = "B";
else if(avg >= 50)
grade = "C";
else
grade = "D";
System.out.println(rollNo[i] + "\t\t\t" + names[i] + "\t\t\t" +
avg + "\t\t\t" + grade);
}
}
}

Variable Datatype Description


names String[] User input
rollNo int[] User input
marks1 int[] User input
marks2 int[] User input
marks3 int[] User input
i int Loop variable
avg int Stores the average
grade String Stores the grade

(Example shortened for brevity)


Linear Search
import java.util.Scanner;

public class LinearSearch {


public static void main(String[] args) {
String[] names = {"AB", "CD", "EF", "GH", "IJ", "KL", "MN", "OP",
"QR", "ST"};
int[] phoneNumbers = {11, 22, 33, 44, 55, 66, 77, 88, 99, 10};
Scanner in = new Scanner(System.in);

System.out.println("List of contacts and numbers: ");


for(int i = 0; i < 10; i++)
System.out.print(names[i] + "\t");
System.out.println();
for(int i = 0; i < 10; i++)
System.out.print(phoneNumbers[i] + "\t");

System.out.print("\n\nEnter a name: ");


String name = in.nextLine();
boolean found = false;
for(int i = 0; i < 10 && !found; i++) {
if(name.equals(names[i])) {
System.out.println("Element found, " + name + "'s phone
number is: " + phoneNumbers[i]);
found = true;
}
}
if(!found) {
System.out.println("Element not found in the list of
contacts.");
}
}
}

Variable Datatype Description


names String[] List of names of contacts
phoneNumbers int[] List of phone numbers of contacts
name String User input
found boolean Stores whether the contact was found in the list
i int Loop variable
ASCII Code Calculator
import java.util.Scanner;

public class StringASCII {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = in.nextLine();
for(int i = 0; i < str.length(); i++) {
System.out.println("The ASCII value of " + str.charAt(i) + " is
" + (int) str.charAt(i));
}
}
}

Variable Datatype Description


str String User input
i int Loop variable
Vowel and Consonant Calculator
import java.util.Scanner;

public class StringASCII {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] names = new String[10];

for (int i = 0; i < 10; i++) {


System.out.print("Enter the name no " + (i + 1) + ": ");
names[i] = in.nextLine();
}

System.out.println("Name\t\tVowels\t\tConsonants");

for(int i = 0; i < 10; i++) {


int consonants = 0, vowels = 0;
for(int j = 0; j < names[i].length(); j++) {
char c = names[i].toLowerCase().charAt(j);
if(Character.isAlphabetic(c)) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c
== 'u')
vowels++;
else
consonants++;
}
}
System.out.println(names[i] + "\t\t\t" + vowels + "\t\t\t" +
consonants);
}
}
}

Variable Datatype Description


names String[] User input
i int Loop variable
j int Loop variable
consonants int Stores number of consonants
vowels int Stores number of vowels
c char Stores the current character being checked

(Example shortened for brevity)


Water Tax Calculator
import java.util.Scanner;

public class WaterTax {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount of water consumed: ");
int w = in.nextInt();
int tax = 0;
if(w >= 45) {
if(w <= 75)
tax = 475;
else if(w <= 125)
tax = 750;
else if(w <= 200)
tax = 1225;
else if(w <= 350)
tax = 2000;
}
System.out.println("The tax is: " + tax);
}
}

Variable Datatype Description


arr int[] Stores the squares of the numbers
i int Loop variable
Employee Salary Calculator
import java.util.Scanner;

public class EmpSal {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] basic = new int[15];
for(int i = 0; i < 15; i++) {
System.out.print("Enter the basic salary for employee " + (i +
1) + ": ");
basic[i] = in.nextInt();
}
for(int i = 0; i < 15; i++) {
System.out.println("Total salary for employee " + (i + 1) + ":
" + basic[i] * 1.4);
System.out.println("Net salary for employee " + (i + 1) + ": "
+ basic[i] * 1.3);
}
}
}

Variable Datatype Description


basic int[] User input
i int Loop variable
Square Calculator
public class Squares {
public static void main(String[] args) {
int[] arr = new int[10];
for(int i = 0; i < 10; i++) {
arr[i] = i * i;
System.out.println(arr[i]);
}
}
}

Variable Datatype Description


arr int[] Stores the squares of the numbers
i int Loop variable
Ascending Calculator
import java.util.Scanner;

public class AscOrder {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr = new int[10];
for(int i = 0; i < 10; i++) {
System.out.print("Enter a number (#" + (i + 1) + "): ");
arr[i] = in.nextInt();
}
boolean asc = true;
for(int i = 1; (i < 10) && asc; i++) {
if(arr[i - 1] > arr[i])
asc = false;
}
if(asc)
System.out.println("The list is in ascending order.");
else
System.out.println("The list is not in ascending order.");
}
}

Variable Datatype Description


arr int[] User input
i int Loop variable
asc bool Stores whether the list is in ascending order or not
Initials and Title of a Name
import java.util.Scanner;

public class NameInTi {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a name: ");
String name = in.nextLine();
String out = name.charAt(0) + ".";
for(int i = 0; i < name.length(); i++) {
if(name.charAt(i) == ' ') {
if(name.indexOf(' ', (i + 1)) != -1)
out += name.charAt(i + 1) + ".";
}
}
for(int i = name.lastIndexOf(' '); i < name.length(); i++) {
out += name.charAt(i);
}
System.out.println(out);
}
}

Variable Datatype Description


name String User input
i int Loop variable
out String Stores the output string
Pyramid Pattern
import java.util.Scanner;

public class PyramidPattern {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = in.nextLine();
for(int i = 1; i <= word.length(); i++) {
for(int j = 0; j < i; j++)
System.out.print(word.charAt(j));
System.out.println();
}
}
}

Variable Datatype Description


word String User input
i int User input
j int User input
First Letter Capitalizer
import java.util.Scanner;

public class PyramidPattern {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a phrase: ");
String phrase = in.nextLine();
String out = phrase.substring(0, 1).toUpperCase();
for(int i = 1; i < phrase.length(); i++) {
if(phrase.charAt(i - 1) == ' ' &&
Character.isAlphabetic(phrase.charAt(i)))
out += Character.toUpperCase(phrase.charAt(i));
else
out += phrase.charAt(i);
}
System.out.println(out);
}
}

Variable Datatype Description


phrase String User input
i int Loop variable
out String Stores the output string

You might also like