0% found this document useful (0 votes)
3 views14 pages

Important Notes and Program Example of C Programming for SEE Class 10

The document provides an overview of C programming, including its history, features, data types, variables, operators, control structures, and sample programs for class 10 students. It emphasizes the importance of C in various applications such as operating systems, web browsers, and database management systems. Additionally, it includes examples of basic C programs demonstrating calculations and control flow using conditional statements.

Uploaded by

himalbest01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Important Notes and Program Example of C Programming for SEE Class 10

The document provides an overview of C programming, including its history, features, data types, variables, operators, control structures, and sample programs for class 10 students. It emphasizes the importance of C in various applications such as operating systems, web browsers, and database management systems. Additionally, it includes examples of basic C programs demonstrating calculations and control flow using conditional statements.

Uploaded by

himalbest01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Important notes and program example of C programming for SEE class 10

https://readersnepal.com/e-notes/neb-new-course-class-12/physics-1

Introduction: C programming is one of the popular and powerful high level


language developed by Denish Ritchie. It was originally developed for
development of UNIX operating system. Later its uses expands to application
development as well as system program development. So, it is also known as
middle level language. In brief,

Some of the implementation of C language are:

1. It is used to develop Operating System eg. UNIX.


2. It is used to develop new programming platform such as C++, C# [C++
is used in Blockchain Technology (Bitcoin, Etherium etc), also C++ is
used in various game engine (Unreal engine and many more)]
3. It is used to develop popular web browser such as Mozilla Firefox,
Thunderbird etc.
4. It is used in development of various GUIs (Graphical User Interface) and
IDEs (Integrated Development Environments).
5. Popular Database Management System (DBMS) like MYSQL was
developed by using C.

Some of the features of C language are:

1. It is a structural programming language. (i.e. it support various control


structure like sequence, branching, looping)
2. It is a modular programming language. (i.e. big program can be reduced
into simpler small program blocks called module)
3. It supports graphics.
4. It has huge library function.
5. It is case-sensitive language.

Data type:C support both numeric as well as alphanumeric datas. Frequently


used Data types in C are:

Type Data type used Format Specifier

Numeric int (for non-decimal numbers) %d

Numeric float (for decimal numbers) %f


Alphanumeric char (for string) %s or %c

[Note: There are more data type which we will discuss later in need]

Variable:Those entities which holds either numeric or alphanumeric values in


program and may change its value through out the time of program execution
are known as variables.

Rule for writing variable:

1. Variable name should not start will number. Eg, 1age is invalid, age1 is
valid
2. Variable name should not have blank space. Eg, first name is invalid,
firstname is valid
3. Keywords cannot be used as variable name. Eg, printf = 2; is invalid,
num = 2; is valid
4. Uppercase variable name are different from lowercase variable name.
Eg, age = 2 is different from AGE = 2

[Note: try to use relevant word as variable name without using any special
symbol]

Operators: The special symbol or sign used to perform some specific function
or operation are called operators.

Types of operators:

a) Arithmetic operator
+ , - , * , / , %

Note that Here, if c = 5/2

The result will be 2 if we initialize ‘c’ as int c.

The result will be 2.5 if we initialize ‘c’ as float c.

If c = 5%2 then result will be 1. Since, % gives remainder after division.

b) Relational operator

> , < , >= , <= , != (not equal to) , == (equal to)


[Note: Here in C language if you want to compare the equal to is referred by
== (two equals)]

c) Assignment operator
= is an assignment operator.

[Note If a = 2 then it mean 2 is assigned to variable a, it does not mean a also


has value 2 and both are equal.]

d) Logical operator
For logical AND use &&
For logical OR use ||
For logical NOT use !

Header files in C

It is a file in C library with .h extension and contains several functions


declaration and definition. Such as we use.
#include for standard input output function eg, printf, scanf etc
#include for mathematical function eg, pow, sin, cos etc
#include for graphical element eg, circle, rectangle, line etc
#include for string handling function eg, strcpy(), strlen(),strupr() etc

And more. [header file can be added according to requirements]

Sample structure of C program:


#include<stdio.h>
void main ( )
{
// block of statements
getch ( );
}

Explanation: C program always execute from top left to right bottom, so


program execution starts from header file which is followed by main( )
function. Remember that main( ) is always executed first. Since, main( ) is a
function it will return some values so we write void in front of main( ) as a
return type which mean null value is returned. getch( ) will hold our output on
the screen unless an until user press some keys. It means get character. All
other statements written inside main( ) function will be executed sequentially.
If you are using void and getch don’t forget to also include conio.h as header
file.
Same program can be written as
#include<stdio.h>
int main( )
{
// block of statements
return 0;
}

Here, since we have written int main( ) The main( ) function should return
integer value so we write return 0 instead of getch( )here. While using int
main( ) and return 0, we don’t need to include conio.h

Output statement:

Output in C program can be displayed by using ‘printf’ statement.

A) In order to display only character we can use following syntax


