1 / 26

Chapter 4 Last part

Chapter 4 Last part. Quiz. Print the integers from -7 to +7, one per line. -7 to + 7 Solution. #include &lt; stdio.h &gt; int main() { int j; for (j= -7; j&lt;=7; j++ ) { printf (&quot;%3d<br>&quot;, j); } system(&quot;PAUSE&quot;); return 0; }. What will be printed?. #include &lt; stdio.h &gt;

blenda
Download Presentation

Chapter 4 Last part

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Last part

  2. Quiz • Print the integers from -7 to +7, one per line.

  3. -7 to +7 Solution #include <stdio.h> int main() { int j; for (j= -7; j<=7; j++ ) { printf("%3d\n", j); } system("PAUSE"); return 0; }

  4. What will be printed? • #include <stdio.h> • main(){ • inti=1; • for(i=1;i;){ • printf("The value of I = %d:\n",i); • i++; • if((i%10)==0){ • i=(!i); • } • } • system("pause"); • return 0; • }

  5. What? #include <stdio.h> int main() { int sum = 0, /* Current sum */ num; /* Number to be read */ printf ( "Please enter the integers:\n" ); for ( scanf ( "%d", &num ); num; scanf ( "%d", &num ) ) sum += num; printf ( "The sum of the numbers is %d\n", sum ); system("pause"); return ( 0 ); }

  6. GCD Solution //Greatest common divisor /* To compute gcd(48,18), divide 48 by 18 to get a quotient of 2 and a remainder of 12. Then divide 18 by 12 to get a quotient of 1 and a remainder of 6. Then divide 12 by 6 to get a remainder of 0, which means that 6 is the gcd.*/ #include <stdio.h> int main(){ int a=12; int b=8; // test case : 48,18 =6 // test case : 12,8 = 4 while(b){ int temp =b; //b is smaller b=a%b; a=temp; } printf("%d \n",a); system("pause"); return 0; }

  7. "unsigned integer" and "signed integer" • All integer data types are signed data types, i.e. they have values which can be positive or negative.

  8. Bits - Signed number representations  Hence in a byte with only 7 bits (apart from the sign bit), the magnitude can range from 0000000 (0) to 1111111 (127). Thus you can represent numbers from −12710 to +12710 once you add the sign bit (the eighth bit). Decimal/Binary Conversion Tool: http://acc6.its.brooklyn.cuny.edu/~gurwitz/core5/nav2tool.html

  9. Integer Overflow • Integer Overflows are arithmetic errors. Integers have finite ranges in computers. •  For 32-bit signed integers, the minimum value is 0x80000000 (-2147483648) and the maximum value is 0x7fffffff (2147483647)

  10. Integer Overflow • #include <stdio.h> • int main ( void ) { • inti=2147483647; • printf ("% d\n" , i+1); • system("pause"); • return 0; • }

  11. What will be printed ? #include <stdio.h> int main(){ int a=0, b=0; a = 3; printf("Value of A :%d\n",a); b = a++; printf("Value of B :%d\n",b); system("pause"); return 0; }

  12. Increment and Decrement Operators • The unusual feature of `++' and `--' is that they can be used either before or after a variable. • The value of ++k is the value of k after it has been incremented. The value of k++ is kbefore it is incremented. • Suppose k is 5. Then • x = ++k; • Increments k to 6 and then sets x to the resulting value, i.e., to 6. But • x = k++;

  13. Increment and Decrement Operators • x = k++; • First sets x to to 5, and then increments k to 6. • The incrementing effect of ++k and k++ is the same, but their values are respectively 5 and 6.

  14. The First 1,000 Primes • A prime number is a positive integer, which is divisible on 1 and itself.  • No even number can be a Primeexcept 2 • We only need to check the divisibility of number until square root of number (we call this sqrt(number)) • We need to check if number is even. If it is, then is not prime. 4. We need to keep count of Primary Numbers Found

  15. Assignment-divide-by-0 error • With the continuestatement, you can avoid a divide-by-0 error without ending the loop completely. • Write a program to divide 4000by -4 to number 10 , except 0;

  16. Assignment-divide-by-0 error Output • 4000 divided by -4 is... -1000 • 4000 divided by -3 is... -1333.33333333 • 4000 divided by -2 is... -2000 • 4000 divided by -1 is... -4000 • 4000 divided by 1 is... 4000 • 4000 divided by 2 is... 2000 • 4000 divided by 3 is... 1333.33333333 • 4000 divided by 4 is... 1000 • 4000 divided by 5 is... 800 • 4000 divided by 6 is... 666.666666667 • 4000 divided by 7 is... 571.428571429 • 4000 divided by 8 is... 500 • 4000 divided by 9 is... 444.44444444444 • 4000 divided by 10 is... 400

  17. Prime numbers and cryptography (RSA) • To decode the secret message, you must first factor the number 5192...4257 of the public key into a product of primes, just as 39 factors as 3 x 13.

  18. Theprime factors of a positive integer • The prime factors of a positive integer are the prime numbers that divide that integer exactly. • As you can see, every factor is a prime number

  19. The prime factors of a positive integer

  20. Example 1: What are the prime factors of 12 ? • It is best to start working from the smallest prime number, which is 2, so let's check: • 12 ÷ 2 = 6 • Yes, it divided evenly by 2. We have taken the first step! • But 6 is not a prime number, so we need to go further. Let's try 2 again: • 6 ÷ 2 = 3 • Yes, that worked also. And 3 is a prime number, so we have the answer: • 12 = 2 × 2 × 3 • As you can see, every factor is a prime number, so the answer must be right.

  21. What is the prime factorization of 17 ? • Hang on ... 17 is a Prime Number. • So that is as far as we can go. • 17 = 17

  22. Prime Factorization Code #include <stdio.h> int main(){ inti; int n=288; for ( i = 2; i <= n; i++) { while (n % i == 0) { printf("%d ,",i); n /= i; } } system("pause"); return 0; }

More Related