Introduction to ProgrammingIntroduction to Programming
Lecture 4Lecture 4
Key Words of CKey Words of C mainmain i fi f elseelse whilewhile do do forfor
x = 2 + 4 ;x = 2 + 4 ; = 6 ;= 6 ;
MemoryMemoryxx
66
MemoryMemoryx = a + b ;x = a + b ;
a b
x
#include <iostream.h>#include <iostream.h>main ( )main ( ){{
int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;int TotalAge ;int TotalAge ;
int AverageAge ; int AverageAge ;
cout << “ Please enter the age of student 1: “ ;cout << “ Please enter the age of student 1: “ ;cin >> age1 ;cin >> age1 ;
cout << “ Please enter the age of student 2: “ ;cout << “ Please enter the age of student 2: “ ;cin >> age2 ;cin >> age2 ;::::TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + age10 ;age10 ;AverageAge = TotalAge / 10 ; AverageAge = TotalAge / 10 ;
cout<< “The average age of the class is :” << AverageAge ;cout<< “The average age of the class is :” << AverageAge ;}}
Quadratic EquationQuadratic Equation In algebraIn algebra
y = axy = ax22 + bx + c + bx + c
In C In C y = ay = a**xx**x + bx + b**x + cx + c
a*b%c +d a*b%c +d
a*(b%c) = a*b%ca*(b%c) = a*b%c
?
DiscriminantDiscriminantb2 - 2a
= b*b - 4*a*c /2 *a Incorrect answer
Solution
= (b*b - 4*a*c) /(2 *a) Correct answer
4c
No expression on the left hand No expression on the left hand side of the assignmentside of the assignment
Integer division truncates Integer division truncates fractional partfractional part
Liberal use of Liberal use of brackets/parenthesisbrackets/parenthesis
Interesting ProblemInteresting Problem
Given a four-digit integer, Given a four-digit integer, separate and print the digits separate and print the digits on the screenon the screen
AnalysisAnalysis Number = 1234Number = 1234
Take the remainder of the above number after dividing by 10Take the remainder of the above number after dividing by 10Eg 1234 / 10 gives remainder 4Eg 1234 / 10 gives remainder 41234 % 10 = 41234 % 10 = 4
Remove last digitRemove last digit– 1234/10 = 123.41234/10 = 123.4– 123123 (Truncation due to Integer Division)(Truncation due to Integer Division)
123 %10 gives 3123 %10 gives 3 Remove last digitRemove last digit
– 123/10 = 12.3123/10 = 12.3– 1212 (Truncation due to Integer Division) (Truncation due to Integer Division)
12 % 10 gives remainder 212 % 10 gives remainder 2 Remove last digitRemove last digit
– 12/10 = 1.212/10 = 1.2– 11 (Truncation due to Integer Division) (Truncation due to Integer Division)
Final digit remainsFinal digit remains
CodeCode#include <iostream.h>#include <iostream.h>main ( )main ( ){{
int number;int number;int digit;int digit;cout << “Please enter a 4 digit integer : ”;cout << “Please enter a 4 digit integer : ”;cin >> number;cin >> number;digit = number %10;digit = number %10;cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’number = number / 10;number = number / 10;digit = number % 10;digit = number % 10;cout <<“The digit is: “ << digit << ‘\n’;cout <<“The digit is: “ << digit << ‘\n’;number = number / 10;number = number / 10;digit = number % 10;digit = number % 10;cout <<“The digit is: “ << digit << ‘\n’;cout <<“The digit is: “ << digit << ‘\n’;number = number / 10;number = number / 10;digit = number % 10;digit = number % 10;cout <<“The digit is: “ << digit;cout <<“The digit is: “ << digit;
}}
Special Character Special Character NewlineNewline
\n\n