2 . A special two-digit number is such that when the sum of its digits is added to the product of
its digits , the result is equal to original number
Example : 59
5+9 = 14
5*9 = 45
45+14 = 59 Therefore , 59 is a special two digit number
Write a program in java to check whether a number is special two-digit number or not
Ans-
class Prog2
{
static void test(int num)//user inputs a two-digit number
{
int m = num;
int product = 1;
int sum = 0;
while(m>0)
{
int dig = m%10;
sum = sum+dig;
product = product*dig;
m/=10;
}
int finalsum = sum+product;
if(finalsum==num)
System.out.println(num+” is a special two digit number “);
else
System.out.println(num+ ” is not a special two digit number “);
}
}
• 4.Write a program to display the following pattern :
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Ans-
• class Program4
{
static void teja()
{
• for(int i = 1,l=1;i<=5;i++,l+=2)
{
int k = 1;
• for(int j = 1,m=l;j<=5;j++,m+=2)
{
if(m>9)
{
System.out.print(k+” “);
k+=2;
}
else
if(l==5&&m==9)
{
System.out.print(“8 “);
}
else
System.out.print(m+” “);
}
System.out.println();
}
}
}
• 5.Write a Program in java to obtain the first eight
numbers of the following series :
1,11,111,1111………………
class Prog5
{
static void test()
{
double s=0.0;
int p;
for(int i = 0;i<=7;i++)
{
s=s+Math.pow(10,i);
p = (int)s;
System.out.print(p+” , “);
}
}
}
• Star Pyramid(Nested Loops)
public class vfg
{ public static void main(String args[])
{
int b=4;
for(int i=1;i<=9;i=i+2)
{ for(int j=1;j<=b;j++)
{ System.out.print(" ");
}
for(int k=1;k<=i;k++)
{ System.out.print("*");
}
System.out.println();
b--;
}
}
}
• Sum Of Double Dimensional Array Diagonals
• import java.io.*;
public class sumofdiagonals
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new
InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
int arr[][] = {{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}};
int sum=0;
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)
{ if(i==j)
sum+=arr[i][j];
}
}
System.out.println(sum);
}
}
• Arrange Names From Array Alphabetically
• import java.io.*;
public class namearranger
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
String arr[] = new String[5];
System.out.println("Enter 5 names.");
for(int i=0;i<5;i++)
{ arr[i]=br.readLine();
}
String temp=null;
System.out.println("They will now be arranged alphabetically");
for(int i=0;i<5;i++)
{ for(int j=0;j<4;j++)
{ if((int)arr[j].charAt(0) > (int)arr[j+1].charAt(0))
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i<5;i++)
{ System.out.println(arr[i]);
}
}
}
• 2 - 4 + 6 - 8......20
• import java.io.*;
public class twominusfourplussixminuseight
{
public static void main(String args[])
{ System.out.println("2-4+6-8...-20");
int a=1;
int s=0;
for(int i=2;i<=20;i+=2)
{
if(a%2!=0)
s+=i;
else
s-=i;
a++;
System.out.println(a+ " " +i+ " " +s);
}
System.out.println(s);
}
}
• Count Positive & Negative Numbers In List
• import java.io.*;
public class countposneg
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
int arr[] = new int[575];
int j=2;
System.out.println("Enter a list of numbers(terminates on entering 0)");
for(int i=0;j<3;i++)
{ arr[i] = Integer.parseInt(br.readLine());
if(arr[i]==0)
{j++; }
}
int neg=0, poseve=0, posodd=0;
for(int i=0;i
{ if(arr[i]<0)
{neg++;}
if(arr[i]>0 && arr[i]%2==0)
{poseve++;}
if(arr[i]>0 && arr[i]%2==1)
{posodd++;}
}
System.out.println("nnNumber Of Negative Numbers: "+neg);
System.out.println("Number Of Positive Even Numbers: "+poseve);
System.out.println("Number of Positive Odd Numbers: "+posodd);
}
}
• Pattern 1 :
• 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
• Java Program :
• public class MainClass
• {
• public static void main(String[] args)
• {
• Scanner sc = new Scanner(System.in);
•
• //Taking rows value from the user
•
• System.out.println("How many rows you want in this pattern?");
•
• int rows = sc.nextInt();
•
• System.out.println("Here is your pattern....!!!");
•
• for (int i = 1; i <= rows; i++)
• {
• for (int j = 1; j <= i; j++)
• {
• System.out.print(j+" ");
• }
•
• System.out.println();
• }}}}
A Program to calculate charges for sending particles when the charges are as
follows
For the first 1KG Rs.15.00 , For additional weight , for every 500gm or
fraction thereof: Rs 8.00
class Prog1
{
static void test(double wt)//user enters the weight in KGs
{
System.out.println(“Parcel Weight is “+wt);
int icharge = 15;
System.out.println(“Initial charge is “+icharge);
int rwt = (int)((wt-1)*1000);
System.out.println(“Remaining weight after deducing 1Kg “+rwt);
int rcharge = (rwt/500)*8;
System.out.println(“Charge excluding fractional part is
Rs.”+(icharge+rcharge));
int fcharge = (rwt%500>0)?8:0;
System.out.println(“Charge for fractional part is Rs. “+fcharge);
int tcharge = icharge+rcharge+fcharge;
System.out.println(“Total Charge is Rs. “+tcharge);
}
}

