4.3 SOME MORE EXAMPLES USING LOOP

4.3 লুপ ব্যৱহাৰ কৰি আৰু কিছুমান উদাহৰণ

In this section, we will do some interesting things using loop. We will use the for loop to write the programs here. zYou may try using a while loop as well.

এই শাখাত, আমি লুপ ব্যৱহাৰ কৰি কিছুমান আকৰ্ষণীয় কাম কৰিম। আমি ইয়াত প্ৰ’গ্ৰামবোৰ লিখিবলৈ লুপৰ বাবে ব্যৱহাৰ কৰিম। জেডআপুনি অলপ সময়ৰ লুপ ব্যৱহাৰ কৰিবলৈচেষ্টা কৰিব পাৰে।

A. Let us write a C program to display the following pattern pattern on our monitor

A. আমাৰ মনিটৰত নিম্নলিখিত পেটাৰ্ণ ৰ আৰ্হি প্ৰদৰ্শন কৰিবলৈ আমি এটা চি প্ৰ’গ্ৰাম লিখোঁ আহক

X

XX

XXX

XXXX

XXXXX

The purpose of the program is simply to display “X” in a systematic way. In the first line, the program needs to display only one X. In the second line, it displays X two times with a space in between. Similarly, in the third line, the program displays X three times and so on.

কাৰ্যসূচীটোৰ উদ্দেশ্য হৈছে কেৱল “এক্স” এক প্ৰণালীবদ্ধ উপায়েৰে প্ৰদৰ্শন কৰা। প্ৰথম শাৰীত, প্ৰ’গ্ৰামটোৱে কেৱল এটা এক্স প্ৰদৰ্শন কৰিব লাগিব। দ্বিতীয় শাৰীত, ই মাজত স্থান ৰখাৰ সৈতে এক্স দুবাৰ প্ৰদৰ্শন কৰে। একেদৰে, তৃতীয় শাৰীত, প্ৰ’গ্ৰামটোৱে তিনিবাৰ এক্স প্ৰদৰ্শন কৰে ইত্যাদি।

You can see from the program (Example 4.11) that we have used a for loop to display “X”. In the first line as we need to display X only time, we used the loop so that it iterates only one time and it displays X in its iteration. Then we printed a line using printf(“\n”) to go to the next line.

আপুনি প্ৰ’গ্ৰামটোৰ (উদাহৰণ 4.11) পৰা চাব পাৰে যে আমি “এক্স” প্ৰদৰ্শন কৰিবলৈ লুপৰ বাবে এটা ব্যৱহাৰ কৰিছোঁ। প্ৰথম শাৰীত যিহেতু আমি কেৱল সময়হে এক্স প্ৰদৰ্শন কৰিব লাগিব, আমি লুপটো ব্যৱহাৰ কৰিছিলো যাতে ই কেৱল এবাৰহে পুনৰাবৃত্তি কৰে আৰু ই ইয়াৰ পুনৰাবৃত্তিত এক্স প্ৰদৰ্শন কৰে। তাৰ পিছত আমি পৰৱৰ্তী শাৰীলৈ যাবলৈ প্ৰিণ্টফ (“”এন”) ব্যৱহাৰ কৰি এটা শাৰী প্ৰিণ্ট কৰিলোঁ।

In the second line, we need to display X two times. Thus the loop needs to iterate two times. We used the same signature in the loop as before.

দ্বিতীয় শাৰীত, আমি দুবাৰ এক্স প্ৰদৰ্শন কৰিব লাগিব। এনেদৰে লুপটো দুবাৰ পুনৰাবৃত্তি কৰিব লাগিব। আমি আগৰ দৰে লুপত একেটা স্বাক্ষৰ ব্যৱহাৰ কৰিছিলো।

As we are required to display 5 lines, we have used 5 for loops in the program. Job of all the for loops was to display “X” but a different number of times. Thus only the condition checking expressions of the for loops were different.

