Professional Documents
Culture Documents
Comp Pr0g
Comp Pr0g
{
//First overloaded function
void check(String str,char ch)
{
int l=str.length();
int count=0;
for(int i=0;i<l;i++)
{
char ch1=str.charAt(i);
if(ch1==ch)
count++;//count the frequency of given character
}
System.out.println("Number of"+ch+"present="+count);
}//end of first method
//Second method begins
void check(String s1)
{
s1=s1.toLowerCase();
int l=s1.length();
for(int i=0;i<l;i++)
{
char ch1=s1.charAt(i);
if(ch1=='a' || ch1=='e' || ch1=='i' || ch1=='o' || ch1=='u')
System.out.print(ch1+" ");//Display the vowels
}
}
}
ALGORITHM
Step 1: Start
1.1 : Accept a string and a character.
1.2 : Find length.
1.3 :Set count as 0.
1.4 Repeat for i=0,1,2…l
Extract the characters of the string.
If character(ch1==ch)
Increase counter variable by 1.
End for.
Step 3: Stop.
VDT
}
int max=num[0],min=num[0];int sum=0;
//Find largest and smallest number and sumof the elements
for(int i=0;i<20;i++)
{
if(num[i]>max)
max=num[i];
if(num[i]<min)
min=num[i];
sum+=num[i];
}
//Display largest,smallest number and sum of the elements
System.out.println("Largest number="+max);
System.out.println("Smallest number="+min);
System.out.println("Sum of the number="+sum);
}
}
VDT
import java.util.*;
class prog5//class begins
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String words[]=new String[20];//Initialization of the array
int max;
//Input 20 words in an array
System.out.println("Enter words:");
for(int i=0;i<20;i++)
{
words[i]=sc.next();
}
//Sorting in descending order
for(int i=0;i<19;i++)
{
max=i;
for(int j=i+1;j<20;j++)
{
if(words[j].compareTo(words[max])>0)
max=j;
}
String t=words[i];
words[i]=words[max];
words[max]=t;
}
System.out.println("Words arranged in descending order are:");
for(int i=0;i<19;i++)
{
System.out.println(words[i]);
}
}
}
VDT
class prog6
{
public static void main(String args[]) {
import java.util.*;
class prog7//class begins
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of students:");
int n=sc.nextInt();
String name[]=new String[n];//Initialization of name array
int roll[]=new int[n];//Initialization of totalmarks array
int sub1[]=new int[n];
int sub2[]=new int[n];
int sub3[]=new int[n];
int avg=0;
//Input name,rollno and three subject marks
for(int i=0;i<n;i++)
{
sc.nextLine();
System.out.println("Enter name of the students:");
name[i]=sc.nextLine();
System.out.println("Enter Roll no:");
roll[i]=sc.nextInt();
System.out.println("Enter three subject marks:");
sub1[i]=sc.nextInt();
sub2[i]=sc.nextInt();
sub3[i]=sc.nextInt();
avg=(sub1[i]+sub2[i]+sub3[i])/3;//Calculation of average marks
System.out.println("Average="+avg);//Display Average marks
//Remarks on average marks
if(avg>=85 && avg<=100)
System.out.println("EXCELLENT");
if(avg>=75 && avg<=84)
System.out.println("DISTINCTION");
if(avg>=60 && avg<=74)
System.out.println("FIRST CLASS");
if(avg>=40 && avg<=59)
System.out.println("PASS");
if(avg<40)
System.out.println("POOR");
}
}
}
VDT
class prog8
{
void joystring(String s,char ch1,char ch2)
{
String s1=s.replace(ch1,ch2);
System.out.println(s1);
}
void joystring(String s)
{
int first_index=s.indexOf(' ');
int last_index=s.lastIndexOf(' ');
System.out.println("First index="+first_index);
System.out.println("Last index="+last_index);
}
void joystring(String s1,String s2)
{
s1=s1+" ";
String s3=s1.concat(s2);
System.out.println("Combined string="+s3);
}
}
VDT
import java.util.*;
class prog9//class begins
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String name[]=new String[20];//Initialization of the array
//Input 20 names in an array
System.out.println("Enter names:");
+
for(int i=0;i<20;i++)
{
name[i]=sc.next();
}
//Sorting in descending order
for(int i=0;i<19;i++)
{
for(int j=0;j<(19-i);j++)
{
if(name[j].compareTo(name[j+1])<0)
{
String t=name[j];
name[j]=name[j+1];
name[j+1]=t;
}
}
}
//Display sorted array
System.out.println("sorted array:");
for(int i=0;i<20;i++)
{
System.out.println(name[i]);
}
}
}
VDT
class prog10
{
public void checkNumber() {
while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}
if (count != 2)
System.out.println("Invalid input, please enter a 2-digit number");
else if ((digitSum + digitProduct) == orgNum)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit number");
}
}
import java.util.Scanner;
class prog11
{
double area(double a, double b, double c) {
double s = (a + b + c) / 2;
double x = s * (s-a) * (s-b) * (s-c);
double result = Math.sqrt(x);
return result;
}
class prog12
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Term Deposit");
System.out.println("Type 2 for Recurring Deposit");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
double p = 0.0, r = 0.0, a = 0.0;
int n = 0;
switch (ch) {
case 1:
System.out.print("Enter Principal: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in years: ");
n = in.nextInt();
a = p * Math.pow(1 + r / 100, n);
System.out.println("Maturity amount = " + a);
break;
case 2:
System.out.print("Enter Monthly Installment: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in months: ");
n = in.nextInt();
a = p * n + p * ((n * (n + 1)) / 2) * (r / 100) * (1 / 12.0);
System.out.println("Maturity amount = " + a);
break;
default:
System.out.println("Invalid choice");
}
}
}
class prog13//class begins
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int y[]={1982,1987,1993,1996,1999,2003,2006,2007,2009,2010};//Initialization of the
array
int lb=0,ub=9,mid,yr;boolean flag=false;
System.out.println("Enter the year to be searched:");
yr=sc.nextInt();
//Binary search
while(lb<=ub)
{
mid=(lb+ub)/2;
if(y[mid]<yr)
lb=mid+1;
if(y[mid]>yr)
ub=mid-1;
if(y[mid]==yr)
{
flag=true;
break;
}
}//end of while loop
if(flag==true)
System.out.println("Record exists");
else
System.out.println("Record does not exists");
}
}
VDT
Prog14
import java.util.Scanner;
class FruitJuice
{
private int product_code;
private String flavour;
private String pack_type;
private int pack_size;
private int product_price;
public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
class prog15
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the ISBN: ");
long isbn = in.nextLong();
}
//Bubble Sorting in descendin order
for(int i=0;i<9;i++)
{
for(int j=0;j<(9-i);j++)
{
if(num[j]<num[j+1])
{
int t=num[j];
num[j]=num[j+1];
num[j+1]=t;
}
}
}
//Display sorted array
System.out.println("sorted array:");
for(int i=0;i<10;i++)
{
System.out.println(num[i]);
}
}
}
VDT
import java.util.*;
class prog19//class begins
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num[]=new int[15];//Initialization of num array
}
//Interchange the consecutive number
for(int i=0;i<10;i+=2)
{
int t=num[i];
num[i]=num[i+1];
num[i+1]=t;
}
//Final Array
System.out.println("Final Array");
for(int i=0;i<10;i++)
{
System.out.print(num[i]+",");
}
}
}
VDT
class prog20
{
public void polygon(int n, char ch) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(ch);
}
System.out.println();
}
}
int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) {
break;
}
}
int t = num;
int sum = 0;
while (t != 0) {
int d = t % 10;
sum += fact(d);
t /= 10;
}
if (sum == num)
System.out.println(num + " is a special number");
else
System.out.println(num + " is not a special number");
}
}
public class Prog24
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = in.nextLine();
str = str.toLowerCase();
String newStr = "";
int len = str.length();
if (str.charAt(i) == 'a' ||
str.charAt(i) == 'e' ||
str.charAt(i) == 'i' ||
str.charAt(i) == 'o' ||
str.charAt(i) == 'u') {
}
else {
newStr = newStr + ch;
}
}
System.out.println(newStr);
}
}
import java.util.Scanner;
if (a > b) {
System.out.println(a);
}
else {
System.out.println(b);
}
if (x > y) {
System.out.println(a);
}
else {
System.out.println(b);
}
int l1 = a.length();
int l2 = b.length();
import java.util.Scanner;
System.out.println("Number\tFrequency");
int count = 0;
for (int i = 0; i < n - 1; i++) {
count++;
if (arr[i] != arr[i + 1]) {
System.out.println(arr[i] + "\t" + count);
count = 0;
}
}
import java.util.*;
class Project28
{
boolean prime(int n)
{
for(int i=2;i<n;i++)
{
if (n%i==0)return false;
}
return true;
}
public static void main(String args[])
{
int i;
Scanner sc=new Scanner(System.in);
i=sc.nextInt(); //79
Project28 abc = new Project28();
if(abc.prime(i)==false){
System.out.println("Enter Prime Number only");
return ;
}
else{
int temp=0;
while(i!=0){
int b;
b=i%10;
temp=temp*10+b;
i=i/10;
}
if(abc.prime(temp)==true)
System.out.println(temp+" Is a Prime Number");
else
System.out.println(temp+" Is not a Prime Number");
}
}
import java.util.*;
class Project29
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
String abc=sc.next();
abc=abc.toUpperCase();
int l = abc.length();
if (abc.charAt(0)==abc.charAt(l-1))
{
boolean flag=true;
for(int i=1;i<l/2;i++)
{
if(abc.charAt(i)!=abc.charAt(l-1-i)){
flag=false;
break;
}
}
if(flag)
System.out.println("Palindrome Word");
else if(!flag)
System.out.println("Special Word");
}else
System.out.println("Neither Special nor Palindrome");
}
}
import java.util.*;
class Project30
{
public static void main (String args[])
{
int input;
Scanner sc = new Scanner(System.in);
input=sc.nextInt();
switch(input){
case 1: int i;
int j;
for(i=7;i>=1;i--)
{
for(j=i;j>=1;j--)
System.out.print(j+" ");
System.out.println();
}
break;
case 2:
int a,b;
for(a=1;a<=9;a++)
{
int d=8;
System.out.print(a+" ");
int temp=a;
for(b=1;b<a;b++){
temp+=d;
System.out.print(temp+" ");
d--;
}
System.out.println();
}
break;
case 3:
for(int x=1; x<=9;x++)
{
int alpha = 65;
for(int y=0; y<x; y++)
{
System.out.print((char)(alpha+y)+" ");
}
System.out.println();
}
break;
default:
}
}
}