[go: up one dir, main page]

0% found this document useful (0 votes)
10 views15 pages

PracticalAssignmentPrograms PDF

The document outlines a series of practical programming assignments for Euro School, HSR, focusing on various algorithms and number properties. Each task includes a specific problem statement, such as checking for prime numbers, Armstrong numbers, and generating Fibonacci sequences, along with sample Java code implementations. The assignments aim to enhance programming skills through hands-on practice with fundamental concepts in computer science.

Uploaded by

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

PracticalAssignmentPrograms PDF

The document outlines a series of practical programming assignments for Euro School, HSR, focusing on various algorithms and number properties. Each task includes a specific problem statement, such as checking for prime numbers, Armstrong numbers, and generating Fibonacci sequences, along with sample Java code implementations. The assignments aim to enhance programming skills through hands-on practice with fundamental concepts in computer science.

Uploaded by

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

Euro School, HSR

Practical Assignment Programs

1 WAP to Accept a 3 numbers and arrange in descending order.


2 WAP to display all the prime numbers from 1 to 100.
3 WAP to display 10 Fibonacci numbers
(A series of numbers in which each number is the sum of the two preceding numbers. The
simplest is the series 0, 1, 1, 2, 3, 5, 8, etc.
4 WAP to accept a number and count the no of even digits and odd digits present in it.
Eg: 156 = No of even digits=1 and No of odd digits=2
5 WAP to accept a number and check if it is a Armstrong number or not.
(Armstrong Number is a positive number if it is equal to the sum of cubes of its digits is called
Armstrong number. Eg:- 153 = 13 +53 +33 )
6 WAP to accept a number and check if it is an Automorphic number or not.
(An Automorphic number is a number whose square “ends” in the same digits as the number
itself. Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625)
7 WAP to accept a number and check if it is a Neon number or not.
(A neon number is a number where the sum of digits of square of the number is equal to the
number. Eg: 9, its square is 9*9 = 81 and sum of the digits is 9. i.e. 9 is a neon number.)
8 WAP to accept a number and check if it is a Palindrome number or not.
(A palindromic number is a number that remains the same when its digits are reversed.
Eg:- 16461, 121, 252)
9 WAP to accept a number and check if it is a Perfect number or not.
(A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding
the number itself. Eg: - 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number.
10 WAP to accept a number and check if it is a Special number (Krishnamurthy) or not.
(A number is said to be special number when the sum of factorial of its digits is equal to the
number itself. Example- 145 is a Special Number as 1!+4!+5!=145.)
11 WAP to accept a number and check if it is a SPY number or not.
(A spy number is a number where the sum of its digits equals the product of its digits.
Eg: 1124 is a spy number, the sum of its digits is 1+1+2+4=8 and the product of its digits is
1*1*2*4=8.)
12 WAP to display all the Buzz numbers from M to N.
13 WAP to accept two numbers and find the LCM and GCD/HCF of it.
14 WAP to accept a number and check if it is a Pronic number or not.
(A number is said to be a pronic number if product of two consecutive integers is equal to the
number. Example- 42 is said to be a pronic number, 42=6×7, here 6 and 7 are consecutive
integers)
15 Write a menu driven program to accept the choice from the user and display the given pattens
If choice =1 display pattern1, if choice =2 display pattern2 otherwise display “Invalid option”.
Pattern1 Pattern2
5
1
4 5
2 3
3 4 5
4 5 6
2 3 4 5
7 8 9 10
1 2 3 4 5
11 12 13 14 15
16 Write a menu driven program to accept the choice from the user and display the given series
If choice =1 display series1 , if choice =2 display series2 otherwise display “Invalid option”.
series1=(1+2)+ (1+2+3)+(1+2+3+4)+ --------- +(1+2+3+4+5----+n)
series2= x/5 +x/10+x/15+-------- n terms
17 WAP to compute the amount that a customer pays for the taxi that he hires based on the
following conditions:
Kms travelled Amount per Km
First 10 kms 25
Next 20 kms 10
Next 40 Kms 15
Above 70 kms 12
Answers

1 WAP to Accept a 3 numbers and arrange in descending order.


import java.util.*;
public class DescendingOrder
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter 3 numbers ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a>b & a>c)
{
if(b>c)
System.out.println(a+", "+b+", "+c);
else
System.out.println(a+", "+c+", "+b);
}
else if(b>c & b>a)
{ if(c>a)
System.out.println(b+", "+c+", "+a);
else
System.out.println(b+", "+a+", "+c);
}
else if(c>a & c>b)
{ if(a>b)
System.out.println(c+", "+a+", "+b);
else
System.out.println(c+", "+b+", "+a);
}
}
}
2 WAP to display all the prime numbers from 1 to 100.
import java.util.*;
public class Prime
{
public static void main(String args[])
{
for(int i=1;i<=100;i++)
{
int c=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
System.out.println(i);
}
}
}
3 WAP to display 10 Fibonacci numbers
(A series of numbers in which each number is the sum of the two preceding numbers. The
simplest is the series 0, 1, 1, 2, 3, 5, 8, etc.
public class FibonacciSeries
{
public static void main(String[] args)
{
int a = 0,b = 1, c=0;
System.out.print(a+", "+b+", ");
for (int i = 3; i <= 10; i++)
{
c = a + b;
System.out.print(c+", ");
a = b;
b = c;
}
}
}
4 WAP to accept a number and count the no of even digits and odd digits present in it.
Eg: 156 = No of even digits=1 and No of odd digits=2
import java.util.Scanner;
public class EvenOdd
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number= ");
int n = sc.nextInt();
int even=0, odd=0;
while(n>0)
{
int d=n%10;
if(d%2==0)
even++;
else
odd++;
n=n/10;
}
System.out.println("No of even digits = "+even);
System.out.println("No of odd digits = "+odd);
}
}
5 WAP to accept a number and check if it is a Armstrong number or not.
(Armstrong Number is a positive number if it is equal to the sum of cubes of its digits is called
Armstrong number. Eg:- 153 = 13 +53 +33 )
import java.util.*;
public class ArmstrongNumber
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int temp=n;
while(n>0)
{
int d=n%10;
sum=sum+(d*d*d);
n=n/10;
}
if(temp==sum)
System.out.println("Armstrong number");
else
System.out.println("Not a Armstrong Number");
}
}
6 WAP to accept a number and check if it is an Automorphic number or not.
(An Automorphic number is a number whose square “ends” in the same digits as the number
itself. Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625)
import java.util.*;
public class ArmstrongNumber
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int temp=n;
while(n>0)
{
int d=n%10;
sum=sum+(d*d*d);
n=n/10;
}
if(temp==sum)
System.out.println("Armstrong number");
else
System.out.println("Not a Armstrong Number");
}
}
7 WAP to accept a number and check if it is a Neon number or not.
(A neon number is a number where the sum of digits of square of the number is equal to the
number. Eg: 9, its square is 9*9 = 81 and sum of the digits is 9. i.e. 9 is a neon number.)

