0% found this document useful (0 votes)
17 views13 pages

Computer Solution

Uploaded by

sdrive11406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

Computer Solution

Uploaded by

sdrive11406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Q1.

import java.util.Scanner;
class question_1{
void main(){
Scanner sc = new Scanner(System.in);
String n = "" , s , cap = "";
char c;
int i, l;
System.out.println("Enter a string ");
s = sc.nextLine();
l = s.length();

for(i = 0 ; i < l ; i++){


c = s.charAt(i);

if(c >= 'A' && c <= 'Z')


cap += c;
else
n += c;
}

n += cap;
System.out.println(n);
}
}

Q2.

import java.util.Scanner;
class question_2{
void main(){
Scanner sc = new Scanner(System.in);
String s , w = "";
char c,p;
boolean palindrome = true;
int i , l;
System.out.println("Enter a sentence");
s = sc.nextLine();
s = " " + s;
l = s.length();
// to make word from the 1st character.
for(i = 1 ; i < l ; i++){
c = s.charAt(i);
p = s.charAt(i-1);
if(Character.isLetter(c) && p == ' ')
w += c;
}

System.out.println(w);
// to check wether the word is palindrome or not
l = w.length();
for(i = 0 ; i < l ; i++){
c = w.charAt(i);
p = w.charAt(l-i-1);
if(p != c){
palindrome = false;
break;
}
}

if(palindrome)
System.out.println("The word formed is palindrome");
else
System.out.println("The word formed is not palindrome");
}
}

Q3.

import java.util.Scanner;
class question_3{
void main(){
Scanner sc = new Scanner(System.in);
String s, n = "", r = " ";
char c;
int i, l;
System.out.println("Enter your string ");
s = sc.nextLine();
l = s.length();

// to remove special character


for(i = 0 ; i < l ; i++){
c = s.charAt(i);
if(Character.isLetter(c) || c == ' ')
n += c;
else
n += ' ';
}

// to reverse the sentence


n += " ";
l = n.length();
s = "";
for(i = 0 ; i < l ; i++){
c = n.charAt(i);
if(c != ' '){
r += c;
}
else{
s = r + s;
r = " ";
}
}
System.out.println(s);
}
}
Q4.

import java.util.Scanner;
class question_4{
String digit(int d){
if(d == 1)
return "One";
if(d == 2)
return "Two";
if(d == 3)
return "Three";
if(d == 4)
return "Four";
if(d == 5)
return "Five";
if(d == 6)
return "Six";
if(d == 7)
return "Seven";
if(d == 8)
return "Eight";
if(d == 9)
return "Nine";
else
return "Zero";
};
void main(){
Scanner sc = new Scanner(System.in);
int n,r;
String s = "";
System.out.println("Enter a number");
n = sc.nextInt();

while(n!=0){
r = n%10;
n /= 10;
s = digit(r) + " " + s;
}

System.out.println(s);
}
}

Q5.

import java.util.Scanner;
class question_5{
void main(){
Scanner sc = new Scanner(System.in);
int i,j, a;
int num[] = new int[11];
for(i = 0 ; i < 10 ; i++){
System.out.println("Enter a number");
num[i] = sc.nextInt();
}

// to arrange the numbers


for(j = 1 ; j < 10 ; j++){
for(i = 0 ; i < 10 - j ; i++){
if(num[i] > num[i+1]){
int temp = num[i+1];
num[i+1] = num[i];
num[i] = temp;
}
}
}

System.out.println("enter another number");


a = sc.nextInt();
for(i = 0 ; i < 10 ; i++){
if(a < num[i]){
for(j = 10 ; j > i ; j--){
num[j] = num[j-1];
}
num[i] = a;
break;
}
}

for(i = 0 ; i < 11 ; i++)


System.out.println(num[i]);
}
}

Q7.

import java.util.Scanner;
class question_7{
void main(){
Scanner sc = new Scanner(System.in);
int n;
String b = "";
System.out.println("enter a number");
n = sc.nextInt();

while( n!= 0){


int r = n%2;
n = n/2;
b = r + b;
}

System.out.println(b);
}
}

Q8.

import java.util.Scanner;
class question_8{
void main(){
Scanner sc = new Scanner(System.in);
int i = 0,r,n,ans = 0;
n = sc.nextInt();
while(n != 0){
r = n%10;
n = n/10;
if(r==1)
ans += Math.pow(2,i);

++i;
}

System.out.println(ans);
}
}

Q9.

