Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop
    1. Home
    2. Kalana
    3. Posts
    • Profile
    • Following 8
    • Followers 5
    • Topics 17
    • Posts 53
    • Best 29
    • Controversial 0
    • Groups 0

    Posts made by Kalana

    • RE: php form එකක තියෙන variable එකක් තව multiple pages වලට යවන්නේ(පෙන්නන්නේ) කොහොමද php වලින්.

      @Kataz ප්‍රශ්නය තව ටිකක් පැහැදිලි කරානම් හරි. මට තේරුනෙත් ඔයා කිව්ව දේ වැරදියට

      posted in General Discussion
      Kalana
      Kalana
    • RE: php form එකක තියෙන variable එකක් තව multiple pages වලට යවන්නේ(පෙන්නන්නේ) කොහොමද php වලින්.

      ප්‍රශ්නයේ හැටියට ඔයා PHP වලට අලුත් කෙනෙක් වගේ පේන්නේ. මේකට ඔයා ට්‍රයි කරපු code එකත් දැම්මනම් වැඩේ ලේසියි. මන් මේකට මෙහෙම උදව්වක් කරන්නම්. ඔයා input කරන විස්තර ඒ ඒ අදාල variable එකේ store වෙනවා. ඊට් පස්සේ ඔයාට තියෙන්නේ ඒ store කරගත්තු variable ටික page_one.php එකටයි page_two.php එකටයි call කරන එක.

      ඔයා මෙහෙම කරන්න. Google එකට ගිහින් crud operations in php කියලා සර්ච් කරන්න. නැත්තම් https://www.tutorialrepublic.com/php-tutorial/php-mysql-crud-application.php ඔය ලින්ක් එකට ගිහින් බලන්න. එතකොට ඔයාට idea එකක් එයි. ඒකෙත් ඔයාගේ ප්‍රශ්නයට අදාල වෙන කොටස තියෙන්නේ Creating the Read Page කියන කොටසේ.

      posted in General Discussion
      Kalana
      Kalana
    • RE: ශ්‍රී ලංකාවට Google Crowdsource!

      Thanks for sharing this brother

      posted in Blogs
      Kalana
      Kalana
    • RE: ලොවම මවිත කරන අලුත්ම malware එක XHelper

      @GeethOnion අලුතින් OS ගහන්න වෙන සීන් එකක් තියෙන්නේ

      posted in Blogs
      Kalana
      Kalana
    • RE: මොනවද මේ RANSOMWARE?

      මගේ PC එකටත් ransomware එකක් ආවා. ඩොලර් 1000ක් ඉල්ලුවා ෆයිල් ටික decrypt කරන password එක දෙන්න. මන් මේකට solution එකක් හොයා ගත්තා. ransomware එකේ වර්ගේ අනූව මේ solution එක කොහොම වැඩ කරයිද දන්නෑ.

      • මුලින්ම කරන්න ඕනා ransomware එකෙන් infected උන file සේරම delete කරන එක.
      • ඊට පස්සේ PC එක format කරන්න ඕනා.
      • ඊට පස්සේ media recovery කරන්න පුලුවන් software එකක් install කරලා අර delete කරපු file ටික recover කරන එක. මේකෙන් අර infection නැතුව ගිහින් original file එක ගන්න පුලුවන්. මගේ ෆොටෝ වලින් 60% විතර මන් ඒ විදිහට recover කරගත්තා. 100% recover කරගන්න තිබ්බා infection එක උන කාලෙම ඒ දේ කලානම්.
      posted in Blogs
      Kalana
      Kalana
    • RE: Gmail වල important emails හදුනාගන්නේ මෙහෙමයි

      important emails විතරක් තියාගෙන අනෙක් emails සේරම delete කරන විදිහක් තියෙනවා. අපිට අවශ්‍ය කරන සියලුම emails, important ටැග් එකත් එක්ක එනවා අනිත් ඒවා inbox එකේ තියෙනවා. emails ගොඩගැහිලා තියෙන කෙනෙක්ට important ඒව ඇරෙන්න ඉතුරු සේරම mails delete කරන්න ඕනනම් මේ දේවල් කරන්න පුලුවන්.

      • මුලින්ම gmail එකට ගිහින් එකේ search bar එකට යන්න.
      • ඊට පස්සේ search bar එකේ label:unimportant ටයිප් කරලා search කරන්න. මේකෙන් වෙන්නේ සේරම important mails බේරිලා අනික් mails විතරක් select වෙන එක. ඊට පස්සේ ඔයාට බය නැතුව mails delete කරන්න පුලුවන්
      posted in Blogs
      Kalana
      Kalana
    • RE: ලොවම මවිත කරන අලුත්ම malware එක XHelper

      ස්තූතියි සහෝදරයා, xhelper ගැන අහලා තිබ්බට ඒකේ භයානකකම දැන ගත්තේ අද. මේකට තාම solution එකක් හොයාගෙන නැද්ද තාම.

      posted in Blogs
      Kalana
      Kalana
    • RE: C Programming මුල සිට ඉගෙනගනිමු (part 12 - arrays)

      @root Programming වලට අලුත් ඕන කෙනෙක්ට C language ගැන අවශ්‍ය මූලික දැනුම සේරම මන් මේ ලිපි වලින් දාන්න ඉන්නේ. මේ මගේ පලවෙනි අත්දැකීම මේ වගේ දෙයක් කරලා ගත්තු. ඉස්සරහට මන් data mining අනිත් languages වගේ දේවලුත් දාන්නම්. මේකම මන් තෝරගත්තේ ගොඩක් campus වල startup එක විදිහට පටන් ගන්නේ C හරි C++ හරි දෙකම හෝ දෙකෙන් එකක් තමා. මේ සයිට් එක කලින් මන් දැනන් හිටියනම් මීට වඩා ලොකු දෙයක් කරන්න තිබ්බා. මන් දැන් research එක කටේ ඉන්නේ. ඒකයි පොඩි අවුලකට තියෙන්නේ.

      posted in Blogs
      Kalana
      Kalana
    • C Programming මුල සිට ඉගෙනගනිමු (part 12 - arrays)

      සුබ දවසක් වේවා ඔයාලට. අද මන් මේ ලිපිය මඟින් කතා කිරීමට බලාපොරොත්තු වන්නේ C Programming වල එන arrays ගැනයි. මොනාද මේ array එකක් කියන්නේ. මුල් ලිපි වල මන් variables ගැන කතා කලා. array එකක් එක්ක variable එකක් සංසන්දනය කරන විට variable එකක් කියන්නේ හරියට කාමරයක් වගේ. මොකද කිව්වොත් එයාට එක සැරේකට තියාගන්න පුලුවන් එක අගයක් විතරයි.

      variable එකක් එක්ක බලද්දි array එකක් කියලා කියන්නේ කාමර විශාල සංඛ්‍යාවක් ඇති ගොඩනැගිල්ලක් වගේ. අපිට තීරණය කරන්න පුලුවන් එම ගොඩනැගිල්ලට අවශ්‍ය කාමර සංඛ්‍යාව කොපමණක්ද කියලා. ඒ කියන්නේ එකවර අගයන් විශාල සංඛ්‍යාවක් array එකකට store කරගන්න පුලුවන් කියන එකයි.

      උදාහරණයක් ගත්තොත් මන් ඔබට කියනවා සිසුන් 100කගේ ලකුණු ඇතුලත් කර ඔවුන්ගේ ලකුණු පිලිවෙලට නැවත output කරන ලෙස. හැබැයි එය විය යුත්තේ සිසුන් 100 දෙනාගෙම ලකුණු ඇතුලත් කිරීමෙන් අනතුරුවයි.

      ඉහතදී ඔබට පෙනෙනවා ඇති අපි loop එකක් පාවිච්චි කලත් ඉහත කාරණය සපුරාලිය නොහැකි බව. variable 100ක් අරන් මේ වැඩේ කිරීමට පුලුවන් උනත් එය ඉතා දුශ්කර කටයුත්තක් බව ඔබට පෙනේ. මේ වගේ දුශ්කර අවස්තා වලදී තමයි අපිට arrays උදව්වට එන්නේ.

      අපි Program එකක් ලියන්න කලින් arrays වල එන වැදගත් කරුණු දැන ගනිමු.

      • array එකක data type වර්ග variable data type වර්ග වලට සමාන වේ. (int, float, char, short,...,etc).
      • array එකක මූලික ආකෘතිය වන්නේ <data type> <array name>[] (උදා -: int number[], float marks[], char words[],....,etc).
      • array එකක් භාවිතා කරන සම්මත ආකාර වන්නේ int numbers[5] = {1, 2, 3, 4 ,5}; හෝ int numbers[] = {1, 2, 3, 4, 5}; අකාරවලින් වේ.
      • ඔබ array එකක් int numbers[5] ලෙස භාවිතා කලොත් එයින් අදහස් වන්නේ එය තුල 0, 1, 2, 3, 4, 5 යන සංඛ්‍යා ඇති බවයි. එනම් 0 සිට 5 දක්වා කාමර ඇති බවයි. එය කාමර වලට කැඩුවොත් පහත පරිදි අර්ත දැක්විය හැකිය.
      int numbers[5] = {1, 2, 3, 4 ,5};
      //ඉහත 1 සිට 5 දක්වා සංඛ්‍යා පහත පරිදි array එකට ආදේශ වේ.
      
      int numbers[0] = 1
      int numbers[1] = 2
      int numbers[2] = 3
      int numbers[3] = 4
      int numbers[4] = 5
      int numbers[5] // අපට array වලදී සෑම විටම භාවිතා කල හැක්කේ numbers[n - 1] දක්වා පමණි. එමනිසා අපට numbers[5] යන්න සංඛ්‍යාවක් ආදේශ කිරීම සදහා භාවිතා කල නොහැක.
      
      #include <stdio.h>
      
      int main()
      {
          int number[5] = {1, 2, 3, 4 ,5};
          
          printf("%d\n", number[0]);
          printf("%d\n", number[1]);
          printf("%d\n", number[2]);
          printf("%d\n", number[3]);
          printf("%d\n", number[4]);
      
          return 0;
      }
      

      Output -:

      1
      2
      3
      4
      5
      

      අපි බලමු දැන් මේකෙදි number[5] භාවිතා කලොත් මොකද වෙන්නේ කියලා.

      #include <stdio.h>
      
      int main()
      {
          int number[5] = {1, 2, 3, 4 ,5, 6}; // 0 සිට 5 දක්වා සංඛ්‍යා 6ක් ඇති බැවින් 6 ඇතුලත් කිරීම.
          
          printf("%d\n", number[0]);
          printf("%d\n", number[1]);
          printf("%d\n", number[2]);
          printf("%d\n", number[3]);
          printf("%d\n", number[4]);
          printf("%d\n", number[5]); // 6 Output කිරීම සදහා
      
          return 0;
      }
      

      Output -:

      1
      2
      3
      4
      5
      32764 // 6 Output නොවේ.
      

      මන් ඉහත කියූ කාරණ සත්‍ය බව ඔබට මෙමගින් පෙනෙනවා ඇති.

      අපි දැන් arrays පාවිච්චි කරලා සරල ගැටලුවක් විසදමු.

      Question -: Write a C program using arrays to fulfill below statements

      1. Enter marks of five students using arrays.
      2. Output marks of all students that you have entered.
      3. Modify your code if user input more than 100 or less than 0 marks user told to input that mark again.

      hint -: you can use loops.

      1. ඉහත ප්‍රශ්න මාලාවලියේ පලවෙනි ප්‍රශ්නය මන් විසඳන්නම්. පලවෙනි ප්‍රශ්නයෙන් කියැවෙන්නේ array එකක් භාවිතා කරමින් සිසුන් 5 දෙනෙකුගේ ලකුණු input කරන ලෙසයි. අපිට hint එකකුත් මේකේ දීලා තියෙනවා loops පාවිච්චි කරන්න කියලා. අපි බලමු එහෙනම් මේ කොටස විසදන හැටි.
      #include <stdio.h>
      
      int main()
      {
          int i;
          int number[i]; // මෙහිදී අප array එකේ විශාලත්වය සදහන් කල නොමැත 
          
          for(i = 0 ; i < 5 ; i++) // array එකක විශාලත්වයේ ආරම්භක අවස්තාව 0 නිසා i = 0 ලෙසත් සිසුන් 5දෙනෙකුගේ ලකුණු ඇතුලත් කරන නිසා i < 5 ලෙස ගනිමු.
          {
              printf("Enter Student mark = ");
              scanf("%d", &number[i]); //වටයෙන් වටයට i++ මගින් i වල අගය එකකින් වැඩි කරයි. ඒ නිසා තමයි අපිට 5දෙනෙකුගේ ලකුණු ඇතුලත් කිරීමට හැකි වන්නේ.
          }
      
          return 0;
      }
      

      Output -:

      Enter Student mark = 12
      Enter Student mark = 23
      Enter Student mark = 45
      Enter Student mark = 56
      Enter Student mark = 67
      
      1. පලවෙනි ප්‍රශ්නය අපි ඉහතදී විසදුවා. දැන් අප දෙවන ප්‍රශ්නය බලමු. අපිට දැන් සිසුන් 5 දෙනාගේ ලකුණු ඇතුලත් කල පිලිවෙලට නැවත Output කල යුතු බවයි දෙවන ප්‍රශ්නයෙන් කියැවෙන්නේ. 5 දෙනෙකුගේ ලකුණු ඇතුලත් කිරීමට loop එකක් අවශ්‍ය උනා වගේ Output කිරීමටත් loop එකක් අවශ්‍ය බව ඔබට පෙනෙනවා ඇති. අපි බලමු එහෙනම් ඒ කොටස විසදන හැටි.
      #include <stdio.h>
      
      int main()
      {
          int i;
          int number[i];
          
          for(i = 0 ; i < 5 ; i++)  // ලකුණු ඇතුලත් කිරීමට භාවිතා කල loop එක
          {
              printf("Enter Student mark = ");
              scanf("%d", &number[i]);
          }
          
          printf("\n");
          
          for(i = 0 ; i < 5 ; i++) //ලකුණු Output කිරීම සඳහා භාවිතා කරන loop එක.
          {
              printf("Student mark is = %d \n", number[i]); ////වටයෙන් වටයට i++ මගින් i වල අගය එකකින් වැඩි කරයි. එමගින් අපට ගබඩා කරගෙන තිබූ ලකුණු පිලිවෙලට Output කල හැකිය
          }
      
          return 0;
      }
      

      Output -:

      Enter Student mark = 12
      Enter Student mark = 23
      Enter Student mark = 45
      Enter Student mark = 56
      Enter Student mark = 67
      
      Student mark is = 12 
      Student mark is = 23 
      Student mark is = 45 
      Student mark is = 56 
      Student mark is = 67 
      
      1. තුන්වන ප්‍රශ්නයෙන් අපට කියන්නේ කිසියම් අවස්තාවක ලකුණු 100ට වඩා හෝ 0 ට වඩා කුඩා සංඛ්‍යාවක් ඇතුලත් කලොත් එම සංඛ්‍යාව වැරදි බවත් නිවැරදි සංඛ්‍යාව නැවත ඇතුලත් කරන ලෙසත් අපගේ program එක වැඩිදියුණු කරන ලෙසයි. ඔබට පෙනෙනවා ඇති මෙය තීරණ ගැනීමේ අවස්තාවක් බව. එම නිසා මෙහිදී අප if හෝ switch භාවිතා කල යුතු වනවා. මන් මේ අවස්තාවේදී ඕන කෙනෙක්ට තේරෙන if එක භාවිතා කරන්නම්.
      #include <stdio.h>
      
      int main()
      {
          int i;
          int number[i];
          
          for(i = 0 ; i < 5 ; i++)// ලකුණු ඇතුලත් කිරීමට භාවිතා කල loop එක            
          {
              printf("Enter Student mark = ");
              scanf("%d", &number[i]);
              
              if(number[i] < 0 || number[i] > 100) //ඇතුලත් කල සංඛ්‍යාව 100ට වඩා විශාල හෝ 0ට වඩා කුඩා දැයි බැලීම.
              {
                  printf("Wrong mark!, Please enter that mark again\n\n"); //ඉහත කාරණ දෙකෙන් එකක් සත්‍ය වුවහොත් මෙය ක්‍රියාත්මක වීම.
                  i--; //loop එක අවසානයේ i++ ක්‍රියාකරන නිසා array එක අකකින් වැඩි වේ. එවිට අප ඇතුලත් කල 100ට වඩා වැඩි 0ට වඩා අඩු සංඛ්‍යා ද output වේ. මෙය වැලැක්වීම සදහා අප i--; භාවිතා කරයි. එමගින් වැරදි සංඛ්‍යා array එකේ ගබඩා වීම වලකී.
              }
          }
          
          printf("\n");
          
          for(i = 0 ; i < 5 ; i++)
          {
              printf("Student mark is = %d \n", number[i]);
          }
      
          return 0;
      }
      

      Output -:

      Enter Student mark = 50
      Enter Student mark = 34
      Enter Student mark = 125
      Wrong mark!, Please enter that mark again
      
      Enter Student mark = 100
      Enter Student mark = -45
      Wrong mark!, Please enter that mark again
      
      Enter Student mark = 45
      Enter Student mark = 0
      
      Student mark is = 50 
      Student mark is = 34 
      Student mark is = 100 
      Student mark is = 45 
      Student mark is = 0 
      

      අපි දැන් array එකක් භාවිතා කරමින් strings සම්බන්ද ගැටළුවක් විසදමු.

      Question -: Write a C program to output any paragraph which you enter by using arrays.

      ඉහත ගැටළුවේ කියන්නේ අපි enter කරන ජේදයක් array එකක් මගින් output කරන ලෙසයි. එසේනම් ඔබට පෙනෙනවා ඇති මෙම ගැටළුවට char හා %s සම්බන්ද වන බව.අපි දැන් මේ ගැටළුව විසදීමට උත්සහ කරමු.

      #include <stdio.h>
      
      int main()
      {
          char str[20];
      	
          printf("Enter a string = ");
          scanf("%s", &str);  //string එකක් ඇතුලත් කිරීම.
          
          printf("%s", str);
      
          return 0;
      }
      

      First try Output -:

      Enter a string = hello world
      hello
      

      Second try Output -:

      Enter a string = helloworld
      helloworld
      

      ඉහතදී ඔබට පෙනෙනවා ඇති හිස්තැනක් තියලා තිබ්බ වාක්‍යයෙන් පලමු වචනය පමණක් output උන අතර හිස්තැනක් තියන්නැතුව එක ගොඩේ ලියපු වාක්‍ය කිසි අවුලක් නැතුව output උනා. ඔබට පෙනෙනවා ඇති ඉහත code එක මගින් අප තබන හිස්තැන් පිලිගන්නේ නැති බව. එබැවින් හිස්තැන් පිලිගැනීමට නම් ඉහත code එකේ පොඩි වෙනසක් කල යුතු වනවා. අප දැන් බලමු ඒ වෙනස මොකක්ද කියලා.

      #include <stdio.h>
      
      int main()
      {
          char str[20];
      	
          printf("Enter a string = ");
          scanf("%[^\n]", &str);  //%s ඉවත් කොට %[^\n] මගින් සිදුවන්නේ අප තබන හිස්තැන් code එක මගින් පිලිගැනීමයි.
      	
          printf("%s", str);
      
          return 0;
      }
      

      Output -:

      Enter a string = hello world
      hello world
      

      ඔබට පෙනෙනවා ඇති %s වෙනුවට %[^\n] භාවිතා කිරීමෙන් හිස්තැන් ගැටළුව මගහරවා ගත හැකි බව.

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

      Question 1 -: Computers are commonly used for data analysis. Studnets in a class has scored marks between 0 to 20 for their mid semester examination. Write a C program that reads marks of 15 students in to a single subscripted array and find the mean of the marks.

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


      Question 2 -: Write a C program that read numbers from an integer array and graph the information in the form of bar chat. Sample output is given below.

      Element Value   Histogram
         0      19    *******************
         1       3    ***
         2      15    ***************
         3       7    *******
         4      11    ***********
         5       9    *********
         6      13    *************
         7       5    *****
         8      17    *****************
         9       1    *
      

      Answer -: https://drive.google.com/open?id=17lDMNZ2KVDWZm4At4ho_ztjVHE4xzDli


      • 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
      • C Programming මුල සිට ඉගෙනගනිමු (part 9 - While loop) -: https://link.lankadevelopers.com/4TBV5
      • C Programming මුල සිට ඉගෙනගනිමු (part 10 - do-while loop) -: https://link.lankadevelopers.com/4WcNd
        C Programming මුල සිට ඉගෙනගනිමු (part 11 - for loop) -: https://link.lankadevelopers.com/8utoa

      මන් ඊළග ලිපියෙන් කියලා දෙන්නම් 2D-arrays ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.
      තව ලිපියකින් හම්බෙමු ජය වේවා.

      posted in Blogs
      Kalana
      Kalana
    • C Programming මුල සිට ඉගෙනගනිමු (part 11 - for loop)

      සුබ දවසක් වේවා ඔයාලට. කලින් ලිපියේ මන් do-while loop එක ගැන කතා කලා. අද මන් කතා කිරීමට බලාපොරොත්තු වෙන්නේ for loop එක ගැන. මෙම loop එකෙන් කරන්නේද් අනිත් loop වර්ග දෙකෙන් කරන කාර්යයම වේ. මෙහි ක්‍රියාකාරිත්වය වැඩි වශ්‍යෙන් සමාන වන්නේ while loop එකේ ක්‍රියාකාරිත්වයටයි. සාමාන්‍යයෙන් loop වලින් වැඩි වශයෙන් අප භාවිතා කරන loop එක වන්නේද for loop එකයි. පලමුව මන් මෙහි ආකෘතිය ගෙනහැර දක්වන්නම්.

      for (initialization; condition test; increment or decrement)
      {
          //Statements to be executed repeatedly
      }
      

      මන් ඉහත ආකෘතිය උදාහරණයක් මගින් පැහැදිලි කරන්නම්. අපි මේ ආකෘතිය යොදා ගනිමින් 1 සිට 10 ට දක්වා ඇති සංඛ්‍යා diplay කරමු.

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

      ඉහත උදාහරණයෙන් ඔබට පෙනෙනවා ඇති while loop එකේදී අපි තැන තැන භාවිතා කල සියලු loop එකට අදාල කොටස් for loop එකේදී එක තැනකට ඇවිත් ඇති බව. මෙය මූලික ආකෘතිය වුවත් මෙය භාවිතා කල හැකි අනු ආකෘති කිහිපයක් පවතිනවා. එනම්

      1. i++ වෙනුවට i = i + 1 භාවිතා කල හැකිය.
      for (i = 1 ; i <= 10 ; i = i + 1)
      {
          printf("%d ", i);
      }
      
      1. for loop එකේ i වල අගය විශේෂණය කර ඇති කොටස එනම් i = 1 කියන කොටස for loop එක එලියෙන් මෙසේ ලිවිය හැකිය. නමුත් ; ලකුණ තිබෙන පරිදි තිබීමට දිය යුතුය.
      int i = 1;
      
      for ( ; i <= 10 ; i++)
      {
          printf("%d ", i);
      }
      
      1. i++ හෙවත් i වල අගය ඉදිරියට ගෙනියන code එක අපට for loop එක ඇතුලත් ඕනෑම තැනක යෙදීමට පුලුවන් වේ. මෙහිද ; ලකුණ තිබෙන පරිදි තිබීමට දිය යුතුය.
      for (i = 1 ; i <= 10 ; )
      {
          printf("%d ", i);
      	i++;
      }
      
      1. i = 1 කොටස හා i++ යන කොටස් දෙකම for එක තුලින් ඉවත් කොට while loop ආකෘතිය මෙන් ලිවීමට හැකියාව ඇත.
      int i = 1;
      
      for ( ; i <= 10 ; )
      {
          printf("%d ", i);
      	i++;
      }
      
      1. i++ විතරක් නොව i වල අගය අඩු කල හැකි i-- ද අපට for loop එකක භාවිතා කල හැකිය.
      for (i = 10 ; i >= 1 ; i--)
      {
          printf("%d ", i);
      }
      
      Output -: 10 9 8 7 6 5 4 3 2 1
      

      මේකෙදිත් මන් ඔයාලට ප්‍රශ්න දීමට බලාපොරොත්තු වෙන්නේ නෑ. මොකද while loop ලිපියේ දීපු ප්‍රශ්න මේකෙදිත් ඔයාලට කරන්න පුලුවන් නිසා. ඒ උණත් මන් for loop එකට ආවේණික ප්‍රශ්න කිහිපයක් කරන්නම්.

      Question -: Write a C program to print below format using for loops.

      *******
      *******
      *******
      *******
      

      for loop කිහිපයක් යොදා ගනිමින් ඉහත හැඩතලය නිරූපණය කරන program එකක් code කරන්න කියලා අපට කියනවා. අපි මෙය පියවර වශයෙන් පැහැදිලි කරගම්මු.

      • අපි පලමුව මෙම මුළු හැඩතලයෙන් කොටසක් එනම් පලමු පේලිය(*******) පමණක් for loop එකක් මගින් output කරමු .
      #include <stdio.h>
      
      int main()
      {
          int i;
          
          for(i = 1 ; i <= 7 ; i++)
          {
              printf("*");
          }
          return 0;
      }
      
      Output -: *******
      
      • ඔබට පෙනෙනවා ඇති එක for loop එකක් මගින් එක් පේලියක් කැමති තරු ගණනකින් පිරවිය හැකි බව. නමුත් අපට අවශ්‍ය වන්නේ ඒ වගේ පේලි 4ක් වේ. තරු 7ක් Output කරන්න එක් for loop
        එකක් අවශ්‍ය නම් ඒ වගේ තරු 7 පේලි 4 ගැනීමට අවශ්‍යනම් තව for loop එකක් අව්ශ්‍ය බව ඔබට පෙනෙනවා ඇති. අපි එය මේ විදිහට කරමු.
           තීරු තීරු
      පේලි  *    *    
      පේලි  *    *
      පේලි  *    *
      

      තීරු ගණන වන තීරු 7 ඉහත for loop එක මගින් අප අරගෙන ඉවර නිසා පේලි 4 ගන්න විදිහ දැන් බලමු.

      • තීරු -: columns
      • පේලි -: raws
      #include <stdio.h>
      
      int main()
      {
          int raw; //පේලි ගණන නිරූපණය කරන variable එක
          int col; //තීරු ගණන නිරූපණය කරන variable එක
          
          for(raw = 1 ; raw <= 4 ; raw++) //පේලි 4ක් print කිරීමට යොදා ගනී.
          {
              for(col = 1 ; col <= 7 ; col++) // තීරු 7ක් එනම් තරු 7 print කිරීමට යොදා ගනී
              {
                  printf("*");
              }
              printf("\n"); //තීරු 7ක් එනම් තරු 7ක් print වීමෙන් අනතුරුව ඉතුරු තරු 7 ඒවා පහලින් පහලට print කිරීමට මෙය යොදා ගනී.
          }
          return 0;
      }
      
      Output -:*******
               *******
               *******
               *******
      

      ඔයාලට තරු රටා පිලිබද වැඩි දුරටත් ඉගෙන ගන්න ඕනනම් මන් ලින්ක් කිහිපයක් දාන්නම් නැත්තම් Google search bar එකට ගිහින් star patterns in C කියලා search කරන්න.
      Link1 -: https://codeforwin.org/2015/07/star-patterns-program-in-c.html
      Link2 -: https://www.programiz.com/c-programming/examples/pyramid-pattern
      Link3 -: https://www.programmingsimplified.com/c-program-print-stars-pyramid

      මන් ඔයාලට සරල වගේම ටිකක් සංකීර්ණ ගැටලු කිහිපයක් දෙන්නම්, නිකන් ඉන්න වෙලාවට කරලා බලන්න.

      Question 1 -: Write for statements that print the following sequence of values:

      a) 3, 8, 13, 18, 23
      b) 20, 14, 8, 2, -4, -10
      c) 19, 27, 35, 43, 51
      

      answer for a -: https://drive.google.com/open?id=1hT_MzDFb7UBIscO1UpGnFXtPbykzjHH2
      answer for b -: https://drive.google.com/open?id=1PAuJdwDuWA1_VflDKTaNLW_hLax-Poeg
      answer for c -: https://drive.google.com/open?id=1HpibNCAQ7lV-Ed-OAt8pm_ktvGjWLIxl


      Question 2 -: Write a C program using for loops to display the following pattern on the screen

      *
      **
      ***
      ****
      *****
      ******
      *******
      *******
      *****
      ***
      *
      

      answer -: https://drive.google.com/open?id=147j8QuV7y-tSYNKTsRGkSoIq1ADAvWgU


      Question 3 -: Calculate the value of pie(3.14) from the infinite series

      = 4 − 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ....
      

      Print a table that shows the value of pie approximated by one term of this series, by two terms, by
      three terms, and so on. How many terms of this series do you have to use to get 3.14159?

      answer -: https://drive.google.com/open?id=1AC1XYfbCRTj3PU5NPZjkiDC1MUj5mNmZ


      Question 4 -: The factorial of a nonnegative integer n is written n! and is defined as follows:

      n! = n * (n-1) * (n-2) * ……1
      

      and

      n! = 1 (for n =0)
      

      For example, 5! = 5 * 4 * 3 * 2 * 1, which is 120
      Write a C program that reads a nonnegative integer and computes and print its factorial using a
      for loop.

      answer -: https://drive.google.com/open?id=1Pjo-p8CKhgnJ5WxuM6naT2El-mJ3pjpX


      • 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
      • C Programming මුල සිට ඉගෙනගනිමු (part 9 - While loop) -: https://link.lankadevelopers.com/4TBV5
      • C Programming මුල සිට ඉගෙනගනිමු (part 10 - do-while loop) -: https://link.lankadevelopers.com/4WcNd

      මන් ඊළග ලිපියෙන් කියලා දෙන්නම් arrays ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.
      තව ලිපියකින් හම්බෙමු ජය වේවා.

      posted in Blogs
      Kalana
      Kalana
    • C Programming මුල සිට ඉගෙනගනිමු (part 10 - do-while loop)

      සුබ දවසක් වේවා ඔයාලට. කලින් ලිපියේ මන් while loop එක ගැන කතා කලා. අද මන් කතා කිරීමට බලාපොරොත්තු වෙන්නේ do-while loop ගැන. මෙම loop එක ගැන කතා කලොත් මෙහි කාර්‍යයද වන්නේ while loop එකේ කාර්‍ය්යම වේ. නමුත් මේ දෙකෙහි පොඩි වෙනසක් ති‍යෙනවා. ඒක ගැන මන් ඉදිරියට කතා කරන්නම්. තවද do-while loop එක තමයි අපි අඩු වශයෙන්ම භාවිතා කරන loop එක.
      පලමුව මන් මෙහි ආකෘතිය ගෙනහැර දක්වන්නම්

      do
      {
          //Statements 
          //Increment(++) or Decrement(--) operator
      }while(condition);
      

      මේ ආකෘතිය යොදා ගනිමින් 1 සිට 10 ට දක්වා ඇති සංඛ්‍යා diplay කරමු.

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

      ඔබට පෙනෙනවා ඇති මෙම පිලිතුරම අපට ලැබුනා while loop එකක් භාවිතා කිරීමේදී(කලින් ලිපිය බලන්න) නමුත් do-while වල පොඩි විශේෂත්වයක් පවතිනවා. ඒ තමයි while(condition); වල ඇති condition එක පරීක්ෂා කිරීමට පෙර do එක තුල ඇති code එක run වීමයි. මන් එය පහත උදාහරණ දෙකෙන් පෙන්වන්නම්.

      while loop එකක් ක්‍රියා කරන ආකාරය

      #include <stdio.h>
      
      int main()
      {
          int i = 0;
      	
          while(i == 1)
          {
      	printf("Inside of the while loop");
          }
      	
          printf("Outside of the while loop");
      }
      
      Output -: Outside of the while loop
      
      • පලමුව අපි i වල අගය 0ට සමාන කලා. ඊට පස්සේ while එකේ condition එක බලද්දි අපගේ i වල වර්තමාන අගය 1ට සමාන නොවන නිසා while loop එක run නොවී ඒකෙන් එලියේ
        ඇති code එක run උනා.

      do-while loop එකක් ක්‍රියා කරන ආකාරය

      #include <stdio.h>
      
      int main()
      {
         int i = 0;
         
         do
         {
              printf("Inside of the do-while loop\n");
         }while(i==1);
         
         printf("Outside of the do-while loop");
      }
      
      Output -: Inside of the do-while loop
                Outside of the do-while loop
      
      • මෙහිදී ඔබට පෙනෙනවා while loop එක ආකරයෙන් නොව වෙනත් Output එකක් අපට ලැබුනා. මෙහිදී loop එක ඇතුලේ පිහිටි code එකත් run උනා. මෙයට හේතුව වන්නේ අප program එක run කල විට do-while loop එකේ මුලින්ම run උනේ do කොටසයි. condition එක අයිති කොටස පවතින්නේ අවසානයේ නිසා මෙම ක්‍රියාකාරිත්වය නිතර සිදු වේ. එම නිසා එක්වරක් loop එක ඇතුලේ ඇති code එක run වී condition එක පරීක්ෂා කරයි, එහිදී i වල වර්තමාන අගය වන 0 කිසිවිටකත් 1ට සමාන නොවන නිසා තවදුරටත් loop එක ක්‍රියා කිරීම නවතී. ඉන්පසු loop එකට පිටත ඇති code එක run වේ.

      while loop එකේදී කතා කල සියලුම දෑ මෙයටත් අදාල වන නිසා මන් තවදුරටත් මේ ගැන කතා කිරීමට බලාපොරොත්තු වන්නේ නෑ. ඔයාලට මන් ප්‍රශ්න කිහිපයක් දෙන්නම් කරලා බලන්න. එතකොට ඔයාලට තව තේරෙයි මේ ගැන.

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

      Question 2 -: 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 වලදි විසදලා පෙන්නුවා ඒක බලාගෙන මේකත් කරලා බලන්න.


      • 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
      • C Programming මුල සිට ඉගෙනගනිමු (part 9 - While loop) -: https://link.lankadevelopers.com/4TBV5

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

      posted in Blogs
      Kalana
      Kalana
    • C Programming මුල සිට ඉගෙනගනිමු (part 9 - While loop)

      සුබ දවසක් වේවා ඔයාලට. අද මන් කතා කිරීමට බලාපොරොත්තු වෙන්නේ 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 එක ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.

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

      posted in Blogs
      Kalana
      Kalana
    • RE: Kubernetes Sri Lanka Meetup - 2020 Kick Off!

      Thank you @cody

      posted in Events ( Free & Paid )
      Kalana
      Kalana
    • RE: Enadoc TechMeetUp

      Thank you @cody

      posted in Events ( Free & Paid )
      Kalana
      Kalana
    • RE: PIXY (Version 1.1) now available!

      Congratulations brother. Good work!

      posted in Mobile Application Development
      Kalana
      Kalana
    • C Programming මුල සිට ඉගෙනගනිමු (part 8 - Switch Statement)

      සුභ දවසක් වේවා ඔයාලට. අද ම්න් කතා කරන්න බලාපොරොත්තු වෙන්නේ C Programming වල එන Switch Statement එක ගැනයි. Switch Statement එක කියන්නේ අපි කලින් කතා කරපු
      if-else statement එකට සමාන දෙයකි. මෙහිදීද කරන්නේ අප ගත් තීරණයක් හරිද වැරදිද කියලා සොයා බැලීමයි.

      අපි දැන් බලමු Switch Statement එකේ ආකෘතිය මොන වගේ එකක්ද කියලා.

      switch(variable)
      {
      	case condition1 : //codes
      			  //codes
      			  break;
      	
      	case condition2 : //codes
      			  //codes
      			  break;
      					  
      	default : //codes
      		  //codes
      		  break;
      }
      
      • switch(variable) -: මෙහිදී සිදුවන්නේ අපට පරීක්ශා කිරීමට සිදුවන variable එක switch එකක් තුල store කර ගැනීමයි. එසේ කල විට අපට if-else වගේ නැවත නැවත එම
        variable එක ලිවීමට අව්ශ්‍ය නොවේ.

      • case condition1 : //codes -: මෙහිදී අප කරන්නේ අප ඇතුලත් කරපු දත්තය සත්‍ය හෝ අසත්‍යදැයි බැලීමයි. සත්‍ය නම් අපගේ //codes ටික execute වන අතර අසත්‍යනම් ඊලග
        case එක execute වේ. මෙය හරියට if-else වල එන if එකට හා elseif` එකට සමාන වේන්

      • default : //codes -: මෙය හරියටම if-else වල එන else වලට සමාන වේ. කිසිම case එකක් සත්‍ය නොවේනම් default එක ක්‍රියාත්මක වේ. මෙයට කිසිවිටකත්
        condition එකක් නැත.

      • break; -: මෙය යොදන්නේ case හා default සියල්ලම එකවර execute වීම වැලැක්වීමටයි. මෙය යෙදුවේ නැති වුවහොත් case හා default සියල්ලන්ගේම ඇති codes
        run වේ. මෙය මගින් කිසියම් case එකක් සත්‍ය වුවහොත් එම case එක පමණක් run වී සම්පූර්ණswitch(){} එකෙන් අපව එලියට ගෙන යයි.

      Switch Statement භාවිත කිරීමේදී අප නීති කිහිපයක් අනුගමනය කලයුතු වනවා.

      1. switch(variable) මෙහි එන variable එක සෑම විටම integer හෝ character එකක් විය යුතු වනවා. එය කිසිවිටකත් float විය නොහැකිය.
      2. case condition : මෙහි එන condition එකත් සෑම විටම integer හෝ character එකක් විය යුතු වනවා.
      3. case condition : මෙහි එන condition භාවිතා කල හැක්කේ switch එකක් තුල පමණි.
      4. break; අනිවාර්‍ය අංගයක් නොවන අතර එය භාවිතා කරන්නේ. එකවර case කිහිපයක් run වීම වැලැක්වීමටයි.

      මන් කිව්ව ගොඩක් දේවල් පැහැදිලි වෙයි මේ උදාහරණයෙන්.

      Example -: Input any number from the keyboard. After that check the following using switch statement

      • number == 25 , print "number is equal to 25"
      • number == 50 , print "number is equal to 50"
      • number == 75 , print "number is equal to 75"
      • else, print "Your number is not equal to 25, 50 or 75"

      write a C program to fulfill this conditions.

      මෙහිදී කියන්නේ කිසියම් සංඛ්‍යාවක් ඇතුලත් කල විට එය 25, 50, 75 ට සමානනම් සමාන ලෙසත් සමාන නොවේනම් සමාන නොවේ ලෙසත් output කල යුතු බවයි.

      #include <stdio.h>
      
      int main()
      {
          int number;
          
          printf("Enter a number = "); //කිසියම් සංඛ්‍යාවක් ඇතුලත් කිරීම.
          scanf("%d",&number);
          
          switch(number)
          {
              case 25 : printf("Number is equal to 25"); //ඇතුලත් කල සංඛ්‍යාව 25ට සමානනම්
                        break;
                        
              case 50 : printf("Number is equal to 50");//ඇතුලත් කල සංඛ්‍යාව 50ට සමානනම්
                        break;
                        
              case 75 : printf("Number is equal to 75");//ඇතුලත් කල සංඛ්‍යාව 75ට සමානනම්
                        break;
      
              default : printf("Number is not equal to 25, 50 or 75");//ඇතුලත් කල සංඛ්‍යාව 25, 50, 75 ට සමාන නොවේනම්
                        break;
          }
          return 0;
      }
      

      Output

      First run -: Enter a number = 25
                   Number is equal to 25
      			 
      Second run -: Enter a number = 50
                    Number is equal to 50
      			 
      Third run -: Enter a number = 75
                   Number is equal to 75
      			 
      Fourth run -: Enter a number = 100
                    Number is not equal to 25, 50 or 75
      

      අපි දැන් බලමු break; නැතුව මෙම code එක run කලාම ලැබෙන output එක මොකක්ද කියලා

      #include <stdio.h>
      
      int main()
      {
          int number;
          
          printf("enter a number = ");
          scanf("%d",&number);
          
          switch(number)
          {
              case 25 : printf("\nNumber is equal to 25"); //break; යොදා නැත
              case 50 : printf("\nNumber is equal to 50");
              case 75 : printf("\nNumber is equal to 75");
              default : printf("\nNumber is not equal to 25, 50 or 75");
          }
          return 0;
      }
      

      Output

      First run -: Enter a number = 25
                   Number is equal to 25
      	     Number is equal to 50
      	     Number is equal to 75
      	     Number is not equal to 25, 50 or 75
      			 
      Second run -: Enter a number = 50
                    Number is equal to 50
      	      Number is equal to 75
      	      Number is not equal to 25, 50 or 75
      			 
      Third run -: Enter a number = 75
                   Number is equal to 75
      	     Number is not equal to 25, 50 or 75
      			 
      Fourth run -: Enter a number = 100
                    Number is not equal to 25, 50 or 75
      

      ඉහත උදාහරණයෙන් උබට පෙනෙනවා break; එකක තිබෙන වටිනාකම හා switch එකක ක්‍රියාකාරිත්වය.

      if-else වල වගේ switch වලත් nested ආකාර පවතී. එනම් switch ඇතුලේ තව switch තිබීමට හැකිය.

      Example

      #include <stdio.h> 
      
      int main () 
      {  
          int number1;  
          int number2;  
          
          printf("Enter Number1 = ");
          scanf("%d", &number1);
             
          switch(number1) 
          {  
              case 10 : printf("Number1 is equal to 10");  
                        break;
                        
              case 20 : printf("Number1 is equal to 20 there for enter Number2 = ");
                        scanf("%d", &number2);
                        switch(number2) 
                        {  
                              case 50 : printf("Number2 equal to 50");  
                                        break;
                                        
                              default : printf("Number2 not equal to 50"); 
                        }
                        break;
                         
              default : printf("Number1 is not equal to 10 or 20");
             }  
              
             return 0;  
      }  
      

      Output

      First run -: Enter Number1 = 10
                   Number1 is equal to 10
      			 
      Second run -: Enter Number1 = 20
                    Number1 is equal to 20 there for enter Number2 = 50
      	      Number2 equal to 50
      			 
      Third run -: Enter Number1 = 20
                   Number1 is equal to 20 there for enter Number2 = 70
      	     Number2 not equal to 50
      			 
      Fourth run -: Enter Number1 = 100
                    Number1 is not equal to 10 or 20
      

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

      Question 1 -: Write a C program to create a simple calculator. The program should allow the user to
      enter two numbers and an option. The options are

      1. +
      2. -
      3. *
      4. /
        You should print an appropriate error message if the user enters invalid operator. Use switch statement in your program.

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


      Question 2 -: An online retailer sells five different products whose retail prices are shown in the following
      table:

      Product number  Retail price
            1            2.98
            2            4.50
            3            9.98
            4            4.49
            5            6.87
      

      Write a C program that reads the following:

      1. Product number
      2. Quantity sold for one day
        Your program should use a switch statement to determine the total retail price for each product.

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


      Question 3 -: Write a C program that compute the area of a square (area=side*2 ) or a triangle
      (area=base*height/2) after prompting the user to type the first character of the shape (S or T).

      1. Write the program using if-else statements
      2. Write the program using switch statement.
      3. Modify the program to find the area of a circle when the Letter “C” is entered.
      4. Modify the program to print an error message when the user enters an invalid
        character.

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


      • මූලාශ්‍ර -: https://beginnersbook.com/2014/01/switch-case-statements-in-c/

      • 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

      මන් ඊළග ලිපියෙන් කියලා දෙන්නම් Iterate(loops)statements ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.
      තව ලිපියකින් හම්බෙමු ජය වේවා.

      posted in Blogs
      Kalana
      Kalana
    • RE: JAVA OOP සිංහලෙන්

      Link tika break wela brother. aparade

      posted in Programming
      Kalana
      Kalana
    • RE: C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C)

      @root Thank you for sharing with me

      posted in Blogs
      Kalana
      Kalana
    • C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C)

      සුභ අලුත් අවුරුද්දක් වේවා හැමෝටම!

      සමාවෙන්න මේ දවස් ටිකේ පෝස්ට් දාන්න බැරු උනාට. පොඩි වැඩ වගයක හිටියේ. ලොකු වෙලාවක් තිබ්බේ නැති නිසා මන් මේක ඉංග්‍රීසි වලින් ලිව්වේ. ඊලඟ ලිපි අනිවාර්‍යයෙන් සිංහලෙන් එනවා. මේක තේරෙන් නැත්තම් පොඩ්ඩක් කියන්න. මන් සිංහලට පරිවර්තනය කරන්නම්.

      Many peoples have lot of problems with C format specifiers. Most of them don't know how to use it,
      when can use it, ranges of data types and etc. I have studied these format specifiers and it takes few hours to
      complete this article.

      I would like to show these using table and give an example to each format specifier by explain them.

      char

      Format specifier	Description			Type				range					Uses
      	%c		Character			char				-128 to 128				Use to output single character
      	%c		Character			unsigned char		        0 to 255				Use to output single character
      	%s		String				char * , char []		-					Use to output any range of strings
      

      Examples

      #include <stdio.h>
      
      int main()
      {
          char letter1 = 'a';
          unsigned char letter2 = 'a';
          
          char *words1 = "Hello World!";
          char words2[] = "Hello Guys!";
          
          printf("letter 1 = %c", letter1); //Output only one character
          printf("\nletter 2 = %c\n", letter2);
      
          printf("\n%s", words1); //Output any range of strings
          printf("\n%s", words2);
          
          letter1 = 255;
          letter2 = 255;
          
          printf("\n\nchar          = %d\n", letter1); //Diferrence between char and unsigned char
          printf("Unsigned char = %d\n", letter2);
          
          return 0;
      }
      

      Output

      letter 1 = a
      letter 2 = a
      
      Hello World!
      Hello Guys!
      
      char          = -1
      Unsigned char = 255
      

      Integer(short)

      Format specifier	Description			Type				range(Uses)	
      	%hd		Integer				short int			-32,768 to 32,767
      	%hu		"				unsigned short int	              0 to 65,535
      

      Examples

      #include <stdio.h>
      
      int main()
      {
          short int min1 = -32768;
          short int max1 = 32767;
          
          unsigned short int min2 = 0;
          unsigned short int max2 = 65535;
        
          printf("Minimum value of short int = %hd", min1); 
          printf("\nMaximum value of short int = %hd", max1); 
          
          printf("\n\nMinimum value of unsigned short int = %hu", min2); 
          printf("\nMaximum value of unsigned short int = %hu", max2); 
          
          return 0;
      }
      

      Output

      Minimum value of short int = -32768
      Maximum value of short int = 32767
      
      Minimum value of unsigned short int = 0
      Maximum value of unsigned short int = 65535
      

      Integer

      Format specifier	Description		Type					range						Uses
      	%d		Integer			int				-2,147,483,648 to 2,147,483,647		Can take integers as decimals
      	%u		"			unsigned int				 0 to 4,294,967,295		Output only positive numbers
      	%i		"			unsigned int	                -2,147,483,648 to 2,147,483,647		Can take integers as decimals, 
      															hexadecimals and octals type.
      

      Examples

      #include <stdio.h>
      
      int main()
      {
          int min1 = -2147483648;
          int max1 = 2147483647;
          
          unsigned int min2 = 0;
          unsigned int max2 = 4294967295;
          
          unsigned int min3 = -2147483648;
          unsigned int max3 = 2147483647;
          
          unsigned int decimal = 12;
          unsigned int octal = 012;
          unsigned int hexadecimal = 0X12;
          
          
          printf("Minimum value of int = %d", min1); 
          printf("\nMaximum value of int = %d", max1);
          
          printf("\n\nMinimum value of unsigned int(u) = %u", min2); 
          printf("\nMaximum value of unsigned int(u) = %u", max2);
          
          printf("\n\nMinimum value of unsigned int(i) = %i", min3); 
          printf("\nMaximum value of unsigned int(i) = %i", max3);
          
          printf("\n\n12 in decimal format       = %i", decimal); 
          printf("\n012 in octal format        = %i", octal);
          printf("\n0X12 in hexadecimal format = %i", hexadecimal);
          
          return 0;
      }
      

      Output

      Minimum value of int = -2147483648
      Maximum value of int = 2147483647
      
      Minimum value of unsigned int(u) = 0
      Maximum value of unsigned int(u) = 4294967295
      
      Minimum value of unsigned int(i) = -2147483648
      Maximum value of unsigned int(i) = 2147483647
      
      12 in decimal format       = 12
      012 in octal format        = 10
      0X12 in hexadecimal format = 18
      

      Integer(long and long long)

      Format specifier	Description		Type											range(Uses)
      	%ld or %li	Integer			long int							-2,147,483,648 to 2,147,483,647
      	%lu		"			unsigned long int					 		     0 to 4,294,967,295
      	%lld or %lli	"			long long int			          -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807(-(2^63) to (2^63)-1)
      	%llu		"			unsigned long long int				 			 0 to 18,446,744,073,709,551,615 
      

      Examples

      #include <stdio.h>
      
      int main()
      {
          long int min1 = -2147483648;
          long int max1 = 2147483647;
          
          unsigned long int min2 = 0;
          unsigned long int max2 = 4294967295;
          
          long long int min3 = -9223372036854775808; // 2^63
          long long int max3 = 9223372036854775807; // 2^63 - 1
          
          unsigned long long int min4 = 0;
          unsigned long long int max4 = 18446744073709551615;
      
          
          printf("Minimum value of long int(ld) = %ld", min1); 
          printf("\nMaximum value of long int(ld) = %ld", max1);
          
          printf("\n\nMinimum value of long int(li) = %li", min1); 
          printf("\nMaximum value of long int(li) = %li", max1);
          
          printf("\n\nMinimum value of unsigned long int(lu) = %lu", min2); 
          printf("\nMaximum value of unsigned long int(lu) = %lu", max2);
          
          printf("\n\nMinimum value of long long int(lld) = %lld", min3); 
          printf("\nMaximum value of long long int(lld) = %lld", max3);
          
          printf("\n\nMinimum value of long long int(lli) = %lli", min3); 
          printf("\nMaximum value of long long int(lli) = %lli", max3);
          
          printf("\n\nMinimum value of unsigned long long int(llu) = %llu", min4); 
          printf("\nMaximum value of unsigned long long int(llu) = %llu", max4);
          
          return 0;
      }
      

      Output

      Minimum value of long int(ld) = -2147483648
      Maximum value of long int(ld) = 2147483647
      
      Minimum value of long int(li) = -2147483648
      Maximum value of long int(li) = 2147483647
      
      Minimum value of unsigned long int(lu) = 0
      Maximum value of unsigned long int(lu) = 4294967295
      
      Minimum value of long long int(lld) = -9223372036854775808
      Maximum value of long long int(lld) = 9223372036854775807
      
      Minimum value of long long int(lli) = -9223372036854775808
      Maximum value of long long int(lli) = 9223372036854775807
      
      Minimum value of unsigned long long int(llu) = 0
      Maximum value of unsigned long long int(llu) = 18446744073709551615
      

      Float, Double and Long Double

      Format specifier	Description		Type			Range					Uses
      	%f		Float			float			1.2E-38 to 3.4E+38		Use when number has 6 decimal places
      	%lf		Double			double			2.3E-308 to 1.7E+308	Use when number has 15 decimal places
      	%Lf		Long Double		long Double		3.4E-4932 to 1.1E+4932	Use when number has 19 decimal places
      	%e or %E	Float, Double	        float, double					Scientific notation of float values
      	%g or %G	Float, Double	        float, double					Scientific notation of float values
      												Accept integers and float double also
      

      Examples

      #include <stdio.h>
      
      int main()
      {
          float number1 = 5.12;
          double number2 = 5;
          long double number3 = 5;
          
          printf("Float number is(f)        = %f", number1);
          printf("\nDouble number is(lf)      = %lf", number2);
          printf("\nLong Double number is(Lf) = %Lf", number3);
          
          printf("\n\nFloat number is(e)       = %e", number1);
          printf("\nDouble number is(e)      = %e", number2);
          
          printf("\n\nFloat number is(E)       = %E", number1);
          printf("\nDouble number is(E)      = %E", number2);
          
          printf("\n\nFloat number is(g)       = %g", number1); //Support for both integers and floats, doubles
          printf("\nDouble number is(g)      = %g", number2);
          
          printf("\n\nFloat number is(G)       = %G", number1); //Support for both integers and floats, doubles
          printf("\nDouble number is(G)      = %G", number2);
          
          return 0;
      }
      

      Output

      Float number is(f)        = 5.120000
      Double number is(lf)      = 5.000000
      Long Double number is(Lf) = 5.000000
      
      Float number is(e)       = 5.120000e+00
      Double number is(e)      = 5.000000e+00
      
      Float number is(E)       = 5.120000E+00
      Double number is(E)      = 5.000000E+00
      
      Float number is(g)       = 5.12
      Double number is(g)      = 5
      
      Float number is(G)       = 5.12
      Double number is(G)      = 5
      

      Other Specifiers

      Format specifier	Description		Type							Uses
      	%o		Integer			short int, unsigned short int	Octal representation of Integer.
      						int, unsigned int, long int
      									
      	%x or %X	Integer			short int, unsigned short int   Hexadecimal representation of an Integer.
      						int, unsigned int, long int
      									
      	%p		Void *			void *				Use to get address of pointer or any other variable
      										Address of pointer to void void *
      																	
      	%n		-				-			Prints nothing.It cause printf() to load the variable 
      										pointed by corresponding argument. The loading is done with
      										a value which is equal to the number of characters printed 
      										by printf() before the occurrence of %n.
      																	
      	%%		-				-			Prints % character.
      

      Examples

      #include <stdio.h>
      
      int main()
      {
          int num1 = 65;
          int num2 = 67;
          int num3 = 15;
          
          int* ptr =&num3;
          
          int num4;
          int num5 = 20;
          
          printf("Octal representation of 65 = %o", num1);
          printf("\nOctal representation of 67 = %o", num2);
          
          printf("\n\nHexadecimal representation of 15(x) = %x", num3);
          printf("\nHexadecimal representation of 15(X) = %X", num3);
          
          printf("\n\nAddress of pointer = %p",ptr);
          
          printf("\n\nThe value of %nnum4 and %nnum5 is =  ", &num4, &num5); //Uses of %n
          printf("%d   %d", num4, num5); 
          
          printf("\n\nI got 50%% discount when I was shopping"); //Uses of %%
          
          return 0;
      }
      

      Output

      Octal representation of 65 = 101
      Octal representation of 67 = 103
      
      Hexadecimal representation of 15(x) = f
      Hexadecimal representation of 15(X) = F
      
      Address of pointer = 0x7ffe402adda4 
      
      The value of num4 and num5 is =  15   24
      
      I got 50% discount when I was shopping
      

      I hope this will help who are stuck with format specifiers in c. If I have done any mistakes please mentioned that.

      • 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

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

      • C Programming මුල සිට ඉගෙනගනිමු(part 1 - Introduction) -: https://bit.ly/2O6rLXR
      • C Programming මුල සිට ඉගෙනගනිමු (part 2 - Variables) -: https://bit.ly/2spD6Kn
      • C Programming මුල සිට ඉගෙනගනිමු (part 3 - Operators) -: https://bit.ly/2ruMH22
      • C Programming මුල සිට ඉගෙනගනිමු (part 4 - Input & Output functions) -: https://bit.ly/2qU8IaK
      • C Programming මුල සිට ඉගෙනගනිමු (part 5 - create simple applications) -: https://bit.ly/2DDfy7b
      • C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 1)) -: https://bit.ly/2LiVsmZ
      • C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 2)) -: https://bit.ly/2SJAEJQ

      මන් ඊළග ලිපියෙන් කියලා දෙන්නම් switch statement ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.
      තව ලිපියකින් හම්බෙමු ජය වේවා.

      posted in Blogs
      Kalana
      Kalana
    • RE: ලංකා ඩිවෙලොපර්ස් වෙනස් වෙන්න ඕනෙද ?

      ඔව් අනිවාර්යයෙන්. මේ සයිට් එක තව promote උනානම් campus අතරේ ලොකු වැඩිදියුණුවක් වෙයි.

      posted in General Discussion
      Kalana
      Kalana
    • 1
    • 2
    • 3
    • 2 / 3