Computer Project - TERM II
Computer Project - TERM II
Class- 10 Section- L
Roll No.-13453
Output:
1)
2)
3)
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)
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
Output:
1)
2)
3)
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 ;
}
}
}
}
Output:
1)
2)
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
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)
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)
Output:
1)
2)
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)