You are on page 1of 12

AST 103

Programming with C/C++


Lecture-03: Introduce with C program
Tahmina Akter

Lecturer of Applied Statistics,


Institute of Statistical Research and Training (ISRT),
University of Dhaka, Bangladesh.
Email: takter2@isrt.ac.bd

January, 2018

ogramming with
A simple C program
#i n c l u d e <s t d i o . h>
// A comment
i n t main ( v o i d )
{
p r i n t f ( ” A simple C program \ n” ) ;
return 0;
}

Output:
Dissecting the Code
◮ #include<stdio.h>: it tells the processor to include the
contents of standard input/output header in the
program.
◮ // A comment : // used for define the line as comment
◮ int main (void):
◮ “main” is a program building block called a function.
◮ “int” to the left of main indicates that main “returns”
an integer (whole-number) value.
◮ “void” in parentheses here means that main does not
receive any information.
Dissecting the Code
printf:
◮ instructs the computer to print the character

string.
◮ The escape sequence \n means newline.
Semicolon (;):
◮ terminates a statement, also termed as
statement terminator.
return:
◮ is the exit function.
◮ The value 0 indicates that the program has
terminated successfully.
Simple Tasks
Task 1: Write a program that prints your name.

Task 2: Rewrite the same program with your name should be


printed in multiple lines.
Input and Output
Dissecting the Code II
scanf:
scanf is used to obtain a value from the user.
◮ The %d conversion specifier indicates that the data should be an
integer (the letter d stands for “decimal integer”).
◮ & called the address operator followed by the variable name.

printf :
Printf is defined as before, in addition, the variable name,
preceded by comma(,) after the character string, prints the
corresponding variable value.
Simple Tasks
Task 3.1: Add and subtract two
inputs
Task 3.2: Write a program that give
you the decision that inputted four
values are equal or not
Task 3.3: Write a program for
finding area of circle
Sum of Two Integers
#i n c l u d e < s t d i o . h>
#i n c l u d e < s t d l i b . h>
i n t main ( v o i d )
{
int x;
int y;
int total;
p r i n t f ( ” Enter x\n” ) ;
s c a n f ( ”%d” , &x ) ;
p r i n t f ( ” Enter y\n” ) ;
s c a n f ( ”%d” , &y ) ;
total = x + y;
p r i n t f ( ” Total is %d\n” , total)
re t u r n 0 ;
}
Remainder of 3
#include<stdio.h>
int main()
{
int a;
printf("enter the value");
scanf("%d",&a);
if ((a%3)==0)
{
printf("divisible(n)");
Remainder cont…
}
else if ((a%3)==1)
{
printf("n+1");
}
else
{
printf("n+2");
}
return 0;
}
Homework
Task 3.4: Write a program that asks the
user to enter two numbers, obtain the two
numbers from the user and prints the sum,
product, difference, quotient and remainder
of the two numbers.

You might also like