0% found this document useful (0 votes)
34 views9 pages

Computer Programming Lecture 3

The document discusses C++ code to calculate the average age of a class by prompting the user to enter the ages of 10 students, code for the quadratic formula, rules of evaluation order in expressions using parentheses in C++, and an example of a program that takes a 4-digit number from the user and separates the digits.

Uploaded by

Raja Mustafa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

Computer Programming Lecture 3

The document discusses C++ code to calculate the average age of a class by prompting the user to enter the ages of 10 students, code for the quadratic formula, rules of evaluation order in expressions using parentheses in C++, and an example of a program that takes a 4-digit number from the user and separates the digits.

Uploaded by

Raja Mustafa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lecture No.

3
Bilal Ashfaq Ahmed
Task
Calculate the average age of a class of ten
students. Prompt the user to enter the age of
each student.
#include <iostream.h>
#include<conio.h>
void main ( )
{
int age1, age2, age3, age4, age5, age6, age7, age8, age9,
age10 ;
int TotalAge ;
int AverageAge ;
cout << “ Enter the age of student 1: “ ;
cin >> age1 ;
cout << “ 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();
}
Quadratic Equation
In algebra
y = ax2 + bx + c
In C++
y = a*x*x + b*x + c

 a*b%c +d
Rule of Evaluation
In algebra, there may be curly brackets { } and square brackets
[ ] in an expression but in C we have only parentheses ( ).
Using parentheses, we can make a complex expression easy to
read and understand and can force the order of evaluation. We
have to be very careful while using parentheses, as parentheses
at wrong place can cause an incorrect result.
No expression on the left hand side of the assignment
Integer division truncates fractional part
Liberal use of brackets/parenthesis
Interesting Problem
Write a program that accepts Four digit number from
the user decomposes it into first, second, third and
four digit .
For example if number =1234 then
first=1
Second=2
Third=3
Fourth=4
/* A program that takes a four digits integer from user and shows
the digits on the screen
separately i.e. if user enters 1234, it displays 1,2,3,4 separately. */

#include <iostream.h>
#include<conio.h>
void main ()
{
// declare variables
int num;
int fd, sd, td, frd;
clrscr();
// prompt the user for input
cout <<"\n\t Enter the Four Digit Number: ";
cin >> num;
// Seperation logic

fd = num / 1000;
sd = (num % 1000)/100;
td = (num % 100)/10;
frd= num % 10;
// Display number seperately

cout<<"\n\t First Digit: "<<fd;


cout<<"\n\t Second Digit: "<<sd;
cout<<"\n\t Third Digit: "<<td;
cout<<"\n\t Fourth Digit: "<<frd;

getch();
}
Task
Write a program that takes radius of a circle
from the user and calculates the diameter,
circumference and area of the circle and
display the result.

You might also like