Docsity

Lecture 4: Intro to Programming - Keywords, Memory Allocation, Quadratic Equation, & Digit, Slides of Computer Programming

Partial preview of the text

Download Lecture 4: Intro to Programming - Keywords, Memory Allocation, Quadratic Equation, & Digit and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 4

Key Words of C

 main

 if

 else

 while

 do

 for

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 ;

}

a*b%c +d

a(b%c) = ab%c

 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

Code

#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;

} Docsity.com

Special Character Newline

\n