C Practical File
C Practical File
C Practical File
To perform this practical open the IDE (Integrated Development Environment) and code as
described:
Code:
#include<stdio.h>
#include<conio.h>
void man()
{
float kg, g;
printf(“enter the weight in kilogram to convert into grams : \n”);
scanf(“%f”, & kg);
g = kg * 1000;
printf(“equivalent weight in gram = %f”, g);
getch();
}
Note:
Naming a Variable:
A variable name can be of anything we want to call out variable. Yet there are specific rules
we must follow while naming a variable:
• A variable name can contain alphabets, digits and underscore (_) only.
• The starting letter can’t be a digit.
• White Spaces can’t be used.
• The name should not be Reserved Keyword or Special character.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 12.2221;
printf(“output = %d”, a );
getch();
}
Declaration:
We can’t declare a variable without specifying its data type. The data type of a variable
depends on what we want to store in the variable and how much space we want it to hold.
Practical – 3
“Special symbols that are used to perform action or operations are known as operators.”
For example, the symbol plus (+) is used to perform addition so it is an operator.
Arithmetic Operators:
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
int b=6;
printf(" a + b = %d\n", a+b);
getch();
}
Relational Operators:
Relational operators are used for the comparison between two or more numbers. Same as
Java, C has also six relational operators and their value is in Boolean i.e. either True or False
(1 or 0).
Code :
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2;
int b=2;
printf(" a == b = %d\n ", a==b);
getch();
}
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2;
int b=4;
printf("a == b = %d\n", a==b);
getch();
}
Code :
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter number a \n");
scanf("%d", &a);
printf("enter number b \n");
scanf("%d", &b);
printf("the sum, substraction, multipication and division of your entered number is : \n");
printf("a + b = %d\n", a+b);
printf("a + b = %d\n", a-b);
printf("a + b = %d\n", a*b);
printf("a + b = %d\n", a/b);
getch();
}
Note: In this program we are taking the values of a & b from the user. We can also predefine
them
Practical – 5
Programming exercises on formatting input/output using printf and scanf and their return type
values.
printf function:
The printf()function is used for printing the output. It returns the number of characters that are
printed. If there is some error then it returns a negative value.
A program that demonstrates this is as follows:
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
char str[] ="The sky is blue";
printf("The value returned by printf() for the above string is : %s", str);
getch();
}
scanf function:
The scanf() function is used for obtaining the input from the user. It return the number of input
values that are scanned. If there is some input failure or error then it return EOF (end-of-file). A
program that demonstrates this is as follows:
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int x, y, z;
printf("The value returned by the scanf() function is : ");
scanf("%d%d%d", &x, &y, &z);
printf("\nx = %d", x);
printf("\ny = %d", y);
printf("\nz = %d", z);
getch();
}
The output will be:
The value returned by the scanf() function is : 10 20 30
x = 10
y = 20
z = 30
Practical – 6
Use:
To specify the conditions under which a statement or group of statement should be
executed.
Syntax:
if (Boolean-expression)
statement;
Action:
If the Boolean expression is true, the specified statement is executed; otherwise it is not. In
either case, execution continues with the next statement in the program.
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Input a number : ");
scanf("%d", &num);
if(num < = 10)
printf("Number is less than 10 \n");
getch();
}
Note: if the user enter a number that is less than 10, then only that condition the
program will print Number is less than 10 otherwise the program will be completed without
printing anything.
Practical – 7
Programming exercises using if – Else.
Use:
To choose exactly one out of two statements (possibly compound statements) to be executed;
specifies the conditions under which the first statement is to be executed and provides alternative
statement to execute if these conditions are not met.
Syntax:
if (Boolean-expression)
statement-1;
else
statement-2;
Action:
If the Boolean expression is true, statement-1 will execute and statement-2 is skipped; otherwise
statement-1 is skipped and statement-2 is executed. In either case, execution continues with
the next statement in the program.
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Input a number : ");
scanf("%d", &num);
if(num < = 10)
printf("Number is less than 10 \n");
else
printf("Number is greater than 10 \n");
getch();
}
Note: Here the user has entered ’12’ as the input, which is greater than 10 that’s why the
condition has become false for the if statement. And it jumped to the else statement and
executed completely.
Practical – 8
Programming exercises on switch statement.
The control statement that allows us to make a decision effectively from the number of
choices is called a switch, or a switch case-default since these keywords go together
to make up the control statement. The expression in switch returns an integral value,
which is then compared with the different cases. Switch executes that block of code, which
matches the case value. If the value does not match with any of the cases, then the default
block is executed.
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int i=9;
switch(i)
{
case 5:
printf("value is 7");
break;
case 0:
printf("value is 8");
break;
case 9:
printf("value is 9");
break;
default:
printf("value is not present");
}
getch();
}
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int num, i=1;
printf("please enter a number to start the counting : ");
scanf("%d", &num);
do
{
printf("%d\n", i);
i++;
} while (i <= num);
getch();
}
1
2
3
4
5
6
7
Practical – 10
Programming exercises on for – statement.
The “For” Loop is used to repeat a specific code until a specific condition is satisfied. The
for-loop statement is very specialized. We use for a loop when we know the number
of iterations we want, whereas when we do not know about the number of iterations, we
use while loop. Here is the syntax of the loop in C programming.
Syntax:
for ( initialize counter ; test counter ; increment/decrement counter)
{
//set of statements
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num, fact=1;
printf("Input a number : ");
scanf("%d", &num);
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int s[5] = {89, 76, 98, 91, 84}, i ;
printf("\n---Students marks details--- ");
for(i = 0; i < 5; i++)
{
printf("\n s %d = %d \n", i + 1, s[i]);
}
getch();
}
Note: The above program illustrates that the declaration and initialization of one-
dimensional array. The first element of an array s[0]. The last element of an array is a[4]