Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 40

PROBLEM SOVLING

THROUGH C
PREPARED BY
PAVETHRA MANIVEL, AP/CSE
MY FIRST C PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
printf("Hello World");
}
PROGRAM EXPLANATION
• # INCLUDE is a preprocessor directive which
tells the compiler how to process the source
code.
• STDIO stands for Standard Input / Output
• H – Extension of Header file.
• Header file – The file that tells the compiler
how to call some functionality (without
knowing how the functionality actually
works).
PROCESSING OF SOURCE CODE

HIGH LEVEL
ALGORITHM COMPILER
PROGRAM

MACHINE LEVEL
RESULT MEMORY
PROGRAM
EXAMPLE 2
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
print(a)
}
EXPLANATION
• In this example, we have a statement
something called a = 5.
• Any programming language accepts values in
terms of variables.
• Variables can simply be called as an envelope
cover, where the envelope cover will contain a
letter.
HOW DOES THE COMPILER IDENTIFIES
A VARIABLE
• The so called compiler provides a location
which is technically called “memory space” or
“address” for each and every variable in the
source code.
• Consider the following set of statements:
a=5
b = 10
c = 25
HOW DOES THE COMPILER IDENTIFIES
A VARIABLE

Address 0
Address 1
A Address 2
Address 3
Address 4
Address 5
B
Address 6
Address 7
C Address n
C TOKENS
• Tokens is an object that represents something
else.
• C Tokens are building blocks which are
assembled together to write a C program.
• Each and every small individual unit in C
program is called as tokens.
C TOKENS
• The following are the C Tokens:
– Keywords
– Variables.
– Constants
– Operators
STATEMENTS
• Statements can be defined as a set of declaration
or sequence of action. Statement causes the
computer to perform some task or activity.
Following are the type of statements.
• Assignment statements.
• Null statements.
• Block of statements.
• Expression statements.
STATEMENTS
• A statement which uses assignment operator to
assign values to variables.
Example
basic = 20000;

Null Statements
A Statement without any characters and it is only
semicolon is called null statement.
Example
; (Null Statement)
STATEMENT
Block of statements
Block contains several statements that are enclosed
within a pair of curly braces { }.
These can be any expression, assignments and keywords.
Example
{
int a = 20;
float b = 34.34;
printf(“%d”, a);
printf(“%f”,b);
}
STATEMENTS
Expression Statements
Expression is a combination of operands and
operators. It can consist of arithmetic, relational
or logical expressions.
Example
a=10
b=20
a>b
DELIMITERS
Symbol Name Meaning
# Hash Preprocessor Directives.
, Comma Variable delimeters to
separate list of variables.
: Colon Label delimeter
; Semicolon Statement delimIter
() Paranthesis Used in expression or in
function.
{} Curly braces Used for block
statements.
[] Square brackets Used in array.
MAIN FUNCTION

input input

MAIN ( )

output
MAIN FUNCTION – SUB FUNCTION

input input
MAIN ()

SUB 1

SUB 2

output
INPUT / OUTPUT
• Consider the following scenario:
Mam: What is your name?
Student: My name is Ananya
Mam: What is your previous class percentage?
Student: I have secured 90%
The above scenario happens in our daily day to day
life. Now the question how am I going to ask a
question, provide the answer (input) and say the
answer (display the output) in terms of the C
Language .
INPUT / OUTPUT
• Input Function
• The scanf( ) stands for scan formatting and it is
used to read formatted data from they keyboard.
The scanf function takes a text stream from the
keyboard, extracts and formats data from the
stream according to a format control string and
then stores the data in specified program
variables.
Syntax
scanf(“control string”, &var1, &var2, …. , &varn);
INPUT / OUTPUT
Format Code / Meaning
Control String
%c Single Character

%d Decimal Integer

%f Floating point values

%s Strings
SCANF - RULES
• Rule 1: The scanf function works until:
• The maximum number of characters has been processed.
• A white space character is encountered
• An error is detected.
• Rule 2: Every variable that has to be processed must have a conversion
specification associated with it. Therefore, the following scanf( ) statement
will result in error has no conversion specification associated with it.
scanf(%d %d”, &num1, &num2, &num3).
In the above statement, num3 has no conversion specification associated
with it.
• Rule 3: An error will be generated if the format string is ended with a
white space character.
• Rule 4: The value entered by the programmer must match the character
specified in the control string.
• Rule 5: Input data values must be separated by spaces.
PRINTF()
• The printf( ) function stands for print formatting. This function is used to
display information required by the user and also prints the values of the
variables.
• The printf( ) takes data values, converts them to a text stream using
formatting specification in the control string and passes the resulting text
stream to output device.
• The control string may contain zero or more conversion specifications,
textual data and control characters to be displayed.
Syntax
printf(“control string”, var1,&var2, …. ,&varn);
Rules for writing printf( )
• The variable must be separated by commas and need not be preceded
with ‘&’ sign.
• The control string and the variables must match in their order.
• Control string must be in quotations.
EXAMPLE 3
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
printf("Enter the age of the student");
scanf("%d",&age);
printf("The entered age is",age);
}
COMPARISON WITH C++ LANGUAGE
• Mostly the syntax of C programming is followed in C++ language
also.
• C++ follows object oriented principles.
• Rules of using a variable, constant, delimiters, data types are same
as C programming.
• Everything in C++ is associated with classes and objects, along with
its attributes and methods. For example: in real life, a car is
an object. The car has attributes, such as weight and color,
and methods, such as drive and brake.
• Attributes and methods are basically variables and functions that
belong to the class. These are often referred to as "class members".
• A class is a user-defined data type that we can use in our program,
and it works as an object constructor, or a "blueprint" for creating
objects.
INPUT / OUTPUT IN C++
Output Stream:
The Output Streams allow us to perform write operations on output
devices such as screen, disk etc., It is performed using the cout object.
The syntax for the standard output stream is as follows:

