Assignment No 1 - Fundamentals of Programming
Assignment No 1 - Fundamentals of Programming
---> 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.
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.
# 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.
*********************************************************************************************
****************************************************************************************
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
double l=sc.nextDouble();
double b=sc.nextDouble();
if(l==b)
System.out.println("Square...");
else
System.out.println("Not a Square...");
*********************************************************************************************
****************************************************************************************
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);
}
}
*********************************************************************************************
****************************************************************************************
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);
*********************************************************************************************
****************************************************************************************
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;
while(n != 0)
{
i = 1;
fact = 1;
rem = n % 10;
if(sum == temp)
else
System.out.println();
}
}
*********************************************************************************************
****************************************************************************************
Que:7
class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
1)
*
***
*****
*******
*********
import java.util.Scanner;
2)
*********
*******
*****
***
*
import java.util.Scanner;
}
scanner.close();
}
}
3)
1
21
321
4321
import java.util.Scanner;
System.out.println();
}
scanner.close();
}
}
4}
1
01
101
0101
import java.util.Scanner;
5)
A
BG
CHM
DINS
EJOTY
FKPUZ_
6)
F
FE
FED
FEDC
FEDCB
FEDCBA
*********************************************************************************************
****************************************************************************************
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.
*********************************************************************************************
****************************************************************************************
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]);
}
}
*********************************************************************************************
****************************************************************************************
--->
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);
}
}
*********************************************************************************************
****************************************************************************************