0% found this document useful (0 votes)
82 views

Assignment No 1 - Fundamentals of Programming

The document contains questions and answers related to Java programming concepts. There are 8 questions covering topics like data types in Java, output of a sample program, checking if a rectangle is a square, swapping two numbers without a third variable, checking if a character is uppercase or lowercase, checking if a number is strong, printing prime numbers from 1 to 100, and printing different patterns.

Uploaded by

Darade Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Assignment No 1 - Fundamentals of Programming

The document contains questions and answers related to Java programming concepts. There are 8 questions covering topics like data types in Java, output of a sample program, checking if a rectangle is a square, swapping two numbers without a third variable, checking if a character is uppercase or lowercase, checking if a number is strong, printing prime numbers from 1 to 100, and printing different patterns.

Uploaded by

Darade Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Que:1 What is Datatype and how many type of datatypes in java explain in brief?

---> A data type, in programming, is a classification that specifies which type of value a variable has and
what type of mathematical, relational or logical operations can be applied to it without causing an error.

Datatype : Two Types Of Datatype


1)Primitive data types

2)Non-primitive data types

Primitive Data Types:-


A primitive data type specifies the size and type of variable values, and it has no additional methods.

Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127


short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

# Primitive number types are divided into two groups:

1) Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals.
Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.

2) Floating point types represents numbers with a fractional part, containing one or more decimals.
There are two types: float and double.

#Integer Types
1)Byte:-
The byte data type can store whole numbers from -128 to 127.This can be used instead of int or other integer
types to save memory when you are certain that the value will be within -128 and 127.

2)Short:-
The short data type can store whole numbers from -32768 to 32767.

3)Int:-
The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial,
the int data type is the preferred data type when we create variables with a numeric value.

4)Long:-
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807.
This is used when int is not large enough to store the value. Note that you should end the value with an "L".
5)Float:-
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value wi
th an "f".

6)Double:-
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value
with a "d"

7)Booleans:-
A boolean data type is declared with the boolean keyword and can only take the values true or false.

8)Characters:-
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' o
r 'c'.

9)Strings:-
The String data type is used to store a sequence of characters (text). String values must be surrounded by double q
uotes.

Non-Primitive Data Types:-


Non-primitive data types are called reference types because they refer to objects.

# The main difference between primitive and non-primitive data types are:

a) Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and
is not defined by Java (except for String).

b) Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.

c) A primitive type has always a value, while non-primitive types can be null.

d) A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.

e) The size of a primitive type depends on the data type, while non-primitive types have all the same size.

Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.

*********************************************************************************************
****************************************************************************************

Que:2 What will be the output of the following program?

public class JavaDemo1{


public static void main(String[] args)
{
int a=11, b=22, c
c = a + b + a++ + b++ + ++a + ++b;
System.out.println(“a:- ”+a);
System.out.println(“b:- ”+b);
System.out.println(“c:- ”+c);
}
}

Output:-
a:- 13
b:- 24
c:- 103

*********************************************************************************************
****************************************************************************************

Que:3 Write a Program to take a values of length and breadth of a rectangle from user and check if it is square or no
t?

mport java.util.*;

class CheckIf

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter the length: ");

double l=sc.nextDouble();

System.out.println("Enter the breadth: ");

double b=sc.nextDouble();

if(l==b)

System.out.println("Square...");

else

System.out.println("Not a Square...");

*********************************************************************************************
****************************************************************************************

Que:4 Write a Program to swap to numbers without using 3rd variable ?

import java.util.*;
class Swap
{
public static void main(String a[])
{
System.out.println("Enter the value of x and y");
Scanner sc = new Scanner(System.in);

int x = sc.nextInt();
int y = sc.nextInt();
System.out.println("before swapping numbers: "+x +" "+ y);

x = x + y;
y = x - y;
x = x - y;
System.out.println("After swapping: "+x +" " + y);
}
}

*********************************************************************************************
****************************************************************************************

Que:5 Write a program to check whether a entered character is lowercase (a to z ) or uppercase ( A to Z ).

import java.util.Scanner;
public class CheckUpperLower
{
public static void main(String args[])
{
char ch;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the character ");

ch=scan.next().charAt(0);

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


{
System.out.println(ch+" is an upper case letter ");
}
else if(ch>='a' && ch<='z')
{
System.out.println(ch+" is a lower case letter ");
}
else
{
System.out.println(ch+" is not a Alphabets ");
}
}
}

