Important Notes and Program Example of C Programming for SEE Class 10
Important Notes and Program Example of C Programming for SEE Class 10
https://readersnepal.com/e-notes/neb-new-course-class-12/physics-1
[Note: There are more data type which we will discuss later in need]
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
+ , - , * , / , %
b) Relational operator
c) Assignment operator
= is an assignment operator.
d) Logical operator
For logical AND use &&
For logical OR use ||
For logical NOT use !
Header files in C
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:
This will display anything that is written inside double quotation mark (“ “)
Eg,
int a = 2, b = 3, c;
c = a+b;
printf(“Sum is %d”, c);
[Note that any number of format specifier and variable can be displayed]
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,
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.
[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:
Program flows from top to bottom Conditional Branching: if, if else, else if,
for
sequentially. switch
do
Sequence: Program flows from top to bottom sequentially with out changing
the flow of program execution
#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.
a) if statement
b) if else statement
c) else if ladder
a) if statement syntax:
if (condition)
{
// block of statements;
}
#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;
}
#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;
}
≤ 50 unit Rs 10/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;
}
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.
1. for loop
2. while loop
3. do loop