170 likes | 246 Views
Introduction to Programming. Lecture 4. Key Words of C. main if else while do for. Memory. Memory. x = 2 + 4 ; = 6 ;. Memory. x = 2 + 4 ; = 6 ;. Memory. a. b. x = a + b ;. x. #include< iostream.h > void main() { int x=10; cout <<"the value of x is=";
E N D
Introduction to Programming Lecture 4
Key Words of C • main • if • else • while • do • for
Memory x = 2 + 4 ; = 6 ;
Memory x = 2 + 4 ; = 6 ;
Memory a b x = a + b ; x
#include<iostream.h> void main() { int x=10; cout<<"the value of x is="; cout<<x<<endl; }
#include<iostream.h> void main() { int x; cout<<"enter the value of x"; cin>>x; }
#include<iostream.h> void main() { intx; inty; intz; cout<<"enter the value of x"; cin>>x; cout<<endl; cout<<"enter the value of y"; cin>>y; cout<<endl; z=x+y; cout<<"the answer is"; cout<<z; }
Quadratic Equation • In algebra y = ax2 + bx + c • In C y = a*x*x + b*x + c
a*(b%c) = a*b%c • ?
b2 - 2a 4c = b*b - 4*a*c /2 *a Incorrect answer Solution = (b*b - 4*a*c) /(2 *a) Correct answer
No expression on the left hand side of the assignment • Integer division truncates fractional part • Liberal use of brackets/parenthesis
TASK • Calculate average of the ages of three persons by using cin>>
#include <iostream.h> main ( ) { int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ; intTotalAge ; intAverageAge ; cout << “ Please enter the age of student 1: “ ; cin >> age1 ; cout << “ Please enter the age of student 2: “ ; cin >> age2 ; : : TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 +age10 ; AverageAge = TotalAge / 10 ; cout<< “The average age of the class is :” << AverageAge ; }