Upload
austin-york
View
27
Download
2
Embed Size (px)
DESCRIPTION
Computer Programming Code: CS1112. 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 #include void main ( ) { - PowerPoint PPT Presentation
Citation preview
Lecture No. 3
Bilal Ashfaq Ahmed
TaskCalculate 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 EquationIn algebra
y = ax2 + bx + cIn C++
y = a*x*x + b*x + c
a*b%c +d
Rule of EvaluationIn 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 assignmentInteger division truncates fractional partLiberal use of brackets/parenthesis
Interesting ProblemWrite 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 inputcout <<"\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();}
TaskWrite 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.