Some more programs and basic logic

How to get an input from a user:

use of function ShowInputDialog(“”). but it shows error. We have to include JOptionpane.ShowInputDialog(“”). it still gives error. so we include header library import javax.swing.*;

import java.util.Scanner;

class Ss
{
public static void main(String s[])
{
System.out.println(“Enter a number from 1-7”);
Scanner r= new Scanner(System.in);
int d=r.nextInt();
do
{
System.out.println(“Yes”);
d++;
} while(d<7);
}
}

How to convert a string value to integer value

Integer.ParseInt(“the string that has to be converted into integer value”)

Use of loops

  1. While
  2. for
  3. do-while
  4. foreach

Each loop needs initialization, condition, increment/decrement.

Assignments

1. WAP for sum of all digits.

class sum

{

public static void main(String[] args)

{ int sum=0;

while(n!=0)

{

sum=n%10;

n%=10;

}

System.out.print(“sum of all digits:”+sum);

 

 

2.WAP to sum of first and last digit,

class sum

{

public static void main(String[] args)

{

int no,last_digit,first_digit,total;

no=145;

last_digit=n%10;

total=last_digit;

first_digit=(no/1000)%10;

total=total+first digit;

System.out.print(” Sum :” +total);

}

 

 

3. WAP for cash denomination.

class sum

{

h=amt/100;

amt=amt%100;

f=amt/50;

amt=amt%50;

ten=amt/20;

amt=amt%20;

ten=amt/10;

System.out.print(“cash denomination is :” +amt);

 

}

Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s