You are on page 1of 30

Student Profile

Name- Pratyush Agrawal

Class- 10 Section- L

Roll No.-13453

Subject- Computer Applications


Index

Serial No. Program Name Page Number


01
02
03
04
05
06 Counting and printing the number of vowels present
in names of 5 people.
Printing the total number of vowels in all 5 names
07
08
09
10
11
12
13
Program 6:
/*writing a program to accept 5 names from the user and count and print the number of vowels
present in each name and the total number of vowels present*/
import java.util.*; //importing utilities package
class array_1 //creating class
{
    public static void main(String args[])
    {
        Scanner sc= new Scanner(System.in);
         System.out.println("Enter the first name of 5 people");//asking user to enter names 
        //declaration and initialization of variables
        String names[]=new String[5]; //array to store the input from the user
        int x[]=new int[5]; // array to store the number of vowels present in each name
        int b=0,s=0;
        String a=""; 
       //accepting names
        for(int i=0;i<5;i++)
        {
            names[i]=sc.next();
        }
        //checking if any character of is a vowel
        for(int i=0;i<5;i++)
        {
            a=names[i];
            a=a.toLowerCase();//converting the given string into lower case characters
            for(int c=0;c<a.length();c++)
            {
                if((a.charAt(c))=='a'||a.charAt(c)=='e'||a.charAt(c)=='i'||a.charAt(c)=='o'||
a.charAt(c)=='u')//checking for presence of a vowel
                    b++;
            }
            x[i]=b;//assigning number of vowels in a word to an array 'x'
            s=s+x[i];//adding the total number of vowels in all the names
            b=0;
        }
        //printing the name along with the number of vowels present
        for(int i=0;i<5;i++)
        {
            if(x[i]==1)
                System.out.println("The name "+names[i]+" has "+x[i]+" vowel.");
            else if(x[i]>1)
                System.out.println("The name "+names[i]+" has "+x[i]+" vowels.");
            else
                System.out.println("The name "+names[i]+" has no vowel.");
        }
        System.out.println("The total number of vowels present in all 5 names: "+s);
        }
}

Output:
1)

2)
3)

Variable Description Table


Name of the Data type Description
Variable
b int counts the number of
vowels in each name

s int adds and stores the


total number of
vowels

a String Stores the name and


is updated each time
the outer loop runs

i int Iterative variable

c int Iterative variable

names String[] Array to store the


input from the user
x int[] Array to store the
number of vowels
present in each name

Program 7:
/*writing a program in java, asking a user to enter a number and checking if the entered number is a
narcissistic number or not.
A narcissistic number is a number that is the sum of its own digits each raised to the power of the
number of digits. Eg- 153 13 +53 +33 => 1+125+27=>153 */
import java.util.*;
class array_2
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int n[]=new int[5]; //array to store the input by the user
int ab[]=new int[5]; //array used to calculate the sum of the cube of the digits of the number
int copy[]=new int[5]; //array to check if the sum is equal to the number
int co[]=new int[5]; //array for storing the count of digits of the numbers
int s[]=new int[5]; //array used to store the sum of the cube of the digits of the number
int ld=0,c=0,la=0;
System.out.println(“Enter a set of 5 numbers”);
for(int i=0;i<5;i++)
{
n[i]=sc.nextInt();
copy[i]=n[i];
ab[i]=n[i];
}
for(int i=0;i<5;i++)
{
while(n[i]>0)
{
ld=n[i]%10;//extracting digits
c++;//counting the digits present in the number
co[i]+=c; //storing the count of digits
c=0;
n[i]/=10;
}
}
for(int i=0;i<5;i++)
{
while(ab[i]>0)
{
la=ab[i]%10;
s[i]+=(Math.pow(la,co[i])); //adding the sum of the cube of the digits
ab[i]/=10;
}
if(copy[i]==s[i])//checking for narcissistic number
{
System.out.println(copy[i]+” is a narcissistic number”);
}
else
{
System.out.println(copy[i]+” is not a narcissistic number”);
}
}
}
}
Output:

1)

2)

Variable Description Table:


Name of the Data type Description
Variable

ld int Extracts the digits

c int Counts the digits present in


the number

la int Extracts the digits

i int Iterative variable

n int[] Array to store the input by


the user

ab int[] Array used to calculate the


sum of the cube of the digits
of the number
copy int[] Array used to check if the
number is narcissistic

co int[] Array used to store the count


of digits in each number

s int[] Array used to store the sum


of the cube of the digits of
the number

