Sunteți pe pagina 1din 16

Program 33

public class Solution{


public static void print_sums(){
int sum = 0 ;
for(int i = 1 ; i <= 10 ; i++){
for(int j = 1 ; j <= i; j++ ){
sum = sum + j ;
}
System.out.println( sum) ;
sum = 0 ;
}
}
public static void main(String[] args)
{
print_sums() ;
}
}

Program 34

import java.util.Scanner;
public class Sum_Series
{
public static void main(String[] args)
{
double sum = 0;
int n;
System.out.println("1/1! + 2/2! + 3/3! + ..N/N!");
Scanner s = new Scanner(System.in);
System.out.print("Enter the no. of terms in series:");
n = s.nextInt();
Sum_Series obj = new Sum_Series();
for(int i = 1; i <= n; i++)
{
sum = sum + (double)i / (obj.fact(i));
}
System.out.println("Sum of series:"+sum);
}
int fact(int x)
{
int mul = 1;
while(x > 0)
{
mul = mul * x;
x--;
}
return mul;
}
}

Program 35
import java.util.Scanner;

public class Sum_Series


{
public static void main(String[] args)
{
double sum = 0;
int n;
System.out.println("1/1! + 2/2! + 3/3! + ..N/N!");
Scanner s = new Scanner(System.in);
System.out.print("Enter the no. of terms in series:");
n = s.nextInt();
Sum_Series obj = new Sum_Series();
for(int i = 1; i <= n; i++)
{
sum = sum + (double)i / (obj.fact(i));
}
System.out.println("Sum of series:"+sum);
}
int fact(int x)
{
int mul = 1;
while(x > 0)
{
mul = mul * x;
x--;
}
return mul;
}
}

Program 36
import java.util.Scanner;

class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search)
/* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) +
".");
break;
}
}
if (c == n) /* Searching element is absent */
System.out.println(search + " is not present in array.");
}
}

Program 37
import java.util.Scanner;

class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last
= n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + (middle + 1) +
".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list.\n");

}
}

Program 38
import java.util.Scanner;

class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap
= array[d];
array[d]
= array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");

for (c = 0; c < n; c++)


System.out.println(array[c]);

Program 39
package com.java2novice.algos;

public class MySelectionSort {


public static int[] doSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}

return arr;

public static void main(String a[]){


int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doSelectionSort(arr1);

for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}

Program 40
package com.java2novice.algos;

public class DecimalToBinary {


public void printBinaryFormat(int number){
int binary[] = new int[25];
int index = 0;
while(number > 0){
binary[index++] = number%2;
number = number/2;
}
for(int i = index-1;i >= 0;i--){
System.out.print(binary[i]);
}
}
public static void main(String a[]){
DecimalToBinary dtb = new DecimalToBinary();
dtb.printBinaryFormat(25);
}

Program 41

import java.io.*;
class prog_19 {
public static void main(String argv[])throws IOException
{
int k=0,i,j,sum=0,temp=0;
System.out.println(To find sum of series 1+(1+2)+(1+2+3)+_______+(1+2+3+_____+N) );
System.out.println(Enter the number : );
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str;
str=br.readLine();
i = str.length();
k = Integer.parseInt(str);
for(i=1;i<=k;i++)
for(j=1;j<=i;j++)
sum = sum+j;
temp = temp+sum;
System.out.println(Sum of series is +temp);
}
}

Program 43
import java.util.*;
public class Initials
{

void Initials()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Full name:" );
String name=sc.nextLine();
int l=name.length();
int pos=0;
for(int i=l-1;i>=0;i--)
{
char ch=name.charAt(i);
if(ch==' ')
{
pos=i;
break;
}
}
System.out.print("The initials are: ");
System.out.print(name.charAt(0)+".");
for(int x=1;x<pos;x++)
{
char ch=name.charAt(x);
if(ch==' ')
{
System.out.print(name.charAt(x+1)+".");
}
}
}
}

Program 44
import java.io.*;
class prog5

{
void main()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the string");
String s = br.readLine();
int i,l;
char ch;
l=s.length();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(Character.isUpperCase(ch))
System.out.print(Character.toLowerCase(ch));
else if(Character.isLowerCase(ch))
System.out.print(Character.toUpperCase(ch));
else
System.out.print(ch);
}
}
}

Program 45

public class Class {

public static void main(String[]


args) {
// TODO Auto-generated method
stub
int
int
int
int
int
int
int

n1 = 5;
n2 = 3;
n3 = 6;
n4 = 8;
max = Math.max(n2 ,n4);
min = Math.min(n2, n4);
sum = max + min;

System.out.println(sum);
}
}

Program 46

import java.io.*;
class factors
{
static void main()throws IOException
{
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
int a,i;
System.out.print("Enter the number : ");
a=Integer.parseInt(buf.readLine());
System.out.print("\n");
System.out.print("The factors are : ");
for(i=1;i<=a/2;i++)
{
if(a%i==0)
System.out.print(i+",");
}
System.out.print(a);
}
}

Program 47
public class JavaProgram
{
public static void main(String args[])
{
String ch;
int i;
for(i=1; i<=255; i++)
{
ch = new Character((char)i).toString();
System.out.print(i+ " -> " + ch + "\t");
}
}
}

Program 48

class StringRev{
public static void main(String args[]){
String str = "He is the one";
String temp = "";
String finalString = "";
for(int i =str.length()-1;i>=0;i--){
temp +=i!=0?str.charAt(i):str.charAt(i)+" ";
if(str.charAt(i) == ' '||i==0){
for(int j=temp.length()-1;j>=0;j--){
finalString += temp.charAt(j);
}
temp = "";
}
}
System.out.println(finalString);
}
}

Program 49
package commissioncalculator;

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
/**
* @author Marci
*/
public class CommissionCalculator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String input; //Input of user
double salary; //This is the annual sales value
double rate; //This is the commission rate
double commission; //This is the amount of commision made
double pay; //Salesperson's pay
double sales; //annual sales
double incentive; //sales incentive
DecimalFormat dollar = new DecimalFormat("#,##0.00");
DecimalFormat percent = new DecimalFormat("##0.0%");
input = JOptionPane.showInputDialog("Enter the annual salary.");
salary = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter current commission rate.");
rate = Double.parseDouble (input);
input = JOptionPane.showInputDialog("Enter the current sales target.");
sales = Double.parseDouble (input);
commission = rate * salary;
pay = commission + salary;
JOptionPane.showMessageDialog(null, "Commission rate is " +
percent.format(rate) + ". The amount of pay is $" +
dollar.format(pay));
public incentive() {
if (sales>=(.80 * sales) && sales<=120,000)
incentive = (.75 * sales);
else if (sales>120,000)
incentive = (1.25 * .75 * sales);
}
}
System.exit(0);
}

Program 50
public class Factorial {
public static void main(String[] args) {
int n = 7;
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
System.out.println("The factorial of 7 is " + result);
}
}

S-ar putea să vă placă și