Introduction to Computer and Programing - Lecture 04

711 views

Published on

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
711
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
35
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to Computer and Programing - Lecture 04

  1. 1. Introduction to Computers and Programming (CSC103) Lecture 04
  2. 2. 2 void main (void) { printf("Testing...n..1n...2n....3n") } Compilation Result? #include <stdio.h> void main (void) { value1 = 50; value2 = 25; int value1, value2, sum; sum = value1 + value2; printf("The sum of %d and %d is %dn", value1, value2, sum); } Compilation Result? 1 2
  3. 3. 3 #include <stdio.h> void main (void) { printf ("Testing...n..1n...2n....3n"); } Output? #include <stdio.h> void main (void) { int value1, value2, sum; value1 = 50; value2 = 25; sum = value1 + value2; printf("The sum of %d and %d is %dn", value1, value2, sum); } Output? 1 2
  4. 4. Arithmetic Instructions in C 4 int ad ; float kot, deta, alpha, beta, gamma; ad = 3200 ; kot = 0.0056 ; deta = alpha * beta / gamma + 3.2 * 2 / 5 ; --------------------------------------------------------------------------------- Here, *, /, -, + are the arithmetic operators. = is the assignment operator. 2, 5 and 3200 are integer constants. 3.2 and 0.0056 are real constants. ad is an integer variable. kot, deta, alpha, beta, gamma are real variables.
  5. 5. Variable Declaration and Use 5 Correct Incorrect float a = 1.5, b = a + 3.1; float b = a + 3.1, a = 1.5; int a, b, c, d ; a = b = c = 10 ; int a = b = c = d = 10; int x, y, z; x = y * z; int x, y, z; y * z = x; Cannot Use aVariable before declaring it!
  6. 6. Integer and Float Conversion 6  Implicit and Explicit Conversions  An arithmetic operation between an integer and integer always yields an integer result.  An operation between a real and real always yields a real result.  An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.
  7. 7. 7 int i ; float b ; i = 3.5 ; b = 30 ;  What would be the value of i and b?  Think of Integer and Float Conversion
  8. 8. 8  k is an integer variable  a is a real/float variable
  9. 9. Receiving User Input  To get the input from the user we use a library function called scanf()  The format of scanf() is; scanf(“<format specifier>”, &<variable name>);  format specifier shows what type of data you want to input or get from user  & before the variable name is must  & is an “Address of Operator”  It gives the location number used by the variable in memory  &num tells the scanf() function at which memory location should it store the value supplied by the user  a blank, a tab or a new line (pressing enter) must separate the values supplied to scanf() Example: scanf(“%d %d %f”, &p,&n,&r); 9
  10. 10. Example #include<stdio.h> void main() { int num; printf(“Enter an Integer: ”); scanf(“%d”, &num); printf(“You entered the number %d”, num); } Output? 10
  11. 11. Example # 2 11 #include<stdio.h> void main() { int x, y; scanf("%d%d",&x,&y); printf(“You have entered x=%d, y=%d",x,y); } Output?
  12. 12. Evaluate yourself! 12  Point out the errors, if any, in the following C statements: (a) int = 314.562 * 150 ; (b) name = ‘Ajay’ ; (c) varchar = ‘3’ ; (d) 3.14 * r * r * h = vol_of_cyl ; (e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ; (f) m_inst = rate of interest * amount in rs ;

×