You are on page 1of 15

Name: Aashutosh

Jaiswal.
Class: XI
A(Science).
Roll no.:19.
Subject: Computer
Science.
Year:2015-2016.

INDEX
Programme 1.
Verify whether at least one of the array element is even
or not.
Programme 2.
Verify whether all the elements of an array are even or
not.
Programme 3.
Verify whether all the other element are larger than the
first element in an array or not.
Programme 4.
Verify whether a number n, has at least one factor
between 2 and its half n/2
Programme 5.
Verify whether every word in a string contains one
occurrence of q or not
Programme 6.
Verify whether every word in a string contains at least
one occurrence of q

Programme 7.
WAP to print five types of pattern by putting the value
of n.
Programme 8.
WAP to Print the Calendar.

Solution:
1)Verify whether at least one of the array element is
even or not.
import java.util.Scanner;
public class EvenNumberCheck
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no. of rows");
int row = sc.nextInt();
System.out.println("Enter the no. of columns");
int col = sc.nextInt();
System.out.println("Please enter the " + (row*col) +
"element/s followed by enter");
int array[][] = new int[row][col];
for(int i = 0; i<row; i++)
{
for(int j = 0; j<col; j++)
{
array[i][j] = sc.nextInt();
}
}
boolean result = false;
for(int i = 0; i<row; i++)
{

for(int j = 0; j<col; j++)


{
if(array[i][j] % 2 == 0)
{
result = true;
break;
}
else
{
//Redundant so commented out
//result = false;
}
}
}
if(result)
{
System.out.println("One of the Array element is
even.");
}
else
{
System.out.println("None of the Array element is
even.");
}
}
}
OUTPUT:
Enter the no. of rows
3
Enter the no. of columns
3
Please enter the 9 element/s followed by enter
1
2
3
4
5
6
7

8
9
One of the Array element is even.

2) Verify whether all the elements of an array are even

or not.
import java.util.Scanner;
public class EvenNumberCheck
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no. of rows");
int row = sc.nextInt();
System.out.println("Enter the no. of columns");
int col = sc.nextInt();
System.out.println("Please enter the " + (row*col) + "
element/s followed by enter");
int array[][] = new int[row][col];
for(int i = 0; i<row; i++)
{
for(int j = 0; j<col; j++)
{
array[i][j] = sc.nextInt();
}
}
boolean result = false;
for(int i = 0; i<row; i++)
{
for(int j = 0; j<col; j++)
{
if(array[i][j] % 2 == 0)
{

result = true;
}
else
{
result = false;
break;
}
}
}
if(result)
{
System.out.println("All the elements of the array
are even.");
}
else
{
System.out.println("One of the element of the array
is not even.");
}
}
}

OUTPUT:
Enter the no. of rows
3
Enter the no. of columns
3
Please enter the 9 element/s followed by enter
2
2
2
2
2
2
2
2
2
All the elements of the Array are Even.

3) Verify whether all the other element are larger than

the first element in an array or not.

import java.util.Scanner;
public class Arrays1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array");
int length = sc.nextInt();
int arr[] = new int[length];
System.out.println("Please enter " + length + " elements
followed by enter");
for(int i = 0; i<length; i++)
{
arr[i] = sc.nextInt();
}
boolean result = false;
for(int i = 1; i<length; i++)
{
if(arr[i] >= arr[0])
{
result = true;
continue;
}
else
{
result = false;
break;
}
}
if(result)
{

System.out.println("All the elements except the


first element is greater than or equal to the first element");
}
else
{
System.out.println("Some of the elements except the
first element is not greater than or equal to the first
element");
}
}
}
OUTPUT:
Enter the size of the array
10
Please enter 10 elements followed by enter
1
2
3
4
5
6
7
8
9
10
All the elements except the first element is greater than
or equal to the first element

4) Verify whether a number n, has at least one factor

between 2 and its half n/2

import java.util.Scanner;
public class Factors
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int num = sc.nextInt();
boolean result = false;
for(int i = 3; i<num/2; i++)
{
if(num % i == 0)
{
result = true;
break;
}
else
{
result = false;
//Continue
}
}
if(result)
{
System.out.println(num + " has atleast one factor
between 2 and " + (num/2));
}
else
{
System.out.println(num + " does not have any factor
between 2 and " + (num/2));
}}
OUTPUT:
Enter a number:

78
78 has atleast one factor between 2 and 39
5) Verify whether every word in a string contains one

occurrence of q or not
import java.util.Scanner;
public class Strings1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String s = sc.nextLine();
int length = s.length();
boolean result = false;
for(int i = 0; i<length; i++)
{
if(s.charAt(i) == 'q')
{
result = true;
break;
}
else
{
//Continue;
}
}
if(result)
{
System.out.println("This string \"" + s + "\" has
atleast one occurence of 'q'.");
}
else
{
System.out.println("This string \"" + s + "\" has no
occurence of 'q'.");
}
}

}
OUTPUT:
Enter a string
queen
This string "queen" has atleast one occurence of 'q'.
6) Verify whether every word in a string contains at

least one occurrence of q


import java.util.Scanner;
public class String2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String s = sc.nextLine();
s = " " + s;
int length = s.length();
int lastIndex = s.lastIndexOf(" ");
boolean result = false;
for(int i = 0; i<length; i++)
{
if(s.charAt(i) == ' ')
{
if(s.charAt(i+1) == 's')
{
result = true;
}
else
{
result = false;
break;
}
}
else
{
//Continue
}

if(i == lastIndex)
{
break;
}
else
{
//Continue
}
}
if(result)
{
System.out.println("Every word in the string \"" +
s.trim() + "\" begins with 's'.");
}
else
{
System.out.println("Every word in the string \"" +
s.trim() + "\" does not begin with 's'.");
}
}
}
OUTPUT:
Enter a string:
Sharukh Rocks
Every word in the string "Sharukh Rocks" does not begin
with 's'

7) WAP to print five types of pattern by putting the

value of n.
import java.util.Scanner;
public class Patterns1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of n:");


int n = sc.nextInt();
String s = ""; //Not a space
for(int i = 1; i<=n; i++)
{
s += i;
}
for(int i = n-1; i>=0; i--)
{
System.out.println(s);
s = s.substring(0, i);
}
System.out.println("");
for(int i = n; i>=1; i--)
{
s += i;
}
for(int i = n-1; i>=0; i--)
{
System.out.println(s);
s = s.substring(0, i);
}
System.out.println("");
for(int i = n; i>=1; i--)
{
s += i;
System.out.println(s);
}
s = "";
System.out.println("");
for(int i = 1; i<=n; i++)
{
s += i;
}
for(int i = 0; i<=n; i++)
{
System.out.println(s.substring(i, n));
}
s = "";
for(int i = 1; i<=n; i++)

{
for(int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println("");
}
}
}

OUTPUT:
Enter the value of n:5
12345
1234
123
12
1
54321
5432
543
54
5
5
54
543
5432
54321
12345
2345
345
45
5
1
22

333
4444
55555

8) WAP to Print the Calendar.

You might also like