Sie sind auf Seite 1von 17

Variables, Expressions, Assignments

Sample program:
Convert this equation to C.
x++ + y++ assume: x = 3 y = 5
#include<stdio.h>
main()
{
int ans;
int x = 3;
int y = 5;
ans = x++ + y++;
printf(\nAnswer is %d, ans);
}
Variables, Expressions, Assignments
Sample Program:
Create a program that computes for the value of
age multiplied by 2.
#include<stdio.h>
main()
{
int age = 7;
age = age * 2;
printf(Age is %d, age);
}
Variables, Expressions, Assignments
EXERCISE #4
Create a program having a variable that is
initialized to your current age. Your program should
be able to display your age 5 years from now
Create a program having 3 variables. Two of the
variables is used to initialize the Birth Year and the
Current Year. The third variable is used to compute for
the current age. Your flowchart should display the age
value.
Variables, Expressions, Assignments
#include<stdio.h>
main()
{
int age = 16;
age = age + 5;
printf(Your age 5 yrs from now is %d, age);
}
Variables, Expressions, Assignments
#include<stdio.h>
main()
{
int age = 0;
int Cyr = 2010;
int Byr = 1992;
age = Cyr - Byr;
printf(Your age is %d, age);
}
Constants, Input, Output Commands
#define preprocessor directive defines constants.
A C constant is really the same thing as a literal.
Note that a literal is a data value that doesnt
change, like the number 4 or a string TUP.

Format: #define CONSTANT Value
Example:

#define PI 3.1416
#define NAME Nina Jose
Constants, Input, Output Commands
MORE ON THE CONVERSION OR FORMAT
SPECIFIERS

%c for char variables or constants
%d for int variables or constants
%f for float variables or constants
Constants, Input, Output Commands
Format specifiers in C
Constants, Input, Output Commands
Sample Program to print the value of sample variable
# include <stdio.h>
main()
{
char sample;
sample = B;
printf (The letter is %c., sample);
}
OUTPUT:
The letter is B.
Constants, Input, Output Commands
Sample Program to display the value of sum
#include <stdio.h>
main()
{
float A, B, sum;
A = 14.75;
B = 5.30;
sum = A + B;
printf(SUM: %f.\n, sum);
}
OUTPUT:
SUM: 20.05
Constants, Input, Output Commands
Sample Program that demonstrates an operation
inside printf() function
#include <stdio.h>
main()
{
int X = 2;
int Y = 5;
printf (Result of 2 * 5 is %d., X * Y);
}
OUTPUT:
Result of 2 * 5 is 10.
Constants, Input, Output Commands
EXERCISE # 5
Create a program that computes for the
circumference of a circle. Assume that the value for
radius is 7.23. Your program should be able to show
the value of the circumference.

Additional facts:
The pie value is 3.1416 which is constant and should
never be changed. You can compute the
circumference by using this formula 2R.
The scanf() Function
The purpose of the scanf() function is to input data
to a program. If printf() is for printing formatted
output, then scanf() is for reading formatted input
Example:
scanf(%d, &num);

General Syntax:
scanf(Format_specifier,&variable);
Constants, Input, Output Commands
Format specifiers in C
Constants, Input, Output Commands
Sample Program to convert dollars to pesos
#include <stdio.h>
main()
{
float dollars, pesos;
printf (Enter the no. of dollars you want to
convert: );
scanf (%f, &dollars);
pesos = dollars * 26.95;
printf (\n\n$ %.2f is equal to %.2f pesos.,
dollars, pesos);
}
Constants, Input, Output Commands
EXERCISE # 6
Create a program that computes for the area of a
circle. Assume that the user only knows the value for
diameter. Your program must be able to accept a
value for the diameter and should be able to show
the value of the Area.
Additional facts:
The pi value is 3.1416 which is constant and should
never be changed. You can compute the Area by using
this formula R
2
.
ASSIGNMENT
Create a program that will compute for the Average
score of a student based on three quizzes. The quizzes
are entered by the user. However, the Average may
have a value having decimal places.
Example:
Q1: 98
Q2: 79
Q3: 88
Ave is 83.33

Das könnte Ihnen auch gefallen