cout << variable

OBJECT INSERTION OR PUT


TO OPERATOR
INPUT / OUTPUT IN C++
• Input Stream:
The input streams allow us to perform read
operation with input devices such as
keyboard. It is performed using the cin object.
The syntax for the standard input stream is as
follows:
cin >> variable

OBJECT EXTRACTION OR GET


OPERATOR
MY FIRST C++ PROGRAM
#include<iostream.h>
void main()
{
cout<<"Welcome to India";
}
STRUCTURE OF C++ PROGRAM
• C++ Headers
• Class Definition
• Member functions Definition
• Main Function
CONTROL STATEMENTS
• Control statements enable us to specify the
flow of program control i.e., the order in
which the instructions in a program must be
executed. Control statements makes possible
to make decisions, to perform tasks
repeatedly or to jump from one section up
code to another.
CONTROL STATEMENTS
• C Program supports us with the following
decision making statements. They are as
follows:
• if statement
• if – else statement
• if –else if – else statement
• nested if statement
• switch …. case statement
CONTROL STATEMENTS
if statement
Generally, if statement is the simplest of all
decision making constructs. It is a single way
decision making statement. It does not have
alternative solution if the condition fails.
Syntax:
if (condition)
statements
CONTROL STATEMENTS
ASSIGNMENT 1
WRITE A C PROGRAM TO INPUT AN AGE; CHECK
WHETHER THE AGE IS A VALID AGE
CONTROL STATEMENTS
if else statement
The if else statement is a two way decision making statement where we
can have an alternate solution if the condition fails. The condition is
evaluated and if the condition is true, the statements inside the if block is
executed. If the condition is false, then the statements inside the else
block is executed.
Syntax
if (condition)
{
statement 1;
}
else
{
statement 2;
}
CONTROL STATEMENTS
ASSIGNMENT 2
Write a c program to input an age; check whether the age is a
valid age. Print both the cases of the output.

ASSIGNMENT 3
Write a C program to enter any character. If the entered
character is in lower case then convert it into upper case and
if it is a lower case character convert it into lower case
HINT:
1. To convert upper case into lower case perform ch+32.
2. To convert lower case into upper case perform ch – 32.
ch is the input variable.
CONTROL STATEMENTS
C language supports if – else – if statements to test additional conditions from the initial
condition. i.e, If we have multiple choices as output we will be having a chained condition to
check and display the output.
Syntax
if (condition)
{
statement 1;
}
else if (condition)
{
statement 2;
}
else
{
Statement 3;
}
CONTROL STATEMENTS
ASSIGNMENT 4
A company decides to give bonus to all its
employees on Diwali. A 5% bonus on salary is given
to the male workers and 10% bonus on salary is
given to the female workers. Write a program to
enter the salary and gender of the employee. If the
salary of the employee is less than Rs.10000/- then
the employee gets 2% bonus on salary. Calculate
the bonus that has to be given to the employee and
display the salary that the employee will get.
CONTROL STATEMENTS
ASSIGNMENT 4
HINT:
Input: salary, bonus, tot_sal_paid and gender.
Condition: if gender = = ‘m’ then perform bonus
= 0.05 * sal;
Similarly check for female gender.
Check whether the salary is < 10000.
Finally display the output.
CONTROL STATEMENTS – SWITCH
STATEMENT
• A switch case statement is a multi-way decision statement that is a
simplified version of an if –else block that evaluates only one
variable. In switch case, the statement blocks refer to the statement
lists that may contain zero or more statements. These statements in
the block are not enclosed within opening and closing braces.
• Switch statements are mostly used in the following situations:
– When there is only one variable to evaluate in the expression.
– When many conditions are being tested for.
• Switch statements are used as an alternative to long if statements
that compare a variable to several integral values.
• In the syntax of switch statement, a keyword called break is used.
The break statement is used at the end of each case. If the break
statement is not used, then the case that matched the following
cases will be executed.
CONTROL STATEMENTS – SWITCH
STATEMENT
• Rules for writing switch statement
• The expression in switch statement must be
an integer value or character constant.
• Each ‘case’ block and ‘default’ block must be
terminated with break statement.
• The case keyword must terminate with :
(colon).
CONTROL STATEMENTS – SWITCH
STATEMENT
switch(expression)
{
case 1:
stmt;
break;
case 2:
stmt;
break;
case n:
stmt;
break;
default:
break;
}

You might also like