0% found this document useful (0 votes)
15 views7 pages

Experiment 1

Uploaded by

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

Experiment 1

Uploaded by

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

Name Anuharsh Pawar

UID no. 2022300004

Experiment No. 1

AIM: Encapsulation
Program 1

PROBLEM Write a program using classes and objects to print all even and
STATEMENT :
of even and odd numbers in the given range.
ALGORITHM: 1)Create a class evenodd
2)In class, read n1 and n2
3)Create void function to print and count even numbers
4) Create void function to print and count odd numbers
5)Create public class ps1
6)In that class, create main function
7)Call even and odd function in main function
PROGRAM: import java.util.Scanner;

class evenodd
{
int n1, n2;

void even(int n1, int n2)


{
int count = 0;

for (int i = n1; i <= n2; i++)


{
if (i%2 == 0)
{
System.out.print(i+" ");
count++;
}
}
System.out.println();
System.out.println("total count of even no.:" + count);

void odd(int n1 ,int n2)


{
int count=0;
for (int i = n1; i <= n2; i++)
{
if (i%2!= 0)
{
System.out.print(i+" ");
count++;
}
}
System.out.println();
System.out.println("total count of odd no.:" + count);
}
}

public class ps1


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
evenodd A1 = new evenodd();
System.out.println("enter range");
A1.n1 = sc.nextInt();
A1.n2 = sc.nextInt();

System.out.println("even no. are");


A1.even(A1.n1, A1.n2);

System.out.println("odd no. are");


A1.odd(A1.n1 , A1.n2);

}
}

RESULT:

Program 2

PROBLEM Write a program to print all Armstrong numbers and Prim


STATEMENT :
the total count of Prime Numbers in the given range.
ALGORITHM: i)Create class print
ii)In class, read n1 and 2
iii)Create void function to print armstrong numbers
iv)Creat void function to print prime numbers
iv)Create public class ps2
v)In that class, create main function
vi)Call armstrong and prime function in main function
PROGRAM: import java.util.Scanner;

class print
{
int n1, n2;

void armstrong(int n1, int n2)


{
int count = 0;

for (int i = n1; i <= n2; i++)


{
int n = i;
int arm = 0;

while (n > 0)
{
int a= n % 10;
arm += a * a * a;
n=n/10;
}

if(arm==i)
{
System.out.print(i+" ");
count++;
}

}
System.out.println();
System.out.println("total count of armstrong no.:" + co
}

void prime(int n1 ,int n2)


{
int c=0;
for(int i=n1;i<=n2;i++)
{
int count=0;

for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}

}
if(count==2)
{
System.out.print(i+" ");
c++;
}

}
System.out.println();
System.out.println("total count of prime no. "+c);

}
}

public class ps2


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
print A1 = new print();

System.out.println("enter range");
A1.n1 = sc.nextInt();
A1.n2 = sc.nextInt();

System.out.println("armstrong no.s are");


A1.armstrong(A1.n1, A1.n2);

System.out.println();
System.out.println("prime no.s are");
A1.prime(A1.n1,A1.n2);

}
}

RESULT:
CONCLUSION: I was able to learn object-oriented programming using java as well as how
to create the logic behind these two experiments.

You might also like