CS201-04 - Introduction to Programming Lecture 4 Key Words...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 4 Key Words of C main if else while do for x = 2 + 4 ; x = 6 ; 6 Memory Memory x = a + b ; a b x #include <iostream.h> main ( ) { int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ; int TotalAge ; int AverageAge ; 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 ; Quadratic Equation In algebra y = ax2 + bx + c In C y = a*x*x + b*x + c a*b%c +d a*(b%c) = a*b%c ? Discriminant b2 - 2a 4c = b*b - 4*a*c /2 *a answer Solution = (b*b - 4*a*c) /(2 *a) Correct answer Incorrect No expression on the left hand side of the assignment Integer division truncates fractional part Liberal use of brackets/parenthesis Interesting Problem Given a four­digit integer, separate and print the digits on the screen Analysis Number = 1234 Take the remainder of the above number after dividing by 10 Eg 1234 / 10 gives remainder 4 1234 % 10 = 4 Remove last digit – 1234/10 = 123.4 (Truncation due to Integer Division) – 123 123 %10 gives 3 Remove last digit – 123/10 = 12.3 (Truncation due to Integer Division) – 12 12 % 10 gives remainder 2 Remove last digit – 12/10 = 1.2 (Truncation due to Integer Division) –1 Final digit remains #include <iostream.h> main ( ) { int number; int digit; cout << “Please enter a 4 digit integer : ”; cin >> number; digit = number %10; cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’ number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit << ‘\n’; number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit << ‘\n’; number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit; } Code Special Character Newline \n ...
View Full Document

This note was uploaded on 01/12/2010 for the course CS CS 201 taught by Professor Dr.naveedmalik during the Spring '09 term at Virtual University of Pakistan.

Ask a homework question - tutors are online