0% found this document useful (0 votes)
108 views

Question 1: List of Odd Numbers From 1 To 100

The document contains code snippets for 7 questions that involve common programming tasks: 1. Listing odd numbers from 1 to 100 2. Listing prime numbers from 1 to 100 3. Calculating factorials from 1 to 10 using recursion 4. Generating the Fibonacci series using recursion and loops 5. Checking if a user-input string is a palindrome by reversing and comparing to the original

Uploaded by

Ahson Khan SN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Question 1: List of Odd Numbers From 1 To 100

The document contains code snippets for 7 questions that involve common programming tasks: 1. Listing odd numbers from 1 to 100 2. Listing prime numbers from 1 to 100 3. Calculating factorials from 1 to 10 using recursion 4. Generating the Fibonacci series using recursion and loops 5. Checking if a user-input string is a palindrome by reversing and comparing to the original

Uploaded by

Ahson Khan SN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 1 : List of Odd numbers from 1 to 100

1. /*
2. List Odd Numbers Java Example
3. This List Odd Numbers Java Example shows how to find and list odd
4. numbers between 1 and any given number.
5. */
6.
7. public class ListOddNumbers {
8.
9. public static void main(String[] args) {
10.
11. //define the limit
12. int limit = 50;
13.
14. System.out.println("Printing Odd numbers between 1 and " + limit);
15.
16. for(int i=1; i <= limit; i++){
17.
18. //if the number is not divisible by 2 then it is odd
19. if( i % 2 != 0){
20. System.out.print(i + " ");
21. }
22. }
23. }
24. }

Question 2: List of PrimeNumbers from 1 to 100

class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";

for (i = 1; i <= 100; i++)


{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);
}
}

Question 3: List of Factorials from 1 to 10

public class iterative {

public static int iteration(int n) {


int result;
if(n==1)return n;
else
result = n*iteration(n-1);
return result;
}

public static void main(String[] args) {


System.out.println("Result is :" + iteration(9));
}
}

Question 4: List of fibonnaci series

package crunchify.com.tutorials;

import java.util.Scanner;

/**
* @author Crunchify.com
*
*/
public class CrunchifyFibonacci {

@SuppressWarnings("resource")
public static void main(String args[]) {

// input to print Fibonacci series upto how many numbers


log("Enter number upto which Fibonacci series to print: ");
int number = new Scanner(System.in).nextInt();
log("\nUsing Method-1: Using Recursion. Provided Number: " + number);
// printing Fibonacci series upto number
for (int i = 1; i <= number; i++) {
log(fibonacciRecusion(i) + " ");
}

log("\nMethod-2: Fibonacci number at location " + number + " is ==> " +


(fibonacciLoop(number) + ""));

// Method-1: Java program for Fibonacci number using recursion.


public static int fibonacciRecusion(int number) {
if (number == 1 || number == 2) {
return 1;
}

return fibonacciRecusion(number - 1) + fibonacciRecusion(number - 2); // tail


recursion
}

// Method-2: Java program for Fibonacci number using Loop.


public static int fibonacciLoop(int number) {
if (number == 1 || number == 2) {
return 1;
}
int fibo1 = 1, fibo2 = 1, fibonacci = 1;
for (int i = 3; i <= number; i++) {
fibonacci = fibo1 + fibo2; // Fibonacci number is sum of previous two
Fibonacci number
fibo1 = fibo2;
fibo2 = fibonacci;

}
return fibonacci; // Fibonacci number
}

private static void log(String number) {


System.out.println(number);

Question 7 : Palindrome string

import java.util.*;

class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1; i >= 0; i-- )


reverse = reverse + original.charAt(i);

if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");

}
}

You might also like