printf(“Sample text”);

This will display anything that is written inside double quotation mark (“ “)

B) In order to display values of variable we can use following syntax


printf(“format specifier”, list of variables,…);

Eg,
int a = 2, b = 3, c;
c = a+b;
printf(“Sum is %d”, c);

It will display: Sum is c

[Note that any number of format specifier and variable can be displayed]

Same program can be display as


printf(“Sum of %d and %d is %d”, a, b, c);

It will display: Sum of 2 and 3 is 5

[Note: if a, b and c was initialized as float than every %d should be replaced


by %f]
Input statement:

Input in C program can be taken by using ‘scanf’ statement.

In order to take input from user we can use following syntax

scanf(“format specifiers”, &variablename1, &variablename2, ….);

Suppose we want to take a and b as input from user. To take number input we
can write
int a, b;
scanf(“%d %d”, &a, &b);

In this example two numeric variable are initialized as integer. So, we write
two %d inside double quotation followed by &variablename. & denotes
address of that variable.

Another example,

If we want to take principal, time and rate than it can be written as


float p, t, r;
scanf(“%f %f %f”, &p, &t, &r);

In this example three numeric variable are initialized as float. Since, their
values may be in decimal So, we write three %f inside double quotation
followed by &variablename. & denotes address of that variable.

If we want to take character or string as an input than first string variable


should be initialized as follows:
char fname[10];

Remember if we initialized variable as ‘char’ than variable used become


string. Since string in C is array of character we should suffix variable name
with size [size] i.e maximum length of character that the variable can hold.

[Note: While taking string as an input we don’t need to write & in variable
name]

Example
char fname[10], lname[10];
scanf(“%s %s”, fname, lname);
Since variable are initialized as array of character we don’t need to mention &
while using scanf.

Control structure in C:

Since C is a structural programming language, we can change the flow of


program execution according to the requirement of the user. Following are the
control structure used in C.

Control Structure used in C

Sequence Branching Looping

Program flows from top to bottom Conditional Branching: if, if else, else if,
for
sequentially. switch

Unconditional Branching: goto while

do

Sequence: Program flows from top to bottom sequentially with out changing
the flow of program execution

[Note: Don't forget to add header file in each program]

Important C Program example

// WAP to calculate the area of a rectangle in c. [Hints: a = l x b]

#include<stdio.h>
void main()
{
int l, b, a;
printf (“Enter Length: ”);
scanf (“%d”, &l);
printf (“Enter Breadth: “);
scanf (“%d”, &b);
a = l * b;
printf (“The area is %d”, a);
getch();
}
//WAP to calculate the simple interest in c. [Hints: i= (ptr)/100]

#include<stdio.h>
int main()
{
float p, t, r, i;
printf (“Enter principal time and rate: ”);
scanf (“%f %f %f”, &p, &t, &r);
i= (p*t*r)/100;
printf (“The interest is %f”, i);
return 0;
}
//WAP to calculate the average of 3 number in c. [Hints: av= (a+b+c)/3]

#include<stdio.h>
int main()
{
float a, b, c, av;
printf (“Enter 3 numbers: ”);
scanf (“%f %f %f”, &a, &b, &rc);
av= (a+b+c)/3;
printf (“The average is %f”, av);
return 0;
}
//WAP to calculate the volume of cylinder in c. [Hints: v= pi*r*r*h]

#include<stdio.h>
int main()
{
float pi=3.14, r, h, v;
printf (“Enter radius and height: ”);
scanf (“%f %f ”, &r, &h);
v= pi*r*r*h;
printf (“The volume is %f”, v);
return 0;
}
//WAP to convert days into respective years, months and days.

#include<stdio.h>
int main()
{
int days, y, m,d, rd;
printf (“Enter days”);
scanf (“%d”, &days);
y = days/365;
rd = days%365;
m = rd/30;
d = rd%30;
printf (“Year = %d Month = %d Day = %d", y, m, d);
return 0;
}

Branching: Program flows can be changed as per the requirement of the user
with or without condition.

Conditional Branching: Flow of program execution changes according to the


condition supplied by the user.

a) if statement
b) if else statement

c) else if ladder

a) if statement syntax:
if (condition)
{
// block of statements;
}