যিহেতু আমি 5 টা শাৰী প্ৰদৰ্শন কৰিব লাগিব, আমি প্ৰ’গ্ৰামটোত লুপৰ বাবে 5 টা ব্যৱহাৰ কৰিছোঁ। লুপৰ বাবে সকলোবোৰকাম আছিল “এক্স” প্ৰদৰ্শন কৰা কিন্তু বেলেগ সংখ্যক বাৰ। এনেদৰে কেৱল লুপৰ বাবে অৱস্থা পৰীক্ষাৰ অভিব্যক্তি পৃথক আছিল।

B. Let us write a C program to display the following pattern on our monitor.

B. আমাৰ মনিটৰত নিম্নলিখিত আৰ্হিটো প্ৰদৰ্শন কৰিবলৈ আমি এটা চি প্ৰ’গ্ৰাম লিখোঁ আহক।

XXXXX

XXXX

XXX

XX

X

This pattern is similar to the above pattern. The difference is that in the first line, we need to display “X” 5 times instead of one. Then 4 times in the next line and so on. The program is presented using Example 4.12. the first for loop in writen so that it iterates 5 times. The second one in writen so that it iterates 4 times and so on.

এই আৰ্হিটো ওপৰোক্ত আৰ্হিৰ দৰে একে। পাৰ্থক্যটো হ’ল প্ৰথম শাৰীত, আমি এটাৰ সলনি 5 বাৰ “এক্স” প্ৰদৰ্শন কৰিব লাগিব। তাৰ পিছত পৰৱৰ্তী শাৰীত 4 বাৰ ইত্যাদি। প্ৰ’গ্ৰামটো উদাহৰণ 4.12 ব্যৱহাৰ কৰি উপস্থাপন কৰা হৈছে। লিখাৰ লুপৰ বাবে প্ৰথমটো যাতে ই 5 বাৰ পুনৰাথিত হয়। লিখাদ্বিতীয়টো যাতে ই 4 বাৰ আৰু এনেদৰে ইৰিটেট কৰে।

C. Let us write a C program to display the following pattern on our monitor.

গ. আমাৰ মনিটৰত নিম্নলিখিত আৰ্হিটো প্ৰদৰ্শন কৰিবলৈ আমি এটা চি প্ৰ’গ্ৰাম লিখোঁ আহক।

XXXXX

XXXX

XXX

XX

X

XX

XXX

XXXX

XXXXX

This pattern is also similar to the above two patterns. We need to display the character “X” a different number of times in different lines.

এই আৰ্হিটো ওপৰোক্ত দুটা আৰ্হিৰ সৈতে একে। আমি “এক্স” চৰিত্ৰটো বিভিন্ন শাৰীত বেলেগ বেলেগ বাৰ প্ৰদৰ্শন কৰিব লাগিব।

Line 1: 5 times

Line 2: 4 times

Line 3: 3 times

Line 4: 2 times

Line 5: 1 times

Line 6: 2 times

Line 7: 3 times

Line 8: 4 times

Line 9: 5 times

We will be using 9 for loops. Each for loop displays the character “X”. First loop will display 5 times, the second loop will display 4 times, and so on as per the list mentioned above. Refer to Example 4.13 for the complete program.

আমি লুপৰ বাবে 9 ব্যৱহাৰ কৰিম। লুপৰ বাবে প্ৰতিটোৱে “এক্স” বৰ্ণটো প্ৰদৰ্শন কৰে। প্ৰথম লুপটো 5 বাৰ প্ৰদৰ্শিত হ’ব, দ্বিতীয় লুপটো 4 বাৰ প্ৰদৰ্শিত হ’ব, আৰু সেয়েহে ওপৰত উল্লেখ কৰা তালিকা অনুসৰি। সম্পূৰ্ণ প্ৰ’গ্ৰামৰ বাবে উদাহৰণ 4.13 চাওক।

We observe that the length of the program is increasing as we are writing more loops. We also need to observe that all the for loops were essentially doing the same work – displaying the character “X”. In the next chapter, we will make use of this observation and learn something interesting where we will write a loop inside another loop. This will significantly reduce the program length.