Program 8:
/*writing a program in java to accept numbers from the user and store the even and odd numbers in
two different arrays, and then printing the two arrays with count of even and odd numbers*/
import java.util.*;
class array_3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“How many numbers do you want to enter?”);
int n=sc.nextInt();
int ar[]=new int[n]; //array accepting the set of numbers entered by the user
int even[]=new int[n]; //array storing the even numbers present in the array “ar[]”
int odd[]=new int[n]; //array storing the odd numbers present in the array “ar[]”
int c=0,c1=0,e=0,o=0;
System.out.println(“Enter “+n+” numbers”);
for(int i=0;i<n;i++)
{
ar[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
if(ar[i]%2==0) //checking for numbers divisible by 2
{
even[e]=ar[i];
e++;
c++; //counting the even numbers present
}
else
{
odd[o]=ar[i];
o++;
c1++; //counting the odd numbers present
}
}
//printing the even numbers
System.out.println(“Even Numbers: “);
for(int i=0;i<e;i++)
System.out.print(even[i]+” “);
// rinting the odd numbers
System.out.println(“\nOdd numbers: “);
for(int i=0;i<o;i++)
System.out.print(odd[i]+” “);
//printing the count of odd and even numbers present
System.out.println(“\nCount of even numbers: “+c);
System.out.println("Count of odd numbers: "+c1);
}
}
Output:
1)

2)

3)
Variable Description Table
Name of the Data type Description
Variable
ar int[] Array accepting the
set of numbers
entered by the users

even int[] Array storing the


even umbers present
in the array “ar[]”
odd int[] Array storing the odd
numbers present in
the array “ar[]”
n int Stores how many
numbers the user
wants to enter
c int Counts the even
numbers present

c1 int Counts the odd


numbers present
e int Index of the array
“even[]”

o int Index of the array


‘odd[]”

i int Iterative variable


Program 9:
/*write a program in java to accept two different sets of numbers, and print both the sets in ascending
order*/
import java.util.*;
class array_4
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//asking user to enter the length of the two arrays
System.out.print("Enter length of array 1: ");
int l=sc.nextInt();
System.out.print("Enter length of array 2: ");
int l1=sc.nextInt();
int P[] = new int[l];//array storing the first set of numbers
int Q[] = new int[l1]; //array storing the second set of numbers
int R[] = new int[l+l1];//merged array
int i=0,j=0;
//accepting values of arrays
System.out.println("Enter elements of first array");
for(i=0;i<l;i++)
P[i]=sc.nextInt();
System.out.println("Enter elements of second array ");
for(i=0;i<l1;i++)
Q[i]=sc.nextInt();
i=0;
while(i<l)
{
R[i]=P[i];//copying values of first array in the resultant array
i++;
}
while(j<l1)
{
R[i++]=Q[j++]; //copying value of second array in the resultant array
}
System.out.println("Elements in ascending order :");
for(i=0;i<R.length-1;i++)
{
//using bubble sort, arranging the elements in ascending order
for(int z=0;z<R.length-1;z++)
{
if(R[z]>R[z+1])
{
int t=R[z];
R[z]=R[z+1];
R[z+1]=t;
}
}
}
for(i=0;i<R.length;i++)
{
System.out.print(R[i] + " "); //printing the merged array in ascending order
}
}
}

Output:

1)
2)

3)

Variable Description Table


Name of the Data type Description
Variable
l int Stores the length of first
array

l1 int Stores the length of


second array

P int[] Array stores the first set


of number

Q int[] Array stores the second


set of number

R int[] Merged array, containing


the elements of both the
“P[]” and “Q[]”
i int Iterative variable

j int Iterative variable

z int Iterative variable

t int holds greater value


between two elements

Program 10:
/*writing a program to accept a set of numbers from the user, and print all the prime numbers present
in the entered set*/
import java.util.*;
class array_5
{
public static void Primes(int nums[])
{
for(int i=0;i<nums.length;i++)
{
boolean isPrime = true;
if(nums[i]<=0)
isPrime= false ;
for(int j=2;j<=(int)Math.sqrt(nums[i]);j++)
{
if(nums[i]%j==0) //checking if the number entered has factors other than 1 and itself
{
isPrime = false;
break;
}
}
if(isPrime==false)
nums[i] =1 ;
}
}

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int lim = sc.nextInt(); //asking user to enter number of elements
int nums[]= new int[lim] ;
System.out.println("Enter "+lim+" elements");
for(int i=0;i<lim;i++)
nums[i] =sc.nextInt() ;
Primes(nums); //calling function Primes(int nums[])
System.out.println("Prime Numbers present-");
for(int i=0;i<lim;i++)
{
if(nums[i]!=1)
System.out.print(nums[i]+" ");
}

}
}

Output:

1)

2)

Name of the Variable Data type Description

lim int Stores the length of array

isPrime boolean Stores true or false if the


number is divisible by
numbers other than 1 and
itself

nums int[] Array stores the set of


numbers entered by the
user
i int Iterative variable

j int Iterative variable

Variable Description Table