Program example of if statement [Note: Don't forget to add header file]


#include<stdio.h>
int main()
{
float p;
printf (“Enter percentage ”);
scanf (“%f”, &p);
if (p>=40)
{
printf (“You are Pass");
}
return 0;
}
#include<stdio.h>
int main()
{
int a;
printf (“Enter your age ”);
scanf (“%d”, &a);
if (a>=18)
{
printf (“You are eligible to vote");
}
return 0;
}

b) if else statement: This statement will execute block of statement1 if the


condition is true other wise will execute block of statement2 if the condition is
false. syntax:
if (condition)
{
// block of statements1;
}
else
{
// block of statements2;
}

Program example of if statement [Note: Don't forget to add header file]


#include<stdio.h>
int main()
{
float p;
printf (“Enter percentage ”);
scanf (“%f”, &p);
if (p>=40)
{
printf (“You are Pass");
}
else
{
printf("You are fail");
}
return 0;
}
#include<stdio.h>
int main()
{
int a;
printf (“Enter your age ”);
scanf (“%d”, &a);
if (a>=18)
{
printf (“You are eligible to vote");
}
else
{
printf("You are not eligible to vote");
}
return 0;
}
// Write a program to find greatest among two number

#include<stdio.h>
int main()
{
int a,b;
printf (“Enter two number”);
scanf (“%d %d”, &a, &b);
if (a>b)
{
printf (“%d is greatest", a);
}
else
{
printf (“%d is greatest", b);
}
return 0;
}
// Write a program to check whether given number is odd or even

#include<stdio.h>
int main()
{
int n, r ;
printf (“Enter number”);
scanf (“%d”, &n);
r = n%2;
if (r ==0)
{
printf (“%d is even", n);
}
else
{
printf (“%d is odd", n);
}
return 0;
}
// Write a program to check whether given number is positive or negative

#include<stdio.h>
int main()
{
int n;
printf (“Enter number”);
scanf (“%d”, &n);
if (n>0)
{
printf (“%d is positive", n);
}
else
{
printf (“%d is negative", n);
}
return 0;
}

c) else if ladder This statement will execute block of statement1 when


condition1 is true, similarly will execute block of statement2 when condition2 is
true, like wise block of statement_n will execute when condition_n is true.
Default statement will be executed when none of the condition is true.syntax:
if (condition1)
{
block of statements1;
}
else if (condition2)
{
block of statements2;
}
else if (condition3)
{
block of statements3;
}
.
.
else
{
default statement;
}
Program example of if statement [Note: Don't forget to add header file]
// Write a program to check whether given number is positive, negative or
zero.

#include<stdio.h>
int main()
{
int n;
printf (“Enter number”);
scanf (“%d”, &n);
if (n>0)
{
printf (“%d is positive", n);
}
else if (n<0)
{
printf (“%d is negative", n);
}
else
{
printf("%d is zero", n);
}
return 0;
}
// Write a program to find greatest among three number.

#include<stdio.h>
int main()
{
int a, b, c;
printf (“Enter 3 number”);
scanf (“%d %d %d”, &a, &b, &c);
if (a>b && a>c)
{
printf (“%d is greatest", a);
}
else if (b>a && b>c)
{
printf (“%d is greatest", b);
}
else
{
printf (“%d is greatest", c);
}
return 0;
}
// Write a program to input percentage and check whether he/she secure
distinction, first division, second division, third division or fail.

#include<stdio.h>
int main()
{
float p;
printf (“Enter percentage”);
scanf (“%f”, &p);
if (p>=80)
{
printf (“%f is Distinction", p);
}
else if (p>=60 && p<80)
{
printf (“%f is First division", p);
}
else if (p>=50 && p<60)
{
printf (“%f is Second division", p);
}
else if (p>=40 && p<50)
{
printf (“%f is Third division", p);
}
else
{
printf (“%f is Fail", p);
}
return 0;
}

Q) Calculate Total electricity bill on the basis of following datas.

Unit Consumed Charge per unit

≤ 50 unit Rs 10/unit

>50 and ≤100 Rs 12/unit

>100 Rs 15/unit
#include<stdio.h>
int main()
{
float u, rs;
printf (“Enter unit consumed”);
scanf (“%f”, &u);
if (u<=50)
{
rs = u*10;
printf (“Total amount is %f", rs);
}
else if (u>50 && u<=100)
{
rs = 50*10 + (u-50)*12;
printf (“Total amount is %f", rs);
}
else
{
rs = 50*10 + 50*12 + (u-100)*15;
printf (“Total amount is %f", rs);
}
return 0;
}

Unconditional Branching: Flow of program execution changes without


condition.

a) goto statement syntax


goto label;
block of statements;

label:

Here flow of program execution will go down directly from goto to label without
any condition skipping all the block of statements within.

OR
label:
block of statements;

goto label;

Here flow of program execution will go up directly from goto to label without
any condition.

Suppose we want to take percentage from the user and check whether he/she
is pass or fail keeping pass mark to be 40. Let us make our program will not
accept percentage greater than 100.
#include<stdio.h>
int main()
{
float p;
label:
printf (“Enter percentage ”);
scanf (“%f”, &p);
if (P>100)
{
printf("Please enter value between 0-100");
goto label;
}
if (p>=40)
{
printf (“You are Pass");
}
else
{
printf("You are fail");
}
return 0;
}

In this program example, our program will ask user to input percentage
between 0 to 100. If user input value greater than 100 than it will ask again for
input. This is the use of goto statement in C.

c) Looping in C: The process of repeating same block of statement multiple


number of time as per the requirement of the user is called looping or
iteration. C supports following looping statement.

1. for loop
2. while loop
3. do loop

You might also like