আমি লক্ষ্য কৰোঁ যে আমি অধিক লুপ লিখি থকাৰ লগে লগে কাৰ্যসূচীটোৰ দৈৰ্ঘ্য বৃদ্ধি পাইছে। আমি এইটোও লক্ষ্য কৰিব লাগিব যে সকলো বোৰ লুপে মূলতঃ একেই কাম কৰি আছিল – “এক্স” চৰিত্ৰটো প্ৰদৰ্শন কৰা। পৰৱৰ্তী অধ্যায়ত, আমি এই পৰ্যৱেক্ষণটো ব্যৱহাৰ কৰিম আৰু কিবা আকৰ্ষণীয় শিকিম য’ত আমি আন এটা লুপৰ ভিতৰত এটা লুপ লিখিম। ই প্ৰ’গ্ৰামৰ দৈৰ্ঘ্য যথেষ্ট হ্ৰাস কৰিব।

D. A C program to extract the individual digits from a given integer.

প্ৰদত্ত ইণ্টেগাৰৰ পৰা গাইগুটীয়া অংকবোৰ আহৰণ কৰিবলৈ ঘ. এ চি প্ৰ’গ্ৰাম।

Here, we are given an integer and we need to find all the digits from it For example, suppose we consider an integer 8724. The digits of the integer will be 8, 7, 2, and 4.

ইয়াত, আমাক এটা ইণ্টেগাৰ দিয়া হৈছে আৰু আমি ইয়াৰ পৰা সকলো বোৰ অংক বিচাৰি উলিয়াব লাগিব উদাহৰণ স্বৰূপে, ধৰি লওঁক আমি এটা ইণ্টেগাৰ 8724 বিবেচনা কৰোঁ। ইণ্টেগাৰৰ অংকবোৰ হ’ব 8, 7, 2, আৰু 4।

Let us try to develop the logic of the problem. Then we can write the logic using C programming language’s syntax to build the program.

আহক আমি সমস্যাটোৰ যুক্তি বিকশিত কৰিবলৈ চেষ্টা কৰোঁ আহক। তাৰ পিছত আমি প্ৰ’গ্ৰামটো নিৰ্মাণ কৰিবলৈ চি প্ৰ’গ্ৰামিং ভাষাৰ বাক্যবিন্যাস ব্যৱহাৰ কৰি যুক্তি লিখিব পাৰোঁ।

In order to extract the digits, we can use the remainder or modulus operation (%). If we perform a modulus operation on the given number with 10, we can extract the last digit, that is 4 because we know 8724 % 10 = 4.

অংকবোৰ আহৰণ কৰিবলৈ, আমি অৱশিষ্ট বা মডুলাছ অপাৰেচন (%) ব্যৱহাৰ কৰিব পাৰোঁ। যদি আমি প্ৰদত্ত নম্বৰত 10 ৰ সৈতে এটা মডুলাছ অপাৰেচন কৰোঁ, আমি অন্তিম অংকটো আহৰণ কৰিব পাৰোঁ, অৰ্থাৎ 4 কাৰণ আমি জানো 8724% 10 = 4।

If we want to extract the next digit from the last, we need to have a number 872. Now, 872 can be calculated from 8724 by performing a division operation by 10. That is, 8724 / 10 = 872. We perform these two operations repeatedly – the modulus operation to extract a digit and the division operation to reduce the number by one tenth. Refer to the program represented using Example 4.14. The modulus operation is performed in line number 10 and the division operation is performed in line number 1

যদি আমি অন্তিমটো অংকৰ পৰা পৰৱৰ্তী অংকটো আহৰণ কৰিব বিচাৰো, আমাৰ ওচৰত 872 নম্বৰ থাকিব লাগিব। এতিয়া, 8724-ৰ পৰা 10 ৰ ভিতৰত এটা বিভাগ অপাৰেচন কৰি 872 গণনা কৰিব পাৰি। অৰ্থাৎ, 8724 / 10 = 872। আমি এই দুটা অপাৰেচন বাৰে বাৰে সম্পাদন কৰোঁ – এটা অংক আহৰণ কৰিবলৈ মডুলাছ অপাৰেচন আৰু সংখ্যাটো এক দশমাংশ হ্ৰাস কৰিবলৈ ডিভিজন অপাৰেচন। উদাহৰণ 4.14 ব্যৱহাৰ কৰি প্ৰতিনিধিত্ব কৰা প্ৰ’গ্ৰামটো চাওক। মডুলাচ অপাৰেচনটো শাৰী 10 ত সম্পন্ন কৰা হয় আৰু ডিভিজন অপাৰেচনটো শাৰী নম্বৰ 1-ত সম্পন্ন কৰা হয়।