*********************************************************************************************
****************************************************************************************

Que:6 Write a Program to accept a number from user check if it is Strong or not?

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n,i;
int fact,rem;

Scanner sc = new Scanner(System.in);


System.out.print("\nEnter the number : ");
n = sc.nextInt();
int sum = 0;
int temp = n;

while(n != 0)
{
i = 1;
fact = 1;
rem = n % 10;

while(i <= rem)


{
fact = fact * i;
i++;
}
sum = sum + fact;
n = n / 10;
}

if(sum == temp)

System.out.println(temp + " is a strong number\n");

else

System.out.println(temp + " is not a strong number\n");

System.out.println();
}
}

*********************************************************************************************
****************************************************************************************

Que:7

class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;

String primeNumbers = "";

for (i = 1; i <= 100; i++)


{
int counter=0;

for(num =i; num>=1; num--)


{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);
}
}

Que:8 Write a program to print following patterns ?

1)
*
***
*****
*******
*********

import java.util.Scanner;

public class Pattern1


{
public static void main(String[] args)
{

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows needed to print the pattern ");

int rows = scanner.nextInt();


System.out.println(" Printing the pattern ");

for (int i=1; i<=rows; i++)


{
// Print space in decreasing order
for (int j=rows; j>i; j--)
{
System.out.print(" ");
}
// Print star in increasing order
for (int k=1; k<=(i * 2) -1; k++)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}

2)
*********
*******
*****
***
*

import java.util.Scanner;

public class Pattern2


{
public static void main(String[] args)
{

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows needed to print the pattern ");

int rows = scanner.nextInt();

System.out.println(" Printing the pattern ");

for (int i=rows; i>=1; i--)


{
// Print star in decreasing order
for (int k=1; k<=(i * 2) -1; k++)
{
System.out.print("*");
}
System.out.println();
// Print space in increasing order
for (int j=rows; j>=i; j--)
{
System.out.print(" ");
}

}
scanner.close();
}
}

3)

1
21
321
4321

import java.util.Scanner;

public class Pattern3


{
public static void main(String[] args)
{

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows needed to print the pattern ");

int rows = scanner.nextInt();

System.out.println(" Printing the pattern ");

for (int k=1; k<=rows; k++)


{
for (int j=k; j>=1; j--)
{
System.out.print(j+" ");
}

System.out.println();
}

scanner.close();
}
}

4}

1
01
101
0101

import java.util.Scanner;

public class Pattern4


{

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();

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


for (int j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
}
}

5)

A
BG
CHM
DINS
EJOTY
FKPUZ_

public class Pattern5


{
public static void main(String[] args)
{

System.out.println(" Printing the pattern ");

for (int i = 0; i <= 5; i++)


{
int alphabet = 65;
int temp = i;
for (int j = i; j >= 0; j--)
{
System.out.print((char) (alphabet + temp) + " ");
temp = temp + 5;
}
System.out.println();
}
}
}

6)
F
FE
FED
FEDC
FEDCB
FEDCBA

public class Pattern6


{
public static void main(String[] args)
{
System.out.println("** Printing the pattern... **");

for (int i = 5; i >= 0; i--)


{
int alphabet = 65;
for (int j = 5; j >= i; j--)
{
System.out.print((char) (alphabet + j) + " ");
}
System.out.println();
}
}
}

*********************************************************************************************
****************************************************************************************

Que:9 Can you pass the negative number as an array size? and Why?
---->
No, array size cannot be negative. If we specify array size as negative, there will be no compile time error.
But there will be run time NegativeArraySizeException.

*********************************************************************************************
****************************************************************************************

Que:10 Can you change the size of the array once you define it? OR Can you insert or delete the elements after crea
ting an array? and why?
---->
No. You can’t change the size of the array once you define it. You can not insert or delete the elements
after creating an array.Only you can do is change the value of the elements.

*********************************************************************************************
****************************************************************************************

Que:11 Write a program to sort element of arrays in Descending order?

import java.util.Scanner;
public class Descending_Order
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}

*********************************************************************************************
****************************************************************************************

Que:12 Write a program to display sum and average of array elements ?

--->

import java.util.Scanner;
public class Sum_Average
{
public static void main(String[] args)
{
int n, sum = 0;
float average;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n ; i++)
{
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum:"+sum);
average = (float)sum / n;
System.out.println("Average:"+average);
}
}
*********************************************************************************************
****************************************************************************************

You might also like