class FibonacciExample1 {
public static void main(String args[]) {
int n1 = 0, n2 = 1, n3, i, count = 10;
System.out.print(n1 + " " + n2); // printing 0 and 1
for (i = 2; i < count; ++i) { // loop starts from 2 because 0 and 1 are already printed
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}
}
}
नीचे दिए गए Fibonacci series Java प्रोग्राम का हर लाइन का हिंदी में विस्तार से वर्णन किया गया है:
👉 यह एक क्लास (Class) है जिसका नाम FibonacciExample1 है। Java प्रोग्राम हमेशा किसी क्लास से शुरू होता है।
👉 यह Java का मुख्य मेथड (Main Method) है, जहाँ से प्रोग्राम की शुरुआत होती है।
-
public – यह दर्शाता है कि यह मेथड सभी जगह से एक्सेस किया जा सकता है।
-
static – इसे क्लास के ऑब्जेक्ट के बिना चलाया जा सकता है।
-
void – यह कोई वैल्यू return नहीं करता।
-
main(String[] args) – Java प्रोग्राम का एंट्री पॉइंट होता है।
👉 यहाँ पर कुछ वैरिएबल्स डिक्लेयर और इनिशियलाइज़ किए गए हैं:
-
n1 = 0 और n2 = 1 – ये Fibonacci series के पहले दो नंबर हैं।
-
n3 – यह अगले नंबर के लिए उपयोग होगा।
-
i – यह loop का काउंटर है।
-
count = 10 – यह बताता है कि Fibonacci के कितने terms प्रिंट करने हैं।
👉 यह लाइन पहले दो नंबर (0 और 1) को स्क्रीन पर प्रिंट करती है।
👉 यह for लूप है, जो तीसरे नंबर से शुरू होकर कुल count terms तक चलता है।
👉 यह अगला Fibonacci नंबर निकालता है — पिछले दो नंबरों (n1 और n2) का योग।
👉 यह नया नंबर (n3) को स्क्रीन पर प्रिंट करता है।
👉 ये दो लाइनें अगले iteration के लिए पुराने नंबरों को अपडेट करती हैं:
-
n1 अब n2 बन जाता है,
-
और n2 अब n3 बन जाता है।
👉 ये ब्रेसेस for लूप, main मेथड, और क्लास को बंद करती हैं।
🔚 निष्कर्ष:
यह प्रोग्राम Fibonacci Series के पहले 10 अंक प्रिंट करता है
Q.13: wap to print tribonacci series?:_
input:_
import java.util.*;
public class Tribonacciseries {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in); // Corrected class name and capitalization
System.out.print("Enter number of terms: ");
int n = sc.nextInt(); // Corrected method name
int a = 1, b = 0, c = 1;
System.out.println("\nTribonacci series:");
if (n >= 1) System.out.print(a);
if (n >= 2) System.out.print("\t" + b);
if (n >= 3) System.out.print("\t" + c);
for (int i = 4; i <= n; i++) {
int d = a + b + c;
System.out.print("\t" + d);
a = b;
b = c;
c = d;
}
System.out.println(); // Newline at the end
}
}
output:-
Enter number of terms: 5
Tribonacci series:
1 0 1 2 3
Q.14 wap to input values into an array and display them?-
input:-
import java.util.Scanner;
public class ArrayInputExample1 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in); // Correct class name and capitalization
System.out.print("Enter the number of elements you want to store: ");
// Reading the number of elements to store in the array
n = sc.nextInt(); // Correct method name
// Creates an array in the memory of length n
int[] array = new int[n]; // Dynamic array size based on user input
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
// Reading array elements from the user
array[i] = sc.nextInt(); // Correct method name
}
System.out.println("Array elements are:");
// Accessing array elements using the for loop
for (int i = 0; i < n; i++) {
System.out.println(array[i]);
}
}
}
output:_
Enter the number of elements you want to store: 5
Enter the elements of the array:
4
4
8
7
100
Array elements are:
4
4
8
7
100
Q.14.wap to input values into an array and dislay them?
input:_
import java.util.Scanner;
public class ArrayInputExample1 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
// Reading the number of elements from the user
n = sc.nextInt();
// Creates an array in the memory of length n
int[] array = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
// Reading array elements from the user
array[i] = sc.nextInt();
}
System.out.println("Array elements are:");
// Accessing array elements using for loop
for (int i = 0; i < n; i++) {
System.out.println(array[i]);
}
sc.close(); // Good practice to close the Scanner
}
}
output:-
Enter the number of elements you want to store: 5
Enter the elements of the array:
4
4
8
7
100
Array elements are:
4
4
8
7
100
यहाँ पर Java प्रोग्राम की line-by-line हिंदी में व्याख्या दी गई है:
👉 यह लाइन Scanner क्लास को इम्पोर्ट करती है जो यूज़र से इनपुट लेने के लिए उपयोग की जाती है।
👉 यह एक सार्वजनिक (public) क्लास है जिसका नाम ArrayInputExample1 है। Java प्रोग्राम इसी क्लास से शुरू होता है।
👉 यह Java का मुख्य मेथड है जहाँ से प्रोग्राम की शुरुआत होती है।
👉 एक इन्टीजर वैरिएबल n डिक्लेयर किया गया है, जो बाद में एरे की साइज को स्टोर करेगा।
👉 Scanner ऑब्जेक्ट sc बनाया गया है जिससे हम यूज़र से इनपुट ले सकते हैं।
👉 यह लाइन यूज़र को बताती है कि उसे कितने एलिमेंट्स एरे में स्टोर करने हैं।
👉 यूज़र द्वारा दिया गया नंबर (n) इनपुट के रूप में लिया जाता है।
👉 एक इन्टीजर टाइप का एरे बनाया गया है जिसकी लंबाई n है।
👉 यूज़र से कहा जाता है कि वह एरे के एलिमेंट्स इनपुट करे।
👉 एक for लूप शुरू किया गया है जो 0 से n-1 तक चलेगा।
👉 यूज़र से इनपुट लेकर एरे के हर इंडेक्स पर वैल्यू स्टोर की जा रही है।
👉 अब यूज़र को बताया जाएगा कि उसने कौन-कौन से एलिमेंट्स डाले थे।
👉 फिर से for लूप शुरू हुआ है जिससे एरे के सभी एलिमेंट्स को एक्सेस किया जा सके।
👉 एरे का हर एलिमेंट एक-एक करके प्रिंट किया जा रहा है।
👉 Scanner को बंद कर दिया गया है। यह एक अच्छी आदत मानी जाती है जिससे रिसोर्सेस की बर्बादी ना हो।
Q.15 .wap to read an array of 10 integers and find sum of all number.
input:-
// Java program to find sum of elements in a given array
class Test {
static int arr[] = {12, 3, 4, 15};
// Method for sum of elements in an array
static int sum() {
int sum = 0; // Initialize sum
// Iterate through all elements and add them to sum
for (int i = 0; i < arr.length; i++)
sum += arr[i];
return sum;
}
// Driver method
public static void main(String[] args) {
System.out.println("Sum of given array is " + sum());
}
}
output:_
Q.16 wap to write two dimensional program?:_
input:_
public class Main {
public static void main(String[] args) {
int[][] myNumber = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumber.length; ++i) {
for (int j = 0; j < myNumber[i].length; ++j) {
System.out.println(myNumber[i][j]);
}
}
}
}
output:_
नीचे दिए गए Java प्रोग्राम का लाइन-बाय-लाइन हिंदी में विवरण प्रस्तुत है:
👉 यह लाइन एक public class Main को परिभाषित करती है।
➡️ Java में execution किसी class से ही शुरू होता है, और class का नाम Main रखा गया है (आप कुछ और भी रख सकते हैं, लेकिन फिर उसे सही से call करना होगा)।
👉 यह Java का main method है, जहाँ से प्रोग्राम की execution शुरू होती है।
➡️ public मतलब यह method सबके लिए accessible है।
➡️ static का मतलब है कि इस method को चलाने के लिए class का object बनाने की ज़रूरत नहीं है।
➡️ String[] args command line से आने वाले arguments को स्टोर करता है।
👉 यह एक 2D array (दो आयामी ऐरे) myNumber को define करता है।
➡️ इसमें दो "rows" हैं:
▪️ पहली row: {1, 2, 3, 4}
▪️ दूसरी row: {5, 6, 7}
➡️ यानी, myNumber एक 2x4 टाइप का irregular 2D array है (क्योंकि दूसरी row में केवल 3 items हैं)।
👉 यह outer loop है जो rows पर चल रहा है।
➡️ myNumber.length array में कितनी rows हैं, वह बताता है।
➡️ i हर row का index है (0 से शुरू होकर आख़िरी row तक)।
👉 यह inner loop है जो हर row के columns (elements) पर चल रहा है।
➡️ myNumber[i].length हर row में कितने elements हैं, वह बताता है।
➡️ j हर element का index है।
👉 यह हर element को एक-एक करके स्क्रीन पर प्रिंट करता है।
➡️ myNumber[i][j] का मतलब है ith row और jth column का element।
👉 ये curly braces ({}) loops और method को बंद कर रहे हैं।
🧾 आउटपुट क्या होगा?
यह कोड इस array को एक-एक करके प्रिंट करेगा:
Comments
Post a Comment