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

Control Structure

a. Write a program to calculate Simple interest

import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float P=sc.nextFloat();
float R=sc.nextFloat();
float T=sc.nextFloat();
float SI = (P * T * R) / 100;
System.out.println("Simple interest = " + SI);
}
}
b. Write a program to convert decimal to binary

import java.util.*;
class HelloWorld {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a decimal number : ");
int n = sc.nextInt();

String bin = "";


int temp = n;
while(temp>0)
{
int r = temp%2;
temp = temp/2;
bin=r+bin;
}
System.out.println("Binary Equivalent of " +n+ " is " +bin);
}

}
c. Program to print series using function: x+x^3/3!+x^5/5!+…+x^n/n!

import java.util.*;

class HelloWorld {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int x=sc.nextInt();

int n=sc.nextInt();

int i,j;

double fact,s=0.0,b=0.0;

long a;

for(i=1;i<=n;i=i+2){

a=(long)(Math.pow(x,i));

fact=1.0;

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

fact=fact*j;

b=(a/fact);

s=s+b;

System.out.println(s);

}
d. Write a program to following pattern

23

456

7 8 9 10

import java.util.*;
class Main {
public static void main(String[] args)
{
int n=1;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(n+" ");
n++;
}
System.out.println();
}
}}
2.Array and String

a.) Given an number. The task is to tell whether the number is valid indian mobile
number or not. Print "Valid" if it is a valid indian mobile number, otherwise print
"Invalid".
import java.util.*;
class HelloWorld {
public static boolean checknum(String s){
int l=s.length();

if(l>13){
return false;
}
else{
if(l<10){
return false;
}
else
{
if(l==10 && (s.charAt(0)=='7' || s.charAt(0)=='8' || s.charAt(0)=='9'))
{
return true;
}
else if(l==11 && s.charAt(0)=='0' && (s.charAt(1)=='7' || s.charAt(1)=='8' ||
s.charAt(1)=='9'))
{
return true;
}
else if(l==12 && s.substring(0, 2).equals("91") && (s.charAt(2)=='7' || s.charAt(2)=='8' ||
s.charAt(2)=='9'))
{
return true;
}
else if(l==13 && s.substring(0, 3).equals("+91") && (s.charAt(3)=='7' || s.charAt(3)=='8' ||
s.charAt(3)=='9'))
{
return true;
}
else{
return false;
}
}
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
Boolean[] arr = new Boolean[n];
int c=0;

for(int i=0;i<n;i++)
{
String s=sc.next();
arr[i]=checknum(s);
}

for(int i=0;i<n;i++){
System.out.println((arr[i]==true) ? "valid" : "Invalid");
}
}

}
b. Virus Outbreak

import java.io.*;
import java.util.*;
public class HelloWorld {
public static Boolean checkvirus(String s1,String s2){
int m = s1.length();
int n = s2.length();

int i = 0;
int j = 0;

while (j < m && i < n){


if (s1.charAt(j) == s2.charAt(i)){
j++;
}
i++;
}
if(j==m)
{
return true;
}
else{
return false;
}
}
public static void main(String args[] ) throws Exception {

Scanner sc = new Scanner(System.in);


String virusname = sc.next();
int n = sc.nextInt();
Boolean[] arr = new Boolean[n];

for(int i=0;i<n;i++)
{
String b = sc.next();
arr[i]= checkvirus(b,virusname);
}

for(int i=0;i<n;i++){
System.out.println((arr[i]==true) ? "POSITIVE" : "NEGATIVE");
}

You might also like