C lects (3)
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,736
On Slideshare
1,735
From Embeds
1
Number of Embeds
1

Actions

Shares
Downloads
55
Comments
0
Likes
0

Embeds 1

http://cpl201.blogspot.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Утга өсгөх бууруулах
    • Илүү богино арга
    i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *= a  ==  i = i * a i /= a  ==  i = i / a
  • 2. Давталт
    • Ижил заавруудыг дахин дахин биелүүлэх .
    Бүлэг кодууд Зарим төлөв Өөрчлөгдөнө Нөхцөл биелэх үед давтагдана
  • 3. Давталт
    • C -д дараах давталтууд байдаг .
    • for , while , do-while .
  • 4. while давталт
    • while ( condition )
    • {
    • statement(s);
    • }
    Нөхцөл үнэн бол statement биелнэ Нөхцөл худал болвол давталт дуусна .
  • 5. Жишээ факториал
    • #include <stdio.h>
    • int main()
    • {
    • int i, n, fact = 1;
    • printf(&quot;Enter a number &quot;);
    • scanf(&quot;%d&quot;, &n);
    • i=1; /* тоолуур */
    • while (i<=n)
    • {
    • fact = fact*i;
    • i++; /* i = i+1 */
    • }
    • printf(&quot;the factorial is %d &quot;, fact);
    • return 0;
    • }
  • 6. Жишээ Фибончийн дараалал fibonacci.c
  • 7. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 Screen 5 lim 0 fib1 1 fib2 --- fib_next
  • 8. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 Screen 5 lim 0 fib1 1 fib2 --- fib_next
  • 9. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 Screen 5 lim 0 fib1 1 fib2 --- fib_next
  • 10. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 Screen 5 lim 0 fib1 1 fib2 1 fib_next
  • 11. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
  • 12. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
  • 13. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
  • 14. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next
  • 15. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 Screen 5 lim 1 fib1 1 fib2 2 fib_next
  • 16. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 Screen 5 lim 1 fib1 1 fib2 2 fib_next
  • 17. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 Screen 5 lim 1 fib1 2 fib2 2 fib_next
  • 18. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 Screen 5 lim 1 fib1 2 fib2 2 fib_next
  • 19. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 Screen 5 lim 1 fib1 2 fib2 2 fib_next
  • 20. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 Screen 5 lim 1 fib1 2 fib2 3 fib_next
  • 21. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 Screen 5 lim 2 fib1 2 fib2 3 fib_next
  • 22. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 Screen 5 lim 2 fib1 3 fib2 3 fib_next
  • 23. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 Screen 5 lim 2 fib1 3 fib2 3 fib_next
  • 24. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 3 Screen 5 lim 2 fib1 3 fib2 3 fib_next
  • 25. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 3 Screen 5 lim 2 fib1 3 fib2 5 fib_next
  • 26. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 3 Screen 5 lim 3 fib1 3 fib2 5 fib_next
  • 27. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next
  • 28. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next
  • 29. Фибоначи алхмаар
    • fib1 = 0;
    • fib2 = 1;
    • printf(&quot;%d &quot;, fib1);
    • while (fib2 < lim)
    • {
    • printf(&quot;%d &quot;, fib2);
    • fib_next = fib1 + fib2;
    • fib1 = fib2;
    • fib2 = fib_next;
    • }
    • printf(&quot; &quot;);
    0 1 1 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next
  • 30. getchar
    • getchar() хэрэглэгчийн тэмдэгтийг хүлээн авна .
    • stdio.h -д агуулгагдана
    • Алдаа гарвал сөрөг утга буцаана .
    • scanf адил .
    char c; c = getchar(); char c; scanf(“%c”, &c);  ==== 
  • 31. Putchar
    • putchar( char ) хаалтан дахь тэмдгийг хэвлэнэ .
    • stdio.h байна
    • Printf адил .
    char c; putchar(c); char c; printf(“%c”, c);  ==== 
  • 32. Жишээ – жижиг үсгийг том болгох . low2up.c
  • 33. Low2up – алхмаар
    • #include <stdio.h>
    • int main()
    • {
    • char c;
    • char upper_c;
    • printf(“Enter a string: &quot;);
    • c = getchar();
    Buffer ‘ #’ ‘ @’ c upper_c Screen
  • 34. Low2up – алхмаар
    • #include <stdio.h>
    • int main()
    • {
    • char c;
    • char upper_c;
    • printf(“Enter a string: &quot;);
    • c = getchar();
    yeS Buffer ‘ #’ ‘ @’ c upper_c Screen
  • 35. Low2up – алхмаар
    • #include <stdio.h>
    • int main()
    • {
    • char c;
    • char upper_c;
    • printf (“Enter a string: &quot;);
    • c = getchar();
    eS Buffer ‘ y’ ‘ @’ c upper_c Screen
  • 36. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    eS Buffer ‘ y’ ‘ @’ c upper_c Screen
  • 37. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    eS Buffer ‘ y’ ‘ @’ c upper_c Screen
  • 38. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    eS Buffer ‘ y’ ‘ Y’ c upper_c Screen
  • 39. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    eS Buffer ‘ y’ ‘ Y’ c upper_c Y Screen
  • 40. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    S Buffer ‘ e’ ‘ Y’ c upper_c Y Screen
  • 41. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    S Buffer ‘ e’ ‘ Y’ c upper_c Y Screen
  • 42. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    S Buffer ‘ e’ ‘ Y’ c upper_c Y Screen
  • 43. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    S Buffer ‘ e’ ‘ E’ c upper_c Y Screen
  • 44. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    S Buffer ‘ e’ ‘ E’ c upper_c YE Screen
  • 45. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ S’ ‘ E’ c upper_c YE Screen
  • 46. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ S’ ‘ E’ c upper_c YE Screen
  • 47. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ S’ ‘ E’ c upper_c YE Screen
  • 48. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ S’ ‘ S’ c upper_c YE Screen
  • 49. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ S’ ‘ S’ c upper_c YES Screen
  • 50. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ ’ ‘ S’ c upper_c YES Screen
  • 51. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ ’ ‘ S’ c upper_c YES Screen
  • 52. Low2up – алхмаар
    • while (c != ' ' && c >= 0)
    • {
    • if (c >= 'a' && c <= 'z')
    • upper_c = c - 'a' + 'A';
    • else
    • upper_c = c;
    • /* Print the converted character.*/
    • putchar(upper_c);
    • /* Get the next character */
    • c = getchar();
    • }
    • putchar(' ');
    Buffer ‘ ’ ‘ S’ c upper_c YES Screen
  • 53. Дасгал
    • Оролт :
      • A B бүхэл тоонууд
    • Гаралт :
      • A нь B –г хэдэн удаа агуулж байна вэ
      • Мэдээж үр дүн A/B
    • тайлбар :
      • Хуваах үйлдэл ашиглахгүй !
  • 54. Бодолт
    • #include <stdio.h>
    • int main()
    • {
    • int a, b, res;
    • printf(&quot;Please enter two numbers. &quot;);
    • scanf(&quot;%d%d&quot;, &a, &b);
    • res = 0;
    • while ( (res+1) * b <= a)
    • res = res + 1;
    • printf(&quot;%d / %d = %d&quot;, a, b, res);
    • return 0;
    • }
  • 55. Давталт дахь break
    • break таармагц давталт дуусна .
    • Тэгээд програм давталтын дараагийн алхмыг биелүүлнэ .
    • break давхар давталтын дотор таарвал дотрох давталт дуусна .
  • 56. Жишээ – Энэ юу хийж байна вэ ?
    • #include <stdio.h>
    • int main() {
    • int i=1;
    • printf(&quot;Enter a line of text: &quot;);
    • char c = getchar();
    • while (c != ' ' && c >= 0) {
    • if (c==' ')
    • break;
    • i++;
    • c = getchar();
    • }
    • }
  • 57. Жишээ – үсэг тоолъё
    • #include <stdio.h>
    • int main() {
    • int i=1;
    • printf(&quot;Enter a line of text: &quot;);
    • char c = getchar();
    • while (c != ' ' && c >= 0) {
    • if (c==' ') /* Found the first space - exit the loop */
    • break;
    • i++;
    • c = getchar();
    • }
    • if (c==' ') /* We found a space */
    • printf(&quot;There are %d letters before the first space. &quot;, i-1);
    • else /* The loop ended without finding spaces */
    • printf(&quot;There are no spaces in the input line. &quot;);
    • return 0;
    • }
  • 58. continue
    • continue таарвал дараагийн хэсэг алгасагдна .
    • Тэгээд давталтын эхнээс биелүүлнэ .
  • 59. for давталт
    • for нь тоолуур хувьсагчийн тусламжтай хэрэгжнэ .
    • for (c = begin; c <= end; c += inc)
    • {
    • давталтын бие
    • }
    initialization condition increment
  • 60. For -факториал #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 61. Алхам алхмаар for факториаль --- i 3 n 1 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 62. Алхам алхмаар for факториаль 1 i 3 n 1 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 63. Алхам алхмаар for факториаль 1 i 3 n 1 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 64. Алхам алхмаар for факториаль 2 i 3 n 1 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 65. Алхам алхмаар for факториаль 2 i 3 n 2 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 66. Алхам алхмаар for факториаль 3 i 3 n 2 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 67. Алхам алхмаар for факториаль 3 i 3 n 6 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 68. Алхам алхмаар for факториаль 4 i 3 n 6 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 69. Алхам алхмаар for факториаль 4 i 3 n 6 fact #include <stdio.h> int main() { int i, n, fact = 1; printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;, &n); for (i=1; i<=n; ++i) { fact *= i; } printf(&quot;the factorial is %d &quot;, fact); return 0; }
  • 70. for давталт ( үрг .)
    • Аль ч for –г while –р орлуулж болно.
    • Гэхдээ заримдаа for тохиромжтой .
    • Тодорхой тоогоор давтах бол for -г хэрэглэ
    • Хэзээ дуусах нь тодорхойгүй бол while .
  • 71. Жишээ – fahrenheit-celsius хувиргах хүснэгт
    • /* Print a Fahrenheit-to-Celsius conversion table */
    • #include <stdio.h>
    • int main ( )
    • {
    • int fahr;
    • double celsius;
    • int lower = 0, upper = 300;
    • int step = 20;
    • for (fahr=lower; fahr<=upper; fahr += step)
    • {
    • celsius = 5.0*(fahr -32.0)/9.0;
    • printf(&quot;%d %g &quot;, fahr, celsius);
    • }
    • return 0;
    • }
  • 72. Давхар for давталт тэгш өнцөгт
    • /* Print a rectangle of *. The height and width are defined by the user */
    • #include <stdio.h>
    • int main( )
    • {
    • int i, j;
    • int height, width;
    • printf(&quot;Please enter the two box dimensions: &quot;);
    • scanf(&quot;%d%d&quot;, &height, &width);
    • for (i = 1; i <= height; i++)
    • {
    • for (j = 1; j <= width; j++)
    • printf(&quot;*&quot;);
    • printf(&quot; &quot;);
    • }
    • }
  • 73. Дасгал Өндөр өгөгдөхөд * тэмдэг ашиглан зурагт харагдах пирамидыг хэвлэ . ***** *** ** **** *
  • 74. Бодолт
    • #include <stdio.h>
    • int main()
    • {
    • int i, j, size;
    • printf(“Please enter a size: ”);
    • scanf(“%d”,&size);
    • for (i = 1; i <= size; i++)
    • {
    • for (j = i; j <= size; j++)
    • printf(&quot;*&quot;);
    • printf(&quot; &quot;);
    • }
    • return 0;
    • }
  • 75. Дасгал Өгөгдсөн натурал тоо хүртлэх анхны тоонуудыг хэвлэ
  • 76. Бодолт
    • #include <stdio.h>
    • int main()
    • {
    • int i, j, last;
    • printf(&quot;enter a number &quot;);
    • scanf(&quot;%d&quot;, &last);
    • for (i = 2; i <= last; i++)
    • {
    • for (j = 2 ; j < i; j++)
    • {
    • if (i % j == 0)
    • break ;
    • }
    • if (j == i)
    • printf(&quot;the number %d is prime &quot;, i);
    • }
    • return 0;
    • }
  • 77. Дасгал Өмнөх програмыг уг тооноос бага эсвэл тэнцүү байх хамгийн том анхны тоог хэвлэдэг болгох өөрчил .
  • 78. Бодолт 1
    • #include <stdio.h>
    • int main()
    • {
    • int i, j, last;
    • int found = 0; /* This indicates if we found the largest prime */
    • printf(&quot;enter a number &quot;);
    • scanf(&quot;%d&quot;, &last);
    • i = last;
    • while (!found) /* Loop until we find our guy */
    • {
    • for (j = 2 ; j < i; j++)
    • if (i % j == 0)
    • break;
    • if (j == i) /* If this is true then i is prime */
    • found = 1;
    • else
    • i--;
    • }
    • printf(&quot;The largest prime not larger than %d is %d. &quot;, last, i);
    • return 0;
    • }
  • 79. Бодолт 2 (with break)
    • #include <stdio.h>
    • int main()
    • {
    • int i, j, last;
    • printf(&quot;enter a number &quot;);
    • scanf(&quot;%d&quot;, &last);
    • for (i=last; i>1; i--)
    • {
    • for (j = 2 ; j < i; j++)
    • if (i % j == 0)
    • break ;
    • if (j == i) /* i is prime. We found our guy */
    • break ;
    • }
    • printf(&quot;The largest prime not larger than %d is %d. &quot;, last, i);
    • return 0;
    • }
  • 80. do while loops
    • do {
    • statement(s)
    • } while ( expression );
    • while тай адил
      • Давталтын бие биелсний дараа нөхцөл шалгана
      • Иймд давталт ядаж нэг биелнэ
  • 81. Жишээ зөв оролт хүлээх
    • #include <stdio.h>
    • int main()
    • {
    • int i;
    • printf(&quot;Please enter a positive number. &quot;);
    • do {
    • scanf(&quot;%d&quot;, &i);
    • if (i <= 0)
    • printf(&quot;Try again. &quot;);
    • } while (i<=0);
    • /* The program continues.... */
    • return 0;
    • }