In the program, we did not want to change the actual number that is taken as input from the keyboard and hence, we took help of a temporary variable “temp”. The variable is initialized with the value that was read from the keyboard (line number 7 in Example 4.14). Then the modulus and division operations were performed on the temporary variable (line number 10 and 12 in Example 4.14).

প্ৰ’গ্ৰামটোত, আমি কীবোৰ্ডৰ পৰা ইনপুট হিচাপে লোৱা প্ৰকৃত সংখ্যাটো সলনি কৰিব নিবিচাৰিছিলো আৰু সেয়েহে, আমি এটা অস্থায়ী চলক “টেম্প”ৰ সহায় লৈছিলো। চলকটো কীবৰ্ডৰ পৰা পঢ়া মানৰ সৈতে আৰম্ভ কৰা হয় (উদাহৰণ 4.14-ত শাৰী নম্বৰ 7)। তাৰ পিছত মডুলাছ আৰু ডিভিজন অপাৰেচনবোৰ অস্থায়ী চলকত সম্পাদন কৰা হৈছিল (উদাহৰণ 4.14-ত ৰেখা নম্বৰ 10 আৰু 12)।

In the second iteration of the loop, the value of temp is 872. The extracted digit becomes 872= 2. Then temp is divided by 10 to get the new temp as 872 / 10 = 87.

প্ৰ’গ্ৰামটোত, আমি কীবোৰ্ডৰ পৰা ইনপুট হিচাপে লোৱা প্ৰকৃত সংখ্যাটো সলনি কৰিব নিবিচাৰিছিলো আৰু সেয়েহে, আমি এটা অস্থায়ী চলক “টেম্প”ৰ সহায় লৈছিলো। চলকটো কীবৰ্ডৰ পৰা পঢ়া মানৰ সৈতে আৰম্ভ কৰা হয় (উদাহৰণ 4.14-ত শাৰী নম্বৰ 7)। তাৰ পিছত মডুলাছ আৰু ডিভিজন অপাৰেচনবোৰ অস্থায়ী চলকত সম্পাদন কৰা হৈছিল (উদাহৰণ 4.14-ত ৰেখা নম্বৰ 10 আৰু 12)।

As the current temp value is greater than 0, the loop will enter its third iteration. In this iteration, the digit is extracted as 87% 10 = 7 and the new temp becomes 87 / 10 = 8.

যিহেতু বৰ্তমানৰ টেম্প মান 0-তকৈ অধিক, লুপটোৱে ইয়াৰ তৃতীয় পুনৰাবৃত্তি প্ৰবিষ্ট কৰিব। এই পুনৰাবৃত্তিত, অংকটো 87% 10 = 7 হিচাপে আহৰণ কৰা হয় আৰু নতুন টেম্প 87 / 10 = 8 হয়।

Temp is still greater than 0 and hence the loop enters into its fourth iteration. Now, the digit is extracted as 8% 10 = 8 and the new temp is 8/10 = 0.

টেম্প এতিয়াও ০ তকৈ ডাঙৰ আৰু সেয়েহে লুপটো ইয়াৰ চতুৰ্থ পুনৰাবৃত্তিত প্ৰৱেশ কৰে। এতিয়া, অংকটো 8% 10 = 8 হিচাপে আহৰণ কৰা হয় আৰু নতুন টেম্প টো 8/10 = 0 হয়।

As the value of the temp becomes zero, the condition of the while loop (line number 8) is not true and thus the control will come out of the loop and it prints a new line (line number 14) and the program terminates.

টেম্পৰ মূল্য শূন্য হোৱাৰ লগে লগে, লুপ (শাৰী নম্বৰ 8) ৰ অৱস্থা সঁচা নহয় আৰু এনেদৰে নিয়ন্ত্ৰণ লুপৰ পৰা ওলাই আহিব আৰু ই এটা নতুন শাৰী (লাইন নম্বৰ 14) প্ৰিণ্ট কৰে আৰু প্ৰ’গ্ৰামটো সমাপ্ত হয়।

