Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    C Programming මුල සිට ඉගෙනගනිමු (part 9 - While loop)

    Blogs
    c programming
    3
    3
    437
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Kalana
      Kalana last edited by Kalana

      සුබ දවසක් වේවා ඔයාලට. අද මන් කතා කිරීමට බලාපොරොත්තු වෙන්නේ loops ගැන. මොනවද මේ loops නැතහොත් Iterative statements කියලා කියන්නේ. මන් ඔයාලට කියනවා එකේ ඉදන් දහයට ඇති සියලුම සංඛ්‍යා diplay වෙන විදිහට program එකක් ලියන්න කියලා. අපි දන්නවා අපිට පුලුවන් මේ වැඩේ printf() දහයකින් මේ වැඩේ කරන්න පුලුවන් කියලා. නමුත් ඔයාට කිව්වොත් එකේ ඉදන් සීයට ඇති සියලුම සංඛ්‍යා diplay වෙන විදිහට program එකක් ලියන්න කියලා. මෙය printf() සීයකින් කරන්න ගියොත් ගොඩක් වෙලා යන බව ඔබට තේරෙනෙවා ඇති. එම නිසා එවැනි කාර්‍යයන් සිදු කිරීම සදහා
      ක්‍රම තුනක් C Programming වල අප භාවිතා කරනවා.

      1. while loop.
      2. do-while loop.
      3. for loop.

      මෙම වර්ග තුනෙන් අද මම පැහැදිලි කිරීමට බලාපොරොත්තු වන්නේ while loop ගැන පමණි. පලමුව මම මෙහි ආකෘතිය ලියා පෙන්වන්නම්.

      while(condition)
      {
          //ඔබට නැවත නැවත run කිරීමට අවශ්‍ය code එක හෝ codes කිහිපය
          //Increment(++) or Decrement(--) operator
      }
      

      මේ ආකෘතිය භාවිතා කරන උදාහරණයක් දැන් බලමු. මන් කලින් කිව්ව එකේ සිට දහයට ඇති සංඛ්‍යා while loop මඟින් diplay කරමු.

      #include <stdio.h>
      
      int main()
      {
          int i = 1;
          
          while(i <= 10)
          {
              printf("%d ", i);
              i++;
          }
      
          return 0;
      }
      
      Output -: 1 2 3 4 5 6 7 8 9 10
      

      අපි දැන් මේ codes කිහිපය පැහැදිලි කර ගනිමු.

      1. int i = 1; -: අපිට ලැබුනු අභියෝගය උනේ එකේ සිට දහයට වෙනතෙක් සංඛ්‍යා diplay කිරීමටයි. එම නිසා අපගේ ආරම්භක ඉලක්කම මම int i වලට සමාන කලා.
      2. while(i <= 10) -: මෙහිදී උබට පෙනෙනවා while loop එක තුල කිසියම් condition එකක් ඇති බව. එම condition එක සත්‍ය වුවහොත් පමණයි අපට loop එක ඇතුලේ තියෙන codes run කිරීමට හැකි වන්නේ. ඉහතදී අප i වල අගය එකට සමාන කලා එමනිසා while(i <= 10) දී එක දහයට වඩා කුඩා බව සත්‍ය නිසා loop එක ඇතුලේ තියෙන codes run කිරීමට අපට හැකියාව ලැබෙනවා.
      3. printf("%d ", i); -: ඉහත දැක්වූ condition සත්‍ය වූ නිසා මෙය run වේ. මෙහිදී i වල වර්තමාන අගය diplay කරයි. මෙම අවස්තාව වන විට i වල වර්තමාන අගය එක වන නිසා එක diplay වේ.
      4. i++; -: මෙහි අරුත වන්නේ i = i + 1 යන්නයි. i වල කලින් අගය එක වේ. එයට තව එකක් එකතු වූ විට දෙක වේ. දැන් i වල නවතම අගය 2 වේ. මෙය නැවතත් while(i <= 10) වලට ගමන් කර සත්‍ය දැයි පරීක්ෂා කරයි දෙක දහයට වඩා කුඩා නිසා condition එක සත්‍ය වී නැවත loop එක ක්‍රියාත්මක වේ. මෙය i = 11 වූ විට නතර වේ. මක් නිසාදයත් එකලොහ දයට වඩා විශාල වන නිසා condition එක අසත්‍ය වන බැවිනි.

      ඉහත සදහන් කල දේවල් මම while loop එකට ඉලක්කම් යොදමින් පැහැදිලි කරන්නම්.

      1. පලමු වටය
      int i = 1;
          
          while(i <= 10) // 1<=10
          {
              printf("%d ", i); //output -: 1
              i++; // i = 1 + 1;
      		     // i = 2
          }
      
      1. දෙවන වටය(i වල නවතම අගය 2 වේ)
          while(i <= 10) // 2<=10
          {
              printf("%d ", i); //output -: 1 2
              i++; // i = 2 + 1;
      		     // i = 3
          }
      

      3.තුන්වන වටය(i වල නවතම අගය 3 වේ)

          while(i <= 10) // 3<=10
          {
              printf("%d ", i); //output -: 1 2 3
              i++; // i = 3 + 1;
      		     // i = 4
          }
      
      1. මෙලෙස වට සියල්ල ගමන් කර අවසන් වටයට කලින් වටය වන 10 වටය(මෙහිදී i වල නවතම අගය 10 වේ)
          while(i <= 10) // 10<=10
          {
              printf("%d ", i); //output -: 1 2 3 4 5 6 7 8 9 10
              i++; // i = 10 + 1;
      		     // i = 11
          }
      
      1. අවසන් වටය හෙවත් එකලොස්වන වටය
      while(i <= 10) // 11<=10 මෙය අසත්‍ය වන නිසා loop එකෙන් එලියට ගොස් එලියේ ඇති codes run වේ.
      {
          //condition එක අසත්‍ය වූ නිසා ඇතුලේ ඇති දේවල් run නොවේ.
      }
      
      return 0; //loop එකෙන් එලියේ ඇත්තේ මෙය පමණක් වන නිසා මෙය run වේ.
      

      මම හිතනවා ඔබට දැන් තේරෙන්න ඇති කියලා while loop එකක ක්‍රියාකාරිත්වය මොකක්ද කියලා.

      අපි කලින් කතා කලා එකේ සිට දහයට සංඛ්‍යා diplay කරන්නේ කොහොමද කියලා.අපි දැන් බලමු දහයේ සිට එක දක්වා සංඛ්‍යා diplay කරන ආකාරය.

      #include <stdio.h>
      
      int main()
      {
          int i = 10; // දහයේ සිට එකට නිසා ආරම්භක අගය 10 ලෙස ගැනීම
          
          while(i >= 1) // දහය එකට වඩා විශාල නිසා condition එක සත්‍ය වේ. මෙහිදී අප එක ගත්තේ මෙම සංඛ්‍යා රටාව අප අවසන් කල යුත්තේ එකෙන් වන බැවිනි.
          {
              printf("%d ", i);// වර්තමාන අගය වන දහය diplay කිරීම
              i--; // i = i - 1 එනම් i = 10 - 1;
      		     // i = 9;
          }
      
          return 0;
      }
      
      Output -: 10 9 8 7 6 5 4 3 2 1
      

      අපි දැන් මෙම while loop යොදා ගනිමින් සරල ගැටලුවක් විසදමු.

      1. write a C program to print 1 to 100 even numbers by using while loop.

      මෙමගින් කියන්නේ එකේ සිට සීයට දක්වා ඇති සියලු ඉරට්ටේ සංඛ්‍යා while loop එකක් මගින් diplay කරන ලෙසයි. මම එය මෙසේ පියවර ආකාරයෙන් කරන්නම්.

      • එකේ සිට සීය දක්වා සංඛ්‍යා while loop එකක් මගින් diplay කරන විදිහ ඔයාලට මන් කියලා දුන්නා.
      • අපි ඇතුලත් කරන සංඛ්‍යාවක් ඉරට්ටේ වන්නේ කෙසේදැයි බලන ආකාරය කලින් ලිපි වල මන් ඔයාලට කියලා දුන්නා, ඒ උනාට මන් ආයේ ඒක කියන්නම්. යම්කිසි සංඛ්‍යාවක් දෙකෙන් ඉතුරු නැතිව බෙදේ නම්එය ඉරට්ටේ සංඛ්‍යාවක් වේ. එනම් if(number % 2 == 0) නම් එම සංඛ්‍යාව ඉරට්ටේ සංඛ්‍යාවකි.
      #include <stdio.h>
      
      int main()
      {
          int number = 1; //අපගේ ආරම්භක සංඛ්‍යාව
          
          while(number <= 100) // condition එක ස්ත්‍ය නම් අප අවසන් කල යුතු සංඛ්‍යාව තෙක් දිගටම loop එක ක්‍රියාත්මක කිරීම
          {
              if(number % 2 == 0) // අපගේ සංඛ්‍යාව දෙකෙන් ඉතුරු නැතිව බෙදේදැයි පරීක්ෂා කිරීම.
              {
                  printf("%d\n", number); // සංඛ්‍යාව දෙකෙන් බෙදේ නම් එම සංඛ්‍යාව diplay කිරීම
              }
              number++; //සංඛ්‍යාවට එකක් එකතු කිරීම.
          }
      
          return 0;
      }
      

      මෙම ගැටලුව while loop දෙකකින් හෙවත් nested while loop වලින්ද කල හැකිය

      #include <stdio.h>
      
      int main()
      {
          int number = 1; //අපගේ ආරම්භක සංඛ්‍යාව
          
          while(number <= 100)// condition එක ස්ත්‍ය නම් අප අවසන් කල යුතු සංඛ්‍යාව තෙක් දිගටම loop එක ක්‍රියාත්මක කිරීම
          {
              while(number % 2 == 0)// ලැබෙන සංඛ්‍යාව දෙකෙන් බෙදේනම් මෙම loop එක ක්‍රියාත්මක කිරීම.
              {
                  printf("%d\n", number);// සංඛ්‍යාව දෙකෙන් බෙදේ නම් එම සංඛ්‍යාව diplay කිරීම
                  number++;//සංඛ්‍යාවට එකක් එකතු කිරීම. එවිට එම සංඛ්‍යාව ඔත්තේ සංඛ්‍යාවක් වේ එම නිසා මෙම  loop එක අසත්‍ය වන බැවින් මෙම loop එකෙන් පිටත codes run වේ.
              }
              number++;//සංඛ්‍යාවට එකක් එකතු කිරීම. එවිට මෙය ඉරට්ටේ සංඛ්‍යාවක් වේ. තවද මෙය තවදුරටත් 100ට වඩා කුඩානම් දෙවන loop එක නැවත සත්‍ය වී එම loop එක ක්‍රියා කරයි.
          }
      
          return 0;
      }
      

      Both will give same output -:

      2
      4
      6
      8
      10
      12
      14
      16
      18
      20
      22
      24
      26
      28
      30
      32
      34
      36
      38
      40
      42
      44
      46
      48
      50
      52
      54
      56
      58
      60
      62
      64
      66
      68
      70
      72
      74
      76
      78
      80
      82
      84
      86
      88
      90
      92
      94
      96
      98
      100
      

      මන් දැන් කලින් එකට වඩා සංකීර්ණ ප්‍රශ්නයක් දෙන්නම්.
      Write a C programm to input marks of three subjects from keyboard and output the total mark and average mark using while loop.

      • මෙයින් කියන්නේ විෂයන් තුනක ලකුණු ඇතුලත් කර මුළු ලකුනු සහ ලකුණු වල සාමාන්‍යය while loop එකක් මගින් diplay කරන ලෙසයි.
      • අප මෙහිදී විෂයයන් තුන subject1, subject2, subject3 ලෙස ගනිමු. මෙහිදී මුලු ලකුනු ලබා ගැනීමටනම් ලකුනින් ලකුන එකතු කිරීමට සිදුවනවා. එමනිසා එම එකතු කිරීම loop එකක් තුල සිදු කල යුතු වනවා.
      • මුළු ලකුණු සහ සාමාන්‍යය ලබාගන්නේ එක්වරක් පමණක් නිසා එය loop එකට පිටින් සිදු කල යුතු බව ඔබට පෙනෙනවා ඇති.
      #include <stdio.h>
      
      int main()
      {
          float mark;
          float total = 0; //මෙය අප keyboard එක මගින් input කරන දත්තයක් නොවන නිසා 0ට සමාන කරයි.
          float average = 0;//මෙය අප keyboard එක මගින් input කරන දත්තයක් නොවන නිසා 0ට සමාන කරයි.
          int i = 1;
          
          while(i <= 3)
          {
              printf("Enter mark of subject%d = ", i);//මෙහිදී i වල වර්තමාන අගය subject එක ඉදිරියෙන් diplay වේ.(උදා -: subject1, subject2..)
              scanf("%f", &mark); //විශයට අදාල ලකුණු මෙමගින් input කිරීම සිදු කරයි.
              
              total = total + mark; // total වල වර්තමාන අගය 0 බැවින් අප ඇතුලත් කල mark වල අගය එම 0ට එකතුවී total වලට නව අගයක් ලැබේ. විශයන් 3ම නිරූපණය වන පරිදි මෙය තුන්වරක් එකතු වේ.
              i++; // පලමු විශයට 1ක් එකතු කර දෙවන විශයට යාම.
          }
          
          average = total / 3; // විශයන් තුනම එකතු වූ පසු එය මෙතනට totalලෙස පැමිණ සම්පූර්ණ විශයන් ගණන වූ 3 මගින් බෙදීමෙන් average එක ලබා ගනී.
      
          printf("\nTotal is   = %0.2f", total); //මුලු ලකුනු diplay කරයි
          printf("\nAverage is = %0.2f", average);// ලකුනු වල සාමාන්‍යය diplay කරයි
          return 0;
      }
      

      Output -:

      Enter mark of subject1 = 30
      Enter mark of subject2 = 30
      Enter mark of subject3 = 60
      
      Total is   = 120.00
      Average is = 40.00
      

      මන් ඔයාලට ප්‍රශ්න කිහිපයක් දෙන්නම් උත්තරත් එක්ක උත්සහ කරලා බලන්න කරන්න පුලුවන්ද කියලා. බැරි උනොත් හරි තේරෙන් නැත්තම් හරි මට message එකක් දාන්න.

      Question 1 -: Write a program that sums a sequence of integers. Assume that the first integer read the number of values remaining to be entered. Your program should read only one value each time scanf is executed.

      Answer -: https://drive.google.com/open?id=1wobH_o7GhW2p2_F51MrQRNPGqd4yahu1


      Question 2 -: Write a program that finds the smallest of several integers. Assume that the first value read specifies the number of values remaining

      Answer -: https://drive.google.com/open?id=12CwgubdYTySRXkkoveYlArUZxaXlFhKd


      Question 3 -: Collecting money becomes increasingly difficult during periods of recession, so companies may tighten their credit limits to prevent their accounts receivable (money owed to them) from becoming too large. In response to pronologed recession, one company has cut its customers’
      credit limits in half. Thus, if a particular customer had a credit limit of $2000, it is now $1000.

      Write a program that analyzes the credit status of three customers of this company. For each customer you are given:

      • The customer’s account number
      • The customer’s credit limit before the recession
      • The customer’s current balance

      Your program should calculate and print the new credit limit for each customer and should determine (and print) which customers have current balances that exceed their new credit limits.

      Answer -: https://drive.google.com/open?id=19I4YBXAoIRad7ffQESDTzfjNJX-OcZLP


      Question 4 -: The simple interest on a loan is calculated by the formula
      Interest = principal * rate * days / 365 ;

      The proceeding formula assumes that rate is the annual interest rate, and therefore includes the division by 365 (days). Develop a program that will input principle, rate and days for several loans, and calculate and display the simple interest for each loan, using the preceding formula.
      Here is a simple input/output dialog:

      Enter loan principal (-1 to end) : 1000.00
      Enter interest rate : 0.1
      Enter term of the loan in days : 365
      The interest charge is 100.00
      
      Enter loan principal (-1 to end) : 1000.00
      Enter interest rate : 0.08375
      Enter term of the loan in days : 224
      The interest charge is 1.40
      
      Enter loan principal (-1 to end) : -1
      

      Answer -: https://drive.google.com/open?id=1qEIpcjMSb14EsLEr4VC18Mz2I44KppOZ


      • C programm එකක් windows වල run කරන විදිහ --> https://bit.ly/2O6rLXR

      ඔබට අවශ්‍යනම් ඔබේ බ්‍රව්සර් එක හරහා online C programms run කරන්න පුලුවන්. එහෙම කරන්න පුලුවන් ලින්ක්ස් මන් පහතින් දාන්නම්

      • https://www.onlinegdb.com/online_c_compiler
      • https://www.tutorialspoint.com/compile_c_online.php
      • https://repl.it/languages/c

      සරලව මුල ඉදන් C programming ඉගෙන ගන්න පුලුවන් හොදම site කිහිපයක් මන් දාන්නම්

      • https://www.geeksforgeeks.org/c-programming-language/
      • https://www.tutorialspoint.com/cprogramming/index.htm
      • https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/

      web url කෙටි කරන්න lankadevelopers ලා හදපු එයාලගෙම සයිට් එකක් තියෙනවා. -> https://link.lankadevelopers.com/

      මගේ කලින් ලිපි

      • C Programming මුල සිට ඉගෙනගනිමු(part 1 - Introduction) -: https://link.lankadevelopers.com/4WpH
      • C Programming මුල සිට ඉගෙනගනිමු (part 2 - Variables) -: https://link.lankadevelopers.com/mXio
      • C Programming මුල සිට ඉගෙනගනිමු (part 3 - Operators) -: https://link.lankadevelopers.com/SHNt
      • C Programming මුල සිට ඉගෙනගනිමු (part 4 - Input & Output functions) -: https://link.lankadevelopers.com/2MNku
      • C Programming මුල සිට ඉගෙනගනිමු (part 5 - create simple applications) -: https://link.lankadevelopers.com/KUF6
      • C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 1)) -: https://link.lankadevelopers.com/8Xe71
      • C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) -: https://link.lankadevelopers.com/761PT
      • C Programming මුල සිට ඉගෙනගනිමු (part 8 - Switch Statement) -: https://link.lankadevelopers.com/7jncK

      මන් ඊළග ලිපියෙන් කියලා දෙන්නම් do-while loop එක ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.

      තව ලිපියකින් හම්බෙමු ජය වේවා.

      1 Reply Last reply Reply Quote 2
      • root
        root Linux Help last edited by

        superb bro. keep it up.

        1 Reply Last reply Reply Quote 1
        • dev_lak
          dev_lak last edited by

          thanks bro superb

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post

          1
          Online

          3.7k
          Users

          1.3k
          Topics

          5.3k
          Posts

          • Privacy
          • Terms & Conditions
          • Donate

          © Copyrights and All right reserved Lanka Developers Community

          Powered by Axis Technologies (PVT) Ltd

          Made with in Sri Lanka

          | |