1 / 9

Computer Programming Code: CS1112

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 <iostream.h> #include<conio.h> void main ( ) {

austin-york
Download Presentation

Computer Programming Code: CS1112

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer ProgrammingCode: CS1112 Lecture No. 3 Bilal Ashfaq Ahmed

  2. Task • Calculate the average age of a class of ten students. Prompt the user to enter the age of each student.

  3. #include <iostream.h> #include<conio.h> void main ( ) { int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ; intTotalAge ; intAverageAge ; 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(); }

  4. Quadratic Equation • In algebra y = ax2 + bx + c • In C++ y = a*x*x + b*x + c • a*b%c +d

  5. 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

  6. 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

  7. /* 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; intfd, sd, td, frd; clrscr(); // prompt the user for input cout <<"\n\t Enter the Four Digit Number: "; cin >> num;

  8. // 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(); }

  9. 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.

More Related