Share on Google+Share on Google+

Amit
programs on for loop
6 Answer(s)      6 years and a month ago
Posted in : Java Beginners
a. write a program to find squares and cubes of 1st 10 integers
b. write a program to to calculate factorial of a no,
c. write a program to print Fibonacci series
d. write a program to check whether entered no is prime or not
e. write a program to print given pattern
          *
        * * *
      * * * * *
        * * * 
          *                                                                             

Ads

View Answers

March 26, 2011 at 11:17 AM


e. write a program to print given pattern * * * * * * * * * * * * *



March 26, 2011 at 1:44 PM


1)

class SquaresAndCubes 
{
    public static void main(String[] args)
    {
        System.out.println("No    Square    Cube");

        for(int i=1;i<=10;i++){
            int sq=i*i;
            int cu=i*i*i;
            System.out.println(i+"\t "+sq+"\t "+cu);
        }
    }
}


March 26, 2011 at 1:46 PM


2)

import java.util.*;
class  Factorial{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter value of m: ");
        int m=input.nextInt();
        long num=m;
        for(int i=m;i>1;i--){
            num=num*(i-1);
        }
        System.out.println(num);
    }
}


March 26, 2011 at 1:47 PM


3)

import java.util.*;

public class Fibonacci{
public static void main(String[] args) throws Exception {
Scanner input=new Scanner(System.in);
System.out.print("Enter value of n: ");
int sum=0;
    int num = input.nextInt();
    int f1=0,f2=0,f3=1;
    for(int i=1;i<=num;i++){
        System.out.println(f3);
        sum+=f3;
        f1=f2;
        f2=f3;
        f3=f1+f2;
    }
    System.out.println("Sum of Fibonacci Series: "+sum);
}
}


March 26, 2011 at 1:48 PM


4)

import java.util.*;

class  CheckPrimeNumber
{
    static boolean isPrime(int number){
          boolean isPrime=false;
          int i=(int) Math.ceil(Math.sqrt(number));
          while (i>1){
             if((number!=i) && (number%i==0)){
                isPrime=false;
                break;
             }
             else if (!isPrime)
             isPrime=true;
             --i;
          }
          return isPrime;
       }
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.println("Enter Number:");
        int num=input.nextInt();
        if(isPrime(num)){
            System.out.println("Number is Prime!");
        }
        else{
            System.out.println("Number is not prime!");
        }
    }
}


March 26, 2011 at 2:48 PM


5)

public class PatternExample {

    public static void main(String[] args) { 
        int n = 2;

        for (int i = -n; i <= n; i++) {
            for (int j = -n; j <= n; j++) {
                if (Math.abs(i) + Math.abs(j) <= n){
                    System.out.print("* ");
                }
                else{ 
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}









Related Tutorials/Questions & Answers:
programs
programs  give some programs on control statementds
loop
loop  i want to write my name(inder) through loop in java using star
Advertisements
loop
loop  what is the difference b/w do while,while and for loop..with example
for loop
for loop  what is for loop   Loops are used for iteration... for a number of times it is finite loop and if repetition is done infinitely it is infinite loop. Java provides several loop structures for iteration. Those
For loop
For loop  Hi! Please explain the following loop: for(i=0;i<a[j];i++) THANX!!   Please reply as soon as possible
for loop
for loop  how to performs the for loop when 2 inputs given like displays the triangle shape and how to take initial values or declaration values plz explain in detailed ex. 1 12 123 1234
Loop
to understand a loop my quesiton are: what du i do wrong in my code since.... (made in blueJ) Class Loop public void loopUpGeneral(int start, int stop... dk  Hi, Please check the for loop once again: for (int i
For Loop
For Loop  Write a program using a for loop that computes the following sum: 1/2 + 2/2 + 3/2 + 4/2 + 5/2 + ... N/2 N will be an integer limit the user enters. This is an example of my upcoming quiz. I need help on knowing
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
loop
loop  strong textHello can someone help me with this problem,I need to output the following code using a loop the code is as follows with the user inputs in inverted commas,thanks. ENTER CODE(XX to Stop) CODE:"D24" QUANTITY:"2
Programs in java
Programs in java  Hi, What are the best programs in java for a beginner? Thanks
php programs
php programs  10 programs on varibles
java programs
java programs  Why word "static" is used in java programs
Java Programs
Java Programs  Hi, What is Java Programs? How to develop application for business in Java technology? Is there any tool to help Java programmer in development of Java Programs? Thanks
php programs
php programs  I need 15 programs on arithmetic operators with comments. Please help me
java programs
java programs  Explain types of Java programs. Also explain how to compile and run them.   Types of Java Programs: Standalone Applications Web Applications Enterprise Applications Console Application Web services
programs in java
programs in java  . I need an application for managing an educational institute. That application should provide the details of Students Courses Faculty Fee details etc.., pl z guide me how to write these programe
java programs
java programs  i need help in understanding the whole concept of the 13 java programs that i hav...here r de programs.. int i,j,m,n; m=Integer.parseInt(args [0]); n=Integer.parseInt(args [1]); System.out.print
c++ programs
c++ programs  i want the syntax for the following programs according to c++ pattern. 1.sum of multiples of an integer upto 10 2.2/4+4/6+6/8+8/10+.....n/n+2 3.1!/1+2!/2+3!/3+4!/4+...n!/n i just want the main logic as i m nt
Java Programs
of their squares. Use a loop. Print out the following- be sure to use loop
java programs
java programs  A union B, transpose of matric, denomination of a given number i need java programs for this category?   Hi Friend, Transpose of matrix: import java.util.*; public class Transpose { public
For..in loop
For..in loop         In this tutorial we will study for..in loop which is similar to for each loop of C#, Java and other popular languages, in this example
Java for loop
Java for loop  Can a for statement loop indefinitely
For Loop - Struts
For Loop  Hi, How to use for loop in Struts2. I don't want use.... How to use the For loop in JSP. Thanks, Rajesh.  Hi Rajesh, i am sending simple use of for loop. In this example, you will see the use
for( ) loop in php
for( ) loop in php  Generally, when we use for loop in PHP or any other programming language why we use i++ and not ++i. What is the reason behind
Limits of "For loop"
Limits of "For loop"  Hi, How can I check the upper limit and lower limit of 'FOR' loop during unit testing using Junit. Thanks & Regards Ankit
for( ) loop in php
for( ) loop in php  Generally, when we use for loop in PHP or any other programming language why we use i++ and not ++i. What is the reason behind
How to write a loop and a while loop
How to write a loop and a while loop  How do I write a 1 loop and a 1 while loop for the example code: public boolean isTheFirstOneBigger (int num1, int num2) { if (num1 > num2) { return true
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads