Lecture 1
Lecture 1
Lecture 1
system("pause") - it will pause a program before it exits. This pause is very useful when
your IDE won't wait as you test a program and as soon as the program finished the
window closes taking all your data with it.
C Compilers
When you write any program in C language then to run that program you
need to compile that program using a C Compiler which converts your
program into a language understandable by a computer. This is called
machine language (ie. binary format).
C supports various data types such as float, int, char, etc., for storing data.
Input Formatting:
scanf("%d", &num1);
Logical Operators
&& And
|| Or
Exercise 1:
Write a program that will get the sum two unique numbers.
Think First! What are the things (variables) that we need for we to get
the sum of the two numbers?
Algorithm:
Input: Enter Two Numbers (num1, num2)
Process: Compute the sum (sum = num1 + num2)
Output: Display the sum (sum)
Exercise 2:
Write a program that will calculate the average of 4 numbers.
Think First! What are the things (variables) that we need for we to get
the average of four numbers
Algorithm:
Input: Enter four numbers.
Process: Compute the Average (Average = num1 + num2 + num3 + num4 / 4)
Output: Display the Average (Average)
The Conditional Statements
The if-else statement is used to express decisions
Syntax
if (condition)
{
statement;
}
else
{
statement;
}
Example1:
Write a program that determines if the input age is qualified to vote or
not. The qualifying age is 18 years old and above.
Think First! What are the things (variables) to determine if the user input
is Qualified to Vote or Not?
Algorithm:
Enter Age:
if (age >=18)
printf("Qualified to Vote");
else
printf("Too Young");
Example1:
Write a program that determines if the input number is POSITIVE or
NEGATIVE.
Think First! What are the things (variables) to determine if the inputted
number is a POSITIVE or NEGATIVE number.
Algorithm:
Enter Number (num1):
if (num1 >= 0)
printf(“Positive");
else
printf(“Negative");