You are on page 1of 15

Lecture-04: Basics Cont

Advance Computer Programming

Advance Computer Programming Lecture-04

Anwar Ghani
Semester: Spring-2013

Department of Physics Faculty of Basic & Applied Sciences


International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Key Words of C

main if else while do for

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Memory

x=2+4; =6;

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Memory
a b

x=a+b;
x

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

#include <iostream.h> #include<conio.h> using namespace std; int 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 ; getch(); Return 0; }
International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Quadratic Equation
In algebra
y = ax2 + bx + c

In C

y = a*x*x + b*x + c

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

a*b%c +d
International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

a*(b%c) = a*b%c

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Discriminant b2 2a 4c
= b*b - 4*a*c /2 *a Solution = (b*b - 4*a*c) /(2 *a) Correct answer Incorrect answer

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

No expression on the left hand side of the assignment Integer division truncates fractional part Liberal use of brackets/parenthesis

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Interesting Problem

Given a four-digit integer, separate and print the digits on the screen

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

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 123 (Truncation due to Integer Division) 123 %10 gives 3 Remove last digit 123/10 = 12.3 12 (Truncation due to Integer Division) 12 % 10 gives remainder 2 Remove last digit 12/10 = 1.2 1 (Truncation due to Integer Division) Final digit remains
International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Code
#include <iostream.h> #include <conio.h> using namespace std; int main(){ int number, digit; cout << "Please enter a 4 digit integer : "; cin >> number; 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 << '\n'; number = number / 10; digit = number % 10; cout <<"The digit is: " << digit; getch(); return 0; }

Program output
Please enter a 4 digit number: 4455

The digit is: 5 The digit is: 5 The digit is: 4 The digit is: 4
// first digit; and then << \n

International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Special Character Newline

\n
International Islamic University, Islamabad

Lecture-04: Basics Cont

Advance Computer Programming

Questions

International Islamic University, Islamabad

You might also like