Program 11:
/* writing a program in java, to accept a string from the user and to replace each consonant with the
previous letter and checking if the previous letter is a vowel or not.
if yes, then replacing it with the next letter
if no, replcing it with the previous letter */
import java.util.*;
class string_5
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter sentence-");
String st=sc.nextLine();
String word="";
char let_bef;
for(int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
ch=Character.toUpperCase(ch);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
word+=ch;
if(ch!='A'&&ch!='E'&&ch!='I'&&ch!='O'&&ch!='U')
{
let_bef=(char)((int)ch-1); //subtracting 1 from the ASCII code of the letter, then converting
the ASCII code to a character
if(let_bef=='A'||let_bef=='E'||let_bef=='I'||let_bef=='O'||let_bef=='U')
let_bef=(char)((int)ch+1); //adding 1 to thhe ASCII code of the letter, then converting the
ASCII code to a character
word+=let_bef;
}
if(ch==' ')
word+=" ";
}
System.out.println("Modified String-\n"+word );
}
}

Output:
1)

2)
Variable Description Table
Name of the Variable Data type Description

st String Stores the entered string


from the user

word String Stores the modified


string

let_bef char Stores the letter before


the character; changes
every time the loop runs

ch char Stores the character


present in the string, and
changes every time the
loop runs
i int Iterative variable

Program 12:
/*writing a program in java to accept a word from the user and check if the
word is special or palindrome or none
special words are words that have same starting letter as the ending...EG---
> Window
palindrome words are words that do not change when reversed...EG-
>WOW
-all palindrome words are special words but not vice versa*/
import java.util.*;
class string_1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word"); //asking user to enter a word
String str=sc.next();
str=str.toUpperCase();
int l=str.length();
if(str.charAt(0)==str.charAt(l-1)) //checking if the first character is
equal to the last character
{
boolean palin=true;
for(int i=1;i<l/2;i++)
{
if(str.charAt(i)!=str.charAt(l-1)) //checking if each character is
equal to the character from the last
{
palin=false;
break;
}
}
if(palin==true)
System.out.println("Palindrome word");
else
System.out.println("Special word");
}
else
System.out.println("Neither a Special nor a Palindrome word");
}
}
Output:
1)

2)

Variable Description Table


Name of the Data type Description
Variable

str String Stores the word


entered by the user

l int Stores the length of


the word entered by
the user

palin Boolean Stores true if the


word is palindrome;
false if special
i int Iterative variable

Program 13:
/*writing a program in java to accept the names of 5 cities and to
print the ones which start with a consonant, and end with a vowel*/
import java.util.*;
class string_2
{
public static boolean vowel(char ch)
{
char letter = Character.toUpperCase(ch);
if (letter == 'A' ||letter == 'E' ||letter == 'I' ||letter == 'O' ||letter
== 'U') //checking if the letter is a vowel
return true;
return false;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String c[] = new String[5];
System.out.println("Enter city names");//asking user to enter
city names
for (int i=0;i<c.length;i++)
c[i] = sc.nextLine(); //taking input from user
System.out.println("\nCities starting with a consonant & ending
with a vowel:");
for (int i=0;i<c.length;i++)
{
if (!vowel(c[i].charAt(0))==true &&
vowel(c[i].charAt(c[i].length() - 1))==true) //checking if the first and
last letter is a vowel or not
System.out.print(c[i]+" ");
}
}
}
Output:
1)

2)

Variable Description Table


Name of the Data type Description
Variable

letter char Stores the character


in uppercase

c String[] Stores the names of


the cities entered by
the user
i int Iterative variable
Program 14:
/*writing a program in java to accept 5 names and a character and then
printing the names starting with the entered character */
import java.util.*;
class string_3
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String name[]=new String[5];
System.out.println("Enter 5 names-");//asking user to enter 5 names
for (int i=0;i<name.length;i++)
{
name[i] = sc.nextLine();
}
System.out.print("Enter a letter: "); //asking user to enter the letter
char ch = sc.next().charAt(0);
System.out.println("Names starting with '"+ch+"'-");
ch = Character.toUpperCase(ch);
for(int i=0;i<name.length;i++)
{
if(Character.toUpperCase(name[i].charAt(0)) == ch)//checking if
the first letter of each name is the same as the letter entered
{
System.out.println(name[i]);
}
}
}
}

Output:
1)

2)

Variable Description Table


Name of the Data type Description
Variable
name String[] Stores 5 names
entered by the
user

ch char Stores the


character stored
by the user

i int Iterative variable

Program 15:
/*writing a program in java to accept a string and count and print the
number of double letter sequences present */
import java.util.*;
class string_4
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter string to check the number of double letter
sequence present"); //asking user to enter a string
String st=sc.nextLine();
st=st.toUpperCase();
int l=st.length();
int c=0;
for(int i=0;i<l-1;i++)
{
if(st.charAt(i)==st.charAt(i+1)) //checking if the letter is equal to
the letter after
c++; //counting the number of times the letters are equal

}
System.out.println("double letter sequence present: "+c);
}
}
Output:
1)

2)

Variable Description Table:


Name of the Data type Description
Variable

st String Stores the string


entered by the user
l int Stores the length of
the string entered by
the user

c int stores the number of


double letter
sequences present

i int Iterative variable

You might also like