Computer Assignmen1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Computer Assignment

1) import java.util.*;
class Marks{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of students");
int n=in.nextInt();
int roll[]=new int[n];
double m1[]=new double[n];
double m2[]=new double[n];
double m3[]=new double[n];
String name[]=new String[n];
for(int i=0;i<n;i++){
System.out.println("Enter the roll numbers,name and marks in 3
subjects for"+(i+1)+"student");
roll[i]=in.nextInt();
name[i]=in.next();
m1[i]=in.nextDouble();
m2[i]=in.nextDouble();
m3[i]=in.nextDouble();
double avg=(m1[i]+m2[i]+m3[i])/3.0;
if(avg>=85&&avg<=100){
System.out.println("EXCELLENT");
}
else if(avg>=75&&avg<=84){
System.out.println("DISTINCTION");
}
else if(avg>=60&&avg<=74){
System.out.println("FIRST CLASS");
}
else if(avg>=40&&avg<=59){
System.out.println("PASS");
}
else{
System.out.println("POOR");
}
}
}
}

2) import java.util.*;
class Cities{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String cities[]=new String[10];
int std[]=new int[10];
for(int i=0;i<10;i++){
System.out.println("Enter city:");
cities[i]=in.next();
System.out.print("Enter std code:");
std[i]=in.nextInt();
}
System.out.println("Enter city name to search:");
String target=in.next();
boolean searchSuccessful=false;
for(int i=0;i<10;i++){
if(cities[i].equals(target)){
System.out.println("Search successful");
System.out.println("City:"+cities[i]);
System.out.println("STD code:"+std[i]);
searchSuccessful=true;
break;
}
}
if(!searchSuccessful){
System.out.println("Search unsuccessful,No such city in the list");
}
}
}

3) import java.util.*;
class BinarySearch{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int[] m={5,7,9,11,15,20,30,45,89,97};
int i,ub=9,lb=0,p,f=0,ns;
System.out.println("Enter the number to be searched");
ns=in.nextInt();
for(i=0;i<10;i++){
p=(ub+lb)/2;
if(m[p]>ns){
lb=p+1;
}
else if(m[p]<ns){
ub=p-1;
}
if(m[p]==ns){
f++;
break;
}
if(f==1){
System.out.println(ns+"found at index"+ i);
}
else{
System.out.println("Search element not found");
}
}
}
}
4) import java.util.*;
class Max_Min{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter numbers");
int a[]=new int[20];
for(int i=0;i<20;i++){
a[i]=in.nextInt();
}
int max=a[0];
int min=a[0];
int sum=0;
for(int i=0;i<20;i++){
if(a[i]>max){
max=a[i];
}
else if(a[i]<min){
min=a[i];
}
sum=sum+a[i];
}
System.out.println("Largest number is"+max);
System.out.println("Smallest number is"+min);
System.out.println("sum is"+sum);
}
}
5) import java.util.*;
class TotalMarks{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int n;
System.out.println("Enter number of students");
n=in.nextInt();
String name[]=new String[n];
double marks[]=new double[n];
double ave,total=0.0;
for(int i=0;i<n;i++){
System.out.println("Enter name and total marks");
name[i]=in.nextLine();
marks[i]=in.nextDouble();
total=total+marks[i];
}
ave=total/n;
System.out.println("Name " + " "+"Deviation");
for(int i=0;i<n;i++){
System.out.println(name[i]+""+(marks[i]-ave));

}
}
}
6) import java.util.*;
class Input{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter any 20 letters");
int vc=0,cc=0;
for(int i=1;i<=20;i++){
char ch=in.next().charAt(0);
if(Character.isUpperCase(ch)==true){
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){
vc++;
}
else{
cc++;
}
}
else{
ch=Character.toUpperCase(ch);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){
vc++;
}
else{
cc++;
}
}
}
System.out.println("Number of Vowels = " + vc);
System.out.println("Number of Consonants = " + cc);
}
}

7) import java.util.*;
class Wonders{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String wonders[]={"CHICHEN ITZA","CHRIST THE REDEEMER","TAJ
MAHAL","GREAT WALL OF CHINA","MACHU PICHU","PETRA","COLOSSEUM"};
String
locations[]={"MEXICO","BRAZIL","INDIA","CHINA","PERU","JORDAN","ITALY"};
System.out.print("Enter country:");
String c=in.nextLine();
int i;
for(i=0;i<locations.length;i++){
if(locations[i].equals(c)){
System.out.println(locations[i]+"-"+wonders[i]);
break;
}
}
if(i==locations.length){
System.out.println("Sorry not found");
}
}
}
8) import java.util.*;
class Square{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int m[]=new int[10];
int arr[]=new int[10];
System.out.println("Enter the numbers in the array");
for(int i=0;i<10;i++){
m[i]=in.nextInt();
}
System.out.println("The squared array is:");
for(int i=0;i<10;i++){
arr[i]=m[i]*m[i];
System.out.println(arr[i]+"");
}
}
}

