Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Practical -1

Programming Exercises on Executing and Editing a C Program

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();
}

The output will be:


Enter the weight in kilogram to convert into grams:
15
Equivalent weight in gram = 15000.000000

Note:

• <stdio.h> stands of Standard Input Output.


• ‘printf’ is used for output operation.
• ‘scanf’ is used for input operation.
• \n is used for start the new line.
• Here we are taking the value of kilogram by the user, we can also predefine the value of
kilogram.
• We can also use ‘int’ data type, in that case we can’t get or give the floating values to
the system.
• If we use ‘int’ data type in that case we must have replace the ‘%f’ with the ‘%d’ to run
the program successfully.
Practical - 2

Programming exercises on defining variables and assigning values to variables.

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.

We can declare and assign to a variable in two ways.


1st Way: int a = 18;
2nd Way: int a;
a= 18;

Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 12.2221;
printf(“output = %d”, a );
getch();
}

The output will be:


Output = 12

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

Programming exercises on arithmetic and relational operators.

“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:

Arithmetic operators are used to perform mathematical operations such as addition,


subtraction etc.

Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
int b=6;
printf(" a + b = %d\n", a+b);
getch();
}

The output will be:


a + b = 11

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();
}

The output will be:


a==b=1

The Output is 1 i.e. True.


If we change the value of a or b the value will be false or 0

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2;
int b=4;
printf("a == b = %d\n", a==b);
getch();
}

The output will be:


a == b = 0
Practical – 4

Programming exercises on arithmetic expressions and their evaluation

To perform this exercise open a IDE and code as described…

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();
}

The output will be:


Enter number a
15
Enter number b
6
The sum, substraction, multipication and division of your entered number is :
a + b = 21
a-b=9
a * b = 90
a/b=2

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

Programming exercises using if statement.

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();
}

The output will be:


Enter a number 6
Number is less than 10

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();
}

The output will be:


Enter a number 12
Number is greater than 10

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();
}

The output will be:


value is 9
Practical – 9
Programming exercises on do – while, statement.
How does the do-while loop work?
• First, the body of the do-while loop is executed once. Only then, the test
condition is evaluated.
• If the test condition returns true, the set of instructions inside the body of the loop is
executed again and the test condition is evaluated.
• This looping process goes on until the test condition becomes false.
• If the test condition returns false, then the loop terminates.

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();
}

The output will be:


Please enter a number to start the counting : 7

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
}

Code: Print factorial of a number by using for loop

#include<stdio.h>
#include<conio.h>
void main()
{
int i, num, fact=1;
printf("Input a number : ");
scanf("%d", &num);

for(i=1 ; i<=num ; i++)


fact = fact * i ;

printf("factorial = %d", fact);


getch();
}

The output will be:


Input a number : 5
factorial = 120
Practical – 11
Programs on one-dimensional array.
Rules for declaring one Dimensional Array:
• An array variable must be declared before being used in a program.
• The declaration must have a data type, variable name and subscript.
• The subscript represents the size of the array. If the size is declared as 10 programmers
can store 10 elements.
• An array index always starts from 0. For example, if an array variable is declared as
s[10], then it ranges from 0 to 9.
• Each array element stored in a separate memory location.

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();
}

The output will be:


---Students marks details---
s1 = 89
s2 = 76
s3 = 98
s4 = 91
s5 = 84

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]

You might also like