import java.util.Scanner;
public class NeonNumber
{
public static void main(String[] args)
{

int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int sqr = n * n;
while (sqr > 0)
{
int r = sqr % 10;
sum = sum + r;
sqr = sqr / 10;
}
if (n == sum)
{
System.out.println("Neon Number");
}
else
{
System.out.println("Not Neon Number");
}
}
}
8 WAP to accept a number and check if it is a Palindrome number or not.
(A palindromic number is a number that remains the same when its digits are reversed.
Eg:- 16461, 121, 252)
import java.util.Scanner;
public class PalindromeNumber
{
public static void main(String[] args)
{
int rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (n == rev)
{
System.out.println("Palindrome Number");
}
else
{
System.out.println("Not Palindrome Number");
}
}
}
9 WAP to accept a number and check if it is a Perfect number or not.
(A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding
the number itself. Eg: - 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number.

import java.util.*;
public class PerfectNumber
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
sum = sum + i;
}
}
if (n== sum)
{
System.out.println("Perfect Number");
}
else
{
System.out.println("Not Perfect Number");
}
}
}
10 WAP to accept a number and check if it is a Special number (Krishnamurthy) or not.
(A number is said to be special number when the sum of factorial of its digits is equal to the
number itself. Example- 145 is a Special Number as 1!+4!+5!=145.)

import java.util.Scanner;
public class SpecialNumber
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
int fact=1;
for(int i=1;i<=r;i++)
{
fact=fact*i;
}
sum = sum+fact;
num = num / 10;
}
if(n==sum)
{
System.out.println("Special Number" );
}
else
{
System.out.println("Not Special Number" );
}
}
}
11 WAP to accept a number and check if it is a SPY number or not.
(A spy number is a number where the sum of its digits equals the product of its digits.
Eg: 1124 is a spy number, the sum of its digits is 1+1+2+4=8 and the product of its digits is
1*1*2*4=8.)

import java.util.*;
public class SpyNumber
{
public static void main(String[] args)
{
int prod = 1, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
sum = sum + r;
prod=prod * r;
num = num / 10;
}
if (prod == sum)
{
System.out.println("Spy Number");
}
else
{
System.out.println("Not Spy Number");
}
}
}
12 WAP to display all the Buzz numbers from M to N.