9) import java.util.*;
class NaturalSum{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int m[]=new int[76];

for(int i=0;i<76;i++){
m[i]= i;
}
int sum=0;
for(int i=0;i<76;i++){
sum=sum+m[i];
}
System.out.println("The sum of the first 75 natural numbers are"+sum);
}
}

10) import java.util.*;


class ReverseArray{
public static void main(String args[]){
int a[]=new int[10];
int i,j=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the elements of array");
for(i=0;i<10;i++){
a[i]=in.nextInt();
}
System.out.println("Elements of the array are=");
for(i=0;i<10;i++){
System.out.println(""+a[i]);
}
for(i=0, j=9;i<5;i++,j--){
a[i]=a[i]+a[j];
a[j]=a[i]-a[j];
a[i]=a[i]-a[j];
}
System.out.println("\nReverse Array is=");
for(i=0;i<10;i++){
System.out.print(""+a[i]);
}

}
}

11) import java.util.*;


import java.util.Scanner;

class FrequencyVowels{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String st;
System.out.println("Enter new string");
int n[]=new int[5];
for(int i=0;i<5;i++){
n[i]=0;
}
int num=0;
st=in.next();
int s=st.length();
System.out.println(s);
for(int i=0;i<s;i++){
if(st.charAt(i)=='a'){
n[0]++;
}
else if(st.charAt(i)=='e'){
n[1]++;
}
else if(st.charAt(i)=='i'){
n[2]++;
}
else if(st.charAt(i)=='o'){
n[3]++;
}
else if(st.charAt(i)=='u'){
n[4]++;
}
num=n[0]+n[1]+n[2]+n[3]+n[4];
System.out.println("No. of a" + n[0]);
System.out.println("No. of e" + n[1]);
System.out.println("No. of i" + n[2]);
System.out.println("No. of o" + n[3]);
System.out.println("No. of u" + n[4]);
System.out.println("Total number of vowels="+num);
}
}
}
12) import java.util.*;
class PosNeg{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int m[]=new int[20];
int min=m[0],max=m[0],p=0,n=0;
System.out.println("Enter the numbers in the array");
for(int i=0;i<20;i++){
m[i]=in.nextInt();
}
for(int i=0;i<20;i++){
if(min>m[i]){
min=m[i];
}
if(max<m[i]){
max=m[i];
}
if(m[i]>0){
p++;
}
else if(m[i]<0){
n++;
}
}
System.out.println("The smallest number is="+min);
System.out.println("The largest number is="+max);
System.out.println("Negative numbers are"+n);
System.out.println("Positive numbers are"+p);
}
}

13) import java.util.*;


class Choice{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Menu");
System.out.println("1. Generating Fibonacci");
System.out.println("2. Sum of Digits");
System.out.println("Enter your choice(1/2)");
int choice=in.nextInt();
switch(choice){
case 1:
{
int i;
int term[]=new int[10];
term[0]=0;
term[1]=1;
System.out.println(term[0]+""+term[1]+"");
for(i=2;i<10;++i){
term[i]=term[i-2]+term[i-1];
System.out.println(term[i]);
}
break;
}
case 2:
{
int num,digit,quotent,sum=0;
System.out.println("Enter an Integer number");
num=in.nextInt();
while(num>=0){
digit=num%10;
sum=sum+digit;
num=num/10;
}
System.out.println("Sum of the digit(s)="+sum);
break;
}
default: System.out.println("Wrong choice");
}
}
}

14) import java.util.*;

class OddEvenSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

int oddSum = 0, evenSum = 0;

for (int i = 0; i < arr.length; i++) {


if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
}

System.out.println("Sum of Odd numbers = " + oddSum);


System.out.println("Sum of Even numbers = " + evenSum);
}
}

15) import java.util.*;

public class TempFC


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double arr[] = new double[20];

System.out.println("Enter 20 temperatures in degree Fahrenheit");


for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextDouble();
}

System.out.println("Temperatures in degree Celsius");


for (int i = 0; i < arr.length; i++) {
double tc = 5 * ((arr[i] - 32) / 9);
System.out.println(tc);
}
}
}

16) import java.util.*;

public class Numbers2


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

for (int i = 0; i < arr.length; i++) {


if (arr[i] < 0)
System.out.print(arr[i] + ", ");
}

for (int i = 0; i < arr.length; i++) {


if (arr[i] >= 0)
System.out.print(arr[i] + ", ");
}
}
}

17) import java.util.*;

class PrimeNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {

int c = 0;
for (int j = 1; j <= arr[i]; j++) {
if (arr[i] % j == 0) {
c++;
}
}

if (c == 2)
System.out.print(arr[i] + ", ");
}
}
}

18) import java.util.*;

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

String name[] = new String[50];


int marks[] = new int[50];
int total = 0;

for (int i = 0; i < name.length; i++) {


System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter marks of student " + (i+1) + ": ");
marks[i] = in.nextInt();
total += marks[i];
in.nextLine();
}

double avg = (double)total / 50;


System.out.println("Subject Average Marks = " + avg);
int hm = 0;