You may compare this program with that of Example B.4 from class IX book. This will help you to understand the importance of loop. In Example B.4, we restricted the length of the number to be of 5 digits and we had to use seperate variables to store the digits. But the current program uses a while loop and uses a single variable “digit”. There is no restriction on the length of the number and the program works for any integer.

আপুনি এই প্ৰ’গ্ৰামটো নৱম শ্ৰেণীৰ কিতাপখনৰ উদাহৰণ বি.4-ৰ সৈতে তুলনা কৰিব পাৰে। ই আপোনাক লুপৰ গুৰুত্ব বুজাত সহায় কৰিব। উদাহৰণ বি.4-ত, আমি সংখ্যাটোৰ দৈৰ্ঘ্য 5 টা অংকৰ হ’বলৈ সীমিত কৰিছিলো আৰু আমি অংকবোৰ সংৰক্ষণ কৰিবলৈ পৃথক চলক ব্যৱহাৰ কৰিব লগা হৈছিল। কিন্তু বৰ্তমানপ্ৰ’গ্ৰামটোৱে অলপ সময়ৰ লুপ ব্যৱহাৰ কৰে আৰু এটা চলক “অংক” ব্যৱহাৰ কৰে। সংখ্যাটোৰ দৈৰ্ঘ্যৰ ওপৰত কোনো বাধা নাই আৰু প্ৰ’গ্ৰামটোৱে যিকোনো ইণ্টেগাৰৰ বাবে কাম কৰে।

E. A C program to find the summation of digits of a given integer.

E. এটা প্ৰদত্ত ইণ্টেগাৰৰ অংকৰ সাৰাংশ বিচাৰিবলৈ এটা চি প্ৰগ্ৰাম।

For example, if the integer is 8724, the output will be 21.

উদাহৰণ স্বৰূপে, যদি ইণ্টেগাৰ8724 হয়, আউটপুট হ’ব 21।

21 is formed by adding the digits: 8+7+2+4.

21 অংকবোৰ যোগ কৰি গঠন কৰা হয়: 8+7+2+4।

In the previous example, we have already extracted the individual digits of an integer. Now, we need to find the summation of these digits. As soon as a digit is extracted from the number, we will add this digit to a previously calculated summation. We will repeatedly perform this operation. The logic is similar to the one we used while finding the summation of a series of numbers.

আগৰ উদাহৰণত, আমি ইতিমধ্যে এটা ইণ্টেগাৰৰ গাইগুটীয়া অংক আহৰণ কৰিছোঁ। এতিয়া, আমি এই অংকবোৰৰ সাৰাংশ বিচাৰি উলিয়াব লাগিব। সংখ্যাটোৰ পৰা এটা অংক আহৰণ কৰাৰ লগে লগে, আমি এই অংকটো আগতে গণনা কৰা সংক্ষিপ্তকৰণএটাত যোগ দিম। আমি বাৰে বাৰে এই অপাৰেচনটো কৰিম। সংখ্যাৰ এটা শৃংখলাৰ সংক্ষিপ্তবিৱৰণ বিচাৰি উলিওৱাৰ সময়ত আমি ব্যৱহাৰ কৰা যুক্তিৰ দৰেই যুক্তিটো।

Example 4.15 shows the program. In this program, we brought the following modifications to the program of Example 4.14.

উদাহৰণ 4.15-এ প্ৰ’গ্ৰামটো দেখুৱায়। এই কাৰ্যসূচীত, আমি উদাহৰণ 4.14-ৰ কাৰ্যসূচীত নিম্নলিখিত সংশোধনবোৰ আনিছিলো।

1. Declared a variable “sum” and initialized it to zero (line number 4)

এটা পৰিৱৰ্তনশীল “যোগফল” ঘোষণা কৰিলে আৰু ইয়াক শূন্যলৈ আৰম্ভ কৰিলে (শাৰী নম্বৰ 4)