import java.util.Scanner;
class perfect{
int n;

perfect(){

perfect(int val){
n = val;
}

void perfect_sq(){
double p;
int i,sq = n;

for(i = 1 ; i <= 5 ; i++){


sq = sq+1;
p = Math.sqrt(sq);
while(p != Math.floor(p)){
sq++;
p = Math.sqrt(sq);
}

System.out.println(sq);
}
}

void sum_of(){
int i ,j , k ,s = 0;
for(i = 1 ; i < n ; i++){
k = 0;
while( s < n){
s += i + k;
k++;
}

if(s == n){
for(j = 0; j < k ; j++){
System.out.print(i + j + " ");
}
System.out.println();
}

s = 0;
}
}

void main(){
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
int a;
a = sc.nextInt();
perfect p = new perfect(a);
p.perfect_sq();
p.sum_of();

}
}

Q10.

import java.util.Scanner;
class piglatin
{
String txt;
int len;
piglatin()
{
txt = "";
len = 0;
}

void readstring(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word in upper case");
txt = sc.nextLine();
len = txt.length();
txt = txt.toUpperCase();
}

void convert(){
String s = "";
char c;
for(int i = 0; i < len ; i++){
c = txt.charAt(i);
if(c == 'A' || c == 'E'|| c == 'I'|| c == 'O' || c == 'U'){
s = txt.substring(i) + s + "AY";
System.out.println(s);
break;
}

s += c;

}
}

void consonant(){
char c;
int i , count = 0;
for(i = 0 ; i < len ; i++){
c = txt.charAt(i);
if(c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U')
count++;

System.out.println("Number of consonant : " + count);


}
}

void main(){
piglatin a = new piglatin();
a.readstring();
a.convert();
a.consonant();
}
}

Q11.

import java.util.Scanner;
class question_11{
void main(){
Scanner sc = new Scanner(System.in);
int n,r,i,m,p = 0;

for(i = 1 ; i <= 10 ; i++){


System.out.println("Enter a Number");
n = sc.nextInt();
m = n;
//to check for palindrome
while(n != 0){
r = n%10;
n /= 10;
p = p*10 + r;
}

if(m == p)
System.out.println(m);

p = 0;
}
}
}

Q12.

import java.util.Scanner;
public class question_12
{
void main(){
Scanner sc = new Scanner(System.in);
int a,b,i,ans = 0;
System.out.println("Enter your first number");
a = sc.nextInt();
System.out.println("Enter your second number");
b = sc.nextInt();

for(i = 1; i <= a; i++){


if(a%i == 0 && b%i == 0)
ans = i;
}

System.out.println("HCF of "+ a + " and " + b + " is : " + ans);


}
}

Q13.

import java.util.Scanner;
public class question_13 extends question_12
{
void main(){
Scanner sc = new Scanner(System.in);
String date;
int day,month,year;
int i,j;
System.out.println("Enter a date as 'dd/mm/yyyy' ");
date = sc.nextLine();

i = date.indexOf('/');
j = date.lastIndexOf('/');

if(i != j && date.length() == 10){


day = Integer.parseInt(date.substring(0, i));
month = Integer.parseInt(date.substring(i+1,j));
year = Integer.parseInt(date.substring(j+1));

if(day >= 1 && day < 31 && month >= 1 && month <= 12
&& year <= 2023){
System.out.println(month + "/" + day + "/" + year);
}
else
System.out.println("INVALID INPUT");

}
else{
System.out.println("INVALID INPUT");
}
}
}

Q14.

import java.util.Scanner;
public class question_14
{
void main(){
Scanner sc = new Scanner(System.in);
int num[] = {1,4,7,2,3,6,4,6};
int n,i,l;
l = num.length;
System.out.println("select the number you want to delete");
for(i = 0 ; i < l ; i++)
System.out.print(num[i] + " ");

n = sc.nextInt();
for(i = 0 ; i < l; i++){
if(n == num[i])
num[i] = 0;
}

}
}

Q15.

import java.util.Scanner;
public class question_15
{
void main(){
Scanner sc = new Scanner(System.in);
String s;
char c;
int i,l,a;
System.out.println("enter a string ");
s = sc.nextLine();
l = s.length();

for(i = 0 ; i < l ; i++){


c = s.charAt(i);
a = c; // to convert char into ASCII code
System.out.println(c + " " + a);
}

}
}

Q16.

import java.util.Scanner;
public class Increment
{
String name;
double basic;
int age;

void getdata(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name , age and basic ");

name = sc.nextLine();
age = sc.nextInt();
basic = sc.nextInt();
};

void calc(){
if(age <= 45)
basic = basic + basic*.1;
if(age > 45 && age < 56)
basic = basic + basic*.15;
if(age >= 56)
basic = basic + basic*.2;
}

void display(){
System.out.println("Age \t basic");
System.out.println(age + "\t" + basic);
}

void main(){
Increment employ = new Increment();
employ.getdata();
employ.calc();
employ.display();
}
}

Q18.

public class question_18


{
void main(int [] A , int [] B){
int i,j,k,len1,len2;
len1 = A.length;
len2 = B.length;
int [] C = new int[len1+len2];
i = 0;
k = 0;
j = len2 -1;

while(i < len1 && j >= 0){


if(A[i] < B[j]){
C[k] = A[i];
i++;
}
else if(B[j] < A[i]){
C[k] = B[j];
j--;
}

k++;
}

while(i < len1){


C[k] = A[i];
i++;
k++;
}

while(j >=0){
C[k] = B[j];
j--;
k++;
}
}
}

Q19.

import java.util.Scanner;
public class question_19
{
void main(){
Scanner sc = new Scanner(System.in);
String s , n = "";
boolean p = true;
int i , j , l;
char c, f;
System.out.println(" Enter a string ");
s = sc.nextLine();
l = s.length();

for(i = 0 ; i < l ; i++){


c = s.charAt(i);
for(j = 0 ; j < n.length() ; j++){
f = n.charAt(j);
if(c == f){
p = false;
break;
}
}

if(p)
n += c;
else
p = true;

System.out.println(n);
}
}

Q20.

import java.util.Scanner;
public class question_20
{
void main(){
Scanner sc = new Scanner(System.in);
long n,d = 0,r,s = 0 , i = 1;
System.out.println("Enter your IMEI number ");
n = sc.nextLong();

while(n != 0){
r = n%10;
n = n/10;

if(i%2 == 0){
r = r*2;
}

if(r>9)
r = r%10 + r/10;

s += r;
i++;
d++;
}

if(s%10 == 0 && d == 15)


System.out.println("VALID INPUT");
else
System.out.println("INVALID INPUT");

}
}

You might also like