for (int i = 1; i < marks.length; i++) {


if (marks[i] > marks[hm])
hm = i;
}

System.out.println("Highest Marks = " + marks[hm]);


System.out.println("Name = " + name[hm]);
}
}

19) import java.util.*;


class EvenOdd
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int i = 0;

int arr[] = new int[20];


int even[] = new int[20];
int odd[] = new int[20];

System.out.println("Enter 20 numbers:");
for (i = 0; i < 20; i++) {
arr[i] = in.nextInt();
}

int en = 0, on = 0;
for (i = 0; i < 20; i++) {
if (arr[i] % 2 == 0)
even[en++] = arr[i];
else
odd[on++] = arr[i];
}

System.out.println("Even Numbers:");
for (i = 0; i < en; i++) {
System.out.print(even[i] + " ");
}

System.out.println("Odd Numbers:");
for (i = 0; i < on; i++) {
System.out.print(odd[i] + " ");
}
}
}

20) import java.util.*;

public class PerfectSquare


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Perfect Squares are:");


for (int i = 0; i < arr.length; i++) {
double sr = Math.sqrt(arr[i]);
if ((sr - Math.floor(sr)) == 0)
System.out.print(arr[i] + ", ");
}
}
}

21) import java.util.*;

class Student
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int studentDetails[] = new int[200];

System.out.println("Enter student details");


for (int i = 0, j = 1; i < 200; i = i + 5, j++) {
System.out.print("Student " + j + " roll number: ");
studentDetails[i] = in.nextInt();
System.out.print("Student " + j + " English Marks: ");
studentDetails[i+1] = in.nextInt();
System.out.print("Student " + j + " Maths Marks: ");
studentDetails[i+2] = in.nextInt();
System.out.print("Student " + j + " Physics Marks: ");
studentDetails[i+3] = in.nextInt();
System.out.print("Student " + j + " Chemistry Marks: ");
studentDetails[i+4] = in.nextInt();
}

for (int i = 0; i < 200; i = i + 5) {


System.out.println("Roll No: " + studentDetails[i]);
if (studentDetails[i+1] > 34 &&((studentDetails[i+2] > 34 &&
studentDetails[i+3] > 34) ||(studentDetails[i+2] > 34 && studentDetails[i+4] >
34) ||
(studentDetails[i+3] > 34 && studentDetails[i+4] > 34))) {
System.out.println("Promotion is granted.");
}
else {
System.out.println("Promotion is not granted.");
}
}
}
}

22) import java.util.*;


class Truncate
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double a[] = new double[10];
int b[] = new int[10];

System.out.println("Enter 10 decimal numbers");


for (int i = 0; i < a.length; i++) {
a[i] = in.nextDouble();
b[i] = (int)a[i];
}

System.out.println("Truncated numbers");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + ", ");
}
}
}

23) import java.util.*;

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

int rollNo[] = new int[50];


int sA[] = new int[50];
int sB[] = new int[50];
int sC[] = new int[50];
double avg[] = new double[50];

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


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
System.out.print("Subject A Marks: ");
sA[i] = in.nextInt();
System.out.print("Subject B Marks: ");
sB[i] = in.nextInt();
System.out.print("Subject C Marks: ");
sC[i] = in.nextInt();
avg[i] = (sA[i] + sB[i] + sC[i]) / 3.0;
}

System.out.println("\nRoll No\tAverage Marks");


for (int i = 0; i < 50; i++) {
System.out.println(rollNo[i] + "\t" + avg[i]);
}
System.out.println("\nStudents with Average above 80:");
for (int i = 0; i < 50; i++) {
if (avg[i] > 80)
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average below 40:");


for (int i = 0; i < 50; i++) {
if (avg[i] < 40)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
}
}

24) import java.util.*;

public class Merge


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int P[] = new int[6];


int Q[] = new int[4];
int R[] = new int[10];
int i = 0;
System.out.println("Enter 6 elements of array P:");
for (i = 0; i < P.length; i++) {
P[i] = in.nextInt();
}

System.out.println("Enter 4 elements of array Q:");


for (i = 0; i < Q.length; i++) {
Q[i] = in.nextInt();
}

i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}

int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}

System.out.println("Elements of Array R:");


for (i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}
}

25) import java.util.*;

public class YearSearch


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};

System.out.print("Enter graduation year to search: ");


int year = in.nextInt();

int l=0,h=9,k=0;
while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
k++;
break;
}
else if (n[m] < year) {
l = m + 1;
}
else {
h = m - 1;
}
}

if (k == 1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}

26) import java.util.*;

public class StandardGrades


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter number of students: ");
int n = in.nextInt();

int rollNo[] = new int[n];


String name[] = new String[n];
int s1[] = new int[n];
int s2[] = new int[n];
int s3[] = new int[n];
double avg[] = new double[n];

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


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
avg[i] = (s1[i] + s2[i] + s3[i]) / 3.0;
}

System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
remark = "Distinction";
else
remark = "Excellent";
System.out.println(rollNo[i] + "\t" + name[i] + "\t" + remark);
}
}
}

You might also like