Presentation1 computer shaan

  • 1.
    2 . Aspecial two-digit number is such that when the sum of its digits is added to the product of its digits , the result is equal to original number Example : 59 5+9 = 14 5*9 = 45 45+14 = 59 Therefore , 59 is a special two digit number Write a program in java to check whether a number is special two-digit number or not Ans- class Prog2 { static void test(int num)//user inputs a two-digit number { int m = num; int product = 1; int sum = 0; while(m>0) { int dig = m%10; sum = sum+dig; product = product*dig; m/=10; } int finalsum = sum+product; if(finalsum==num) System.out.println(num+” is a special two digit number “); else System.out.println(num+ ” is not a special two digit number “); } }
  • 2.
    • 4.Write aprogram to display the following pattern : 1 3 5 7 9 3 5 7 9 1 5 7 8 1 3 7 9 1 3 5 9 1 3 5 7 Ans- • class Program4 { static void teja() { • for(int i = 1,l=1;i<=5;i++,l+=2) { int k = 1; • for(int j = 1,m=l;j<=5;j++,m+=2) { if(m>9) { System.out.print(k+” “); k+=2; } else if(l==5&&m==9) { System.out.print(“8 “); } else System.out.print(m+” “); } System.out.println(); } } }
  • 3.
    • 5.Write aProgram in java to obtain the first eight numbers of the following series : 1,11,111,1111……………… class Prog5 { static void test() { double s=0.0; int p; for(int i = 0;i<=7;i++) { s=s+Math.pow(10,i); p = (int)s; System.out.print(p+” , “); } } }
  • 4.
    • Star Pyramid(NestedLoops) public class vfg { public static void main(String args[]) { int b=4; for(int i=1;i<=9;i=i+2) { for(int j=1;j<=b;j++) { System.out.print(" "); } for(int k=1;k<=i;k++) { System.out.print("*"); } System.out.println(); b--; } } }
  • 5.
    • Sum OfDouble Dimensional Array Diagonals • import java.io.*; public class sumofdiagonals { public static void main(String args[]) throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(read); int arr[][] = {{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}}; int sum=0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(i==j) sum+=arr[i][j]; } } System.out.println(sum); } }
  • 6.
    • Arrange NamesFrom Array Alphabetically • import java.io.*; public class namearranger { public static void main(String args[]) throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(read); String arr[] = new String[5]; System.out.println("Enter 5 names."); for(int i=0;i<5;i++) { arr[i]=br.readLine(); } String temp=null; System.out.println("They will now be arranged alphabetically"); for(int i=0;i<5;i++) { for(int j=0;j<4;j++) { if((int)arr[j].charAt(0) > (int)arr[j+1].charAt(0)) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for(int i=0;i<5;i++) { System.out.println(arr[i]); } } }
  • 7.
    • 2 -4 + 6 - 8......20 • import java.io.*; public class twominusfourplussixminuseight { public static void main(String args[]) { System.out.println("2-4+6-8...-20"); int a=1; int s=0; for(int i=2;i<=20;i+=2) { if(a%2!=0) s+=i; else s-=i; a++; System.out.println(a+ " " +i+ " " +s); } System.out.println(s); } }
  • 8.
    • Count Positive& Negative Numbers In List • import java.io.*; public class countposneg { public static void main(String args[]) throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(read); int arr[] = new int[575]; int j=2; System.out.println("Enter a list of numbers(terminates on entering 0)"); for(int i=0;j<3;i++) { arr[i] = Integer.parseInt(br.readLine()); if(arr[i]==0) {j++; } } int neg=0, poseve=0, posodd=0; for(int i=0;i { if(arr[i]<0) {neg++;} if(arr[i]>0 && arr[i]%2==0) {poseve++;} if(arr[i]>0 && arr[i]%2==1) {posodd++;} } System.out.println("nnNumber Of Negative Numbers: "+neg); System.out.println("Number Of Positive Even Numbers: "+poseve); System.out.println("Number of Positive Odd Numbers: "+posodd); } }
  • 9.
    • Pattern 1: • 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 • Java Program : • public class MainClass • { • public static void main(String[] args) • { • Scanner sc = new Scanner(System.in); • • //Taking rows value from the user • • System.out.println("How many rows you want in this pattern?"); • • int rows = sc.nextInt(); • • System.out.println("Here is your pattern....!!!"); • • for (int i = 1; i <= rows; i++) • { • for (int j = 1; j <= i; j++) • { • System.out.print(j+" "); • } • • System.out.println(); • }}}}
  • 10.
    A Program tocalculate charges for sending particles when the charges are as follows For the first 1KG Rs.15.00 , For additional weight , for every 500gm or fraction thereof: Rs 8.00 class Prog1 { static void test(double wt)//user enters the weight in KGs { System.out.println(“Parcel Weight is “+wt); int icharge = 15; System.out.println(“Initial charge is “+icharge); int rwt = (int)((wt-1)*1000); System.out.println(“Remaining weight after deducing 1Kg “+rwt); int rcharge = (rwt/500)*8; System.out.println(“Charge excluding fractional part is Rs.”+(icharge+rcharge)); int fcharge = (rwt%500>0)?8:0; System.out.println(“Charge for fractional part is Rs. “+fcharge); int tcharge = icharge+rcharge+fcharge; System.out.println(“Total Charge is Rs. “+tcharge); } }