2. Added the extracted digit to the previous summation and updated the new summation (line number 12

নিষ্কাশিত অংকটো পূৰ্বৱৰ্তী সংক্ষিপ্তকৰণত যোগ দিয়ক আৰু নতুন সংক্ষিপ্তকৰণ আপডেট কৰক (শাৰী নম্বৰ 12)

3. Finally, we displayed the summation using a printf() statement (line number 15).

অৱশেষত, আমি এটা প্ৰিণ্টফ() বিবৃতি (শাৰী নম্বৰ 15) ব্যৱহাৰ কৰি সংক্ষিপ্তবিৱৰণ প্ৰদৰ্শন কৰিলোঁ।

F. A C program to check whether a number is prime or not.

F. এটা সংখ্যা প্ৰাইম হয় নে নহয় পৰীক্ষা কৰিবলৈ এটা চি প্ৰ’গ্ৰাম।

We know that a number is prime when it is not divisible by any number other than I or itself. We will use this very logic in the program.

আমি জানো যে এটা সংখ্যা প্ৰাইম হয় যেতিয়া ই মোৰ বা নিজৰ বাহিৰে আন কোনো সংখ্যাৰ দ্বাৰা বিভাজ্য নহয়। আমি কাৰ্যসূচীটোত এই যুক্তিটো ব্যৱহাৰ কৰিম।

Refer to the program in Example 4.16. We first take the number as input from the keyboard. Then we run a loop from i=0 to number/2 to check the divisibility of the number. It is sufficient to run the loop till number/2. We check the divisibility of the number using a modulus (%) operation and double equal to (==) operation.

উদাহৰণ 4.16-ত প্ৰ’গ্ৰামটো চাওক। আমি প্ৰথমে নম্বৰটো কীবৰ্ডৰ পৰা ইনপুট হিচাপে লওঁ। তাৰ পিছত আমি নম্বৰৰ বিভাজ্যতা পৰীক্ষা কৰিবলৈ আই=0 ৰ পৰা নম্বৰ/2 লৈ এটা লুপ চলাওঁ। নম্বৰ/2 লৈকে লুপটো চলাবলৈ ই পৰ্যাপ্ত। আমি মডুলাছ (%) অপাৰেচন ব্যৱহাৰ কৰি নম্বৰৰ বিভাজ্যতা পৰীক্ষা কৰোঁ আৰু (==) অপাৰেচনৰ সমান দুগুণ।

Example 4.16: A C program to check whether a number is prime or not.

উদাহৰণ 4.16: এটা সংখ্যা প্ৰাইম হয় নে নহয় পৰীক্ষা কৰিবলৈ এটা চি প্ৰ’গ্ৰাম।

As soon as the number becomes divisible by another number (line number 9), we set a flag (line number 11) and come out of the loop using a “break” statement (line number 12). As we come out of the loop, we check whether the flag is set or not.

সংখ্যাটো আন এটা সংখ্যাৰ দ্বাৰা বিভাজ্য হোৱাৰ লগে লগে (শাৰী নম্বৰ 9), আমি এখন পতাকা (ৰেখা নম্বৰ 11) ছেট কৰোঁ আৰু “ব্ৰেক” বিবৃতি (শাৰী নম্বৰ 12) ব্যৱহাৰ কৰি লুপৰ পৰা ওলাই আহো। আমি লুপৰ পৰা ওলাই অহাৰ লগে লগে, আমি পতাকাখন ছেট কৰা হৈছে নে নাই পৰীক্ষা কৰোঁ।

If the flag is not set and the number is not 1, then we announce the number to be a prime number. Otherwise the number is not a prime number. We use printf() statements for the announcement.

যদি ফ্লেগটো ছেট কৰা নহয় আৰু নম্বৰটো 1 নহয়, তেন্তে আমি নম্বৰটো এটা প্ৰাইম নম্বৰ হিচাপে ঘোষণা কৰোঁ। অন্যথা সংখ্যাটো এটা ম’ইম নম্বৰ নহয়। আমি ঘোষণাটোৰ বাবে প্ৰিণ্টফ() বিবৃতি ব্যৱহাৰ কৰোঁ।

In the next chapter, we will write one loop inside another loop to tackle some more interesting problems.

পৰৱৰ্তী অধ্যায়ত, আমি আৰু কিছুমান আকৰ্ষণীয় সমস্যাৰ মোকাবিলা কৰিবলৈ আন এটা লুপৰ ভিতৰত এটা লুপ লিখিম।