0% found this document useful (0 votes)
9 views4 pages

PROGRAMS DONE IN CLASS - 1st and 2nd JUNE 2023

Uploaded by

mayuri
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)
9 views4 pages

PROGRAMS DONE IN CLASS - 1st and 2nd JUNE 2023

Uploaded by

mayuri
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/ 4

PROGRAMS DONE IN CLASS –1st and 2nd June 2023

ARRAYS
import java.util.*;
public class SDADDA
{
private int a[] = {2,4,6,1,8};
private int b[][] = new int [3][3];
void inputDDA()
{
Scanner sc = new Scanner(System.in);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.println("Enter the value for row "+i + "column " + j);
b[i][j] = sc.nextInt();
}
}
}
void displaySDA()
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i] + " ");
}
}
void displayDDA()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
public static void main(String args[])
{
SDADDA ob = new SDADDA();
ob.inputDDA();
ob.displayDDA();
ob.displaySDA();
}
}

1|M AY U R I ’ S C O M P U T E R A C A D E M Y
Double dimensional array – left diagonal and right diagonal sum

public class DDA {


private int a[][] = {{1,2,3},{4,6,8},{11,8,10}};
//sum of all the values in a DDA
void addvalues()
{
int sum=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
sum = sum + a[i][j];
}
}
System.out.println("Total = " + sum);
}
void sumLeftDiagonal()
{
int sum=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
sum = sum+a[i][j];
}
}
System.out.println("Left diagonal sum = "+sum);
}

void sumRightDiagonal()
{
int sum=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
if(i+j==a.length-1)
sum = sum+a[i][j];
}
}
System.out.println("Right diagonal sum = "+sum);
}

2|M AY U R I ’ S C O M P U T E R A C A D E M Y
public static void main(String args[])
{
DDA d = new DDA();
d.sumLeftDiagonal();
d.sumRightDiagonal();
d.addvalues();
}
}

BUBBLE SORT

public class Bubblesort


{
private int a[] = {7,8,1,2,3};
void sortAsc()
{
for(int i=0;i<a.length-1;i++)//no.of passes
{
for(int j=0;j<a.length-1;j++)
{
if(a[j]>a[j+1])//change sign to < to sort in descending
{
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}}
}
}
void display()
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+ " ");
}
}
public static void main(String args[])
{
Bubblesort sb = new Bubblesort();
System.out.println("Values before sorting in ascending order");
sb.display();
sb.sortAsc();
System.out.println("Values after sorting in ascending order");
sb.display();
}
}

3|M AY U R I ’ S C O M P U T E R A C A D E M Y
LINEAR SEARCH

import java.util.*;
public class LinearSearch
{
private String name[] = {"Manan","Isha","Mayuri","Varun","Tanya"};
void search()
{
Scanner sc = new Scanner(System.in);
System.out.println("Which name you wish to search?");
String n = sc.nextLine();
boolean b=false;
for(int i=0;i<name.length;i++)
{
if(n.equalsIgnoreCase(name[i]))
{
b = true;
break;
}
}
if(b==true)
System.out.println("Name found");
else
System.out.println("Name not found");
}
public static void main(String args[])
{
LinearSearch ls = new LinearSearch();
ls.search();
}
}

4|M AY U R I ’ S C O M P U T E R A C A D E M Y

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy