Classification of numbers

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 More Conditionals Instructor: Mainak Chaudhuri
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
1 Repetition structures Overview while statement for statement do while statement.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Conditionals Instructor: Mainak Chaudhuri
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Chapter 4: Control Structures SELECTION STATEMENTS.
Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function.
Chapter 4: Control Structures II
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
1 Conditionals Instructor: Mainak Chaudhuri
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 9 Repetition.
AKA the birth, life, and death of variables.
Instructor: Mainak Chaudhuri
Control Structures.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
Chapter 5 Repetition.
Flow Control in Java.
Decision statements. - They can use logic to arrive at desired results
Cube and its properties
Nested Loop Review and Two-Dimensional Arrays
The for-loop and Nested loops
Chapter 9 Control Structures.
Follow the Rainbow By Teresa Jennings
AKA the birth, life, and death of variables.
Lecture Notes – Week 2 Lecture-2
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Perfect squares class identifySquareButLessClever {
Program Flow.
Repetition Statements
Additional control structures
Control Structure.
Follow The Rainbow By Teresa & Paul Jennings
Presentation transcript:

Classification of numbers class classifyNumbers { public static void main (String arg[]) { int n = 8; switch (n) { case 0 : System.out.println(“Zero!”); break; case 1 : System.out.println(“Smallest positive!”); case 2 : System.out.println(“Smallest prime!”); // continued in next slide

Classification of numbers case 3 : System.out.println(“Smallest odd prime!”); break; case 4 : System.out.println(“Smallest prime squared!”); case 5 : System.out.println(“Number of fingers!”); case 6 : System.out.println(“Smallest perfect!”); // continued in next slide

Classification of numbers case 7 : System.out.println(“North seven stars!”); break; case 8 : System.out.println(“Smallest prime cubed!”); case 9 : System.out.println(“Smallest odd prime squared!”); default : System.out.println(“Not a digit!”); } // end of switch } // end of main } // end of class

More classification class differentClassification { public static void main (String arg[]) { int n = 8; switch (n) { case 2: case 3: case 5: case 7: System.out.println(“Prime!”); break; case 1: case 4: case 9: System.out.println(“Square!”); // continued in next slide

More classification case 6: System.out.println(“Perfect!”); break; System.out.println(“Cube!”); case 0: System.out.println(“Zero the Great!”); default: System.out.println(“Not a digit!”); } // end switch } // end main } // end class

More example of switch class rainbow { public static void main (String arg[]) { char c = ‘V’; switch (c) { case ‘V’ : case ‘v’ : System.out.println (“Violet”); break; case ‘I’ : case ‘i’ : System.out.println (“Indigo”); case ‘B’ : case ‘b’ : System.out.println (“Blue”);

More example of switch case ‘G’ : case ‘g’ : System.out.println (“Green”); break; case ‘Y’: case ‘y’ : System.out.println (“Yellow”); case ‘O’ : case ‘o’ : System.out.println (“Orange”); case ‘R’ : case ‘r’ : System.out.println (“Red”); // continued in next slide

More example of switch default : System.out.println (“You are not in rainbow!”); break; }

Nested loops Loop within loop for (i=0; i<=100; i++) { for (j=0; j<=100; j++) { System.out.println (i+j); } Number of loops is called the depth of the nest The innermost loop executes most frequently The outermost loop executes least You can nest while loops within for loops and vice-versa Loop variables should be different for different loops e.g., i and j in this case (what happens if both are i ?)

Nested loops for (i1=p1; i1<q1; i1++) { // outermost statements1 // can be empty for (i2=p2; i2<q2; i2++) { statements2 // can be empty for (i3=p3; i3<q3; i3++) { statements3 // can be empty … for (iN=pN; iN<qN; iN++) { // innermost statementsN } } // How many times does statementsK execute?

Nested-for example class tables { public static void main (String args[]) { int i,j,columns,rows; columns=15;rows=15; for (i=1;i<=rows;i=i+1){ for (j=1;j<=columns;j=j+1){ if (i*j < 10) System.out.print(" "+i*j+" "); else if (i*j < 100) System.out.print(" "+i*j+" "); else System.out.print(i*j+" "); } System.out.println();

All perfect numbers class allPerfectNumbersUptoOneLakh { public static void main (String arg[]) { int n, d, sigma_n; for (n=2; n<=100000; n++) { sigma_n = 1+n; for (d=2; d<=n/2; d++) { if ((n%d)==0) { sigma_n += d; } if (sigma_n == 2*n) { System.out.println (n + “is perfect.”);