import java.util.*;
public class SpyNumber
{
public static void main(String[] args)
{
int prod = 1, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
sum = sum + r;
prod=prod * r;
num = num / 10;
}
if (prod == sum)
{
System.out.println("Spy Number");
}
else
{
System.out.println("Not Spy Number");
}
}
}
13 WAP to accept two numbers and find the LCM and GCD/HCF of it.

import java.util.*;
public class LcmGcd
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter 2 numbers");
int a = sc.nextInt();
int b = sc.nextInt();
int lcm=0, hcf=0;
for(int i=1;i<=b; i++)
{
if(a%i==0 && b%i==0)
hcf=i;
}
lcm=(a*b)/hcf;
System.out.println("Lcm ="+lcm);
System.out.println("Hcf ="+hcf);
}
}
14 WAP to accept a number and check if it is a Pronic number or not.
(A number is said to be a pronic number if product of two consecutive integers is equal to the
number. Example- 42 is said to be a pronic number, 42=6×7, here 6 and 7 are consecutive
integers)

import java.util.Scanner;
public class PronicNumber
{
public static void main(String[] args)
{
int n;
boolean flag=false;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
for(int i=0; i < n; i++)
{
if(i*(i+1) == n)
{
flag =true;
break;
}
}
if(flag)
{
System.out.println("Pronic Number");
}
else
{
System.out.println("Not Pronic Number");
}
}
}
15 Write a menu driven program to accept the choice from the user and display the given pattens
If choice =1 display pattern1 , if choice =2 display pattern2 otherwise display “Invalid option”.
Pattern1 Pattern2
5
1
4 5
2 3
3 4 5
4 5 6
2 3 4 5
7 8 9 10
1 2 3 4 5
11 12 13 14 15

import java.util.*;
class Patterns
{
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
System.out.println(" 1 for Pattern1");
System.out.println(" 2 for Pattern2");
System.out.println(" Enter ur choice");
int choice=sc.nextInt();
switch(choice)
{
case 1:
int a=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(a);
a++;
}
System.out.println();
}
break;
case 2:
for(int i=5;i>=1;i--)
{
for(int j=i;j<=5;j++)
{
System.out.print(j);
}
System.out.println();
}
break;
default:
System.out.println("Invalid Option"); break;
}
}
}
16 Write a menu driven program to accept the choice from the user and display the given series
If choice =1 display series1 , if choice =2 display series2 otherwise display “Invalid option”.
series1=(1+2)+ (1+2+3)+(1+2+3+4)+ --------- +(1+2+3+4+5----+n)
series2= x/5 +x/10+x/15+-------- n terms

import java.util.*;
class Series
{
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
System.out.println(" 1 for Series1");
System.out.println(" 2 for Series2");
System.out.println(" Enter ur choice");
int choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the no of terms");
int n=sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++)
{ int s=0;
for(int j=1;j<=i+1;j++)
{
s=s+j;
}
sum=sum+s;
}
System.out.println(sum);
break;
case 2:
System.out.println("Enter the X value");
int x=sc.nextInt();
System.out.println("Enter the no of terms");
int n1=sc.nextInt();
double a=5; double sum1=0;
for(int i=1;i<=n1;i++)
{ a=a*i;
sum1=sum1+ x/a;
}
System.out.println(sum1);
break;
default:
System.out.println("Invalid Option"); break;
}
}
}
17 WAP to compute the amount that a customer pays for the taxi that he hires based on the
following conditions: Input the Customers name, the taxi number and the no of kilometers.

Kms travelled Amount per Km


First 10 kms 25
Next 20 kms 10
Next 40 Kms 15
Above 70 kms 12

import java.util.*;
class Taxi
{
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter the customers name");
String name=sc.next();
System.out.println(" Enter the taxi number ");
int num=sc.nextInt();
System.out.println("distance travelled");
int km=sc.nextInt();
double amt=0;
if(km>=1 & km<=10)
amt=km*25;
else if(km>10 & km<=30)
amt=25*10+(km-10)*10;
else if(km>30 & km<=70)
amt=25*10+ 20*10+ (km-30)* 15;
else if(km>70)
amt= 25*10+ 20*10+ 30*15 +(km-70)* 12;
System.out.println("Customers Name: "+name);
System.out.println("Taxi number: "+num);
System.out.println("Distance travelled: "+km);
System.out.println("Total amount to be paid: "+amt);
}
}

You might also like