You are on page 1of 42

CP LAB MANUAL

Introduction to C

C is a general purpose, structured programming language. C resembles other high-


level structured programming languages. C also contains additional futures,
however that allow it to be used at a lower level, thus bridges the gap between
machine language and high-level languages.

C has relatively small instruction set.


It has large number of library functions.
It allows user to write additional library functions.
C compilers are commonly available for computers of all sizes.
C programs are highly portable.

History of C

C is programming language developed at AT & Ts Bell laboratories of USA in


1972.It was designed and written by a man named Dennis Ritchie.
Various stages of evaluation of C:

Year Language Developed By Remarks


1960 ALGOL International committee Too general, too abstract
1963 CPL Cambridge University Hard to learn, difficult to
implement
1967 BCPL Martin Richards at Could deal with only
specific
Cambridge University problems
1970 B Ken Thompson at AT & T Could deal with only
specific
problems
1972 C Dennis Ritchie at AT & T Lost generality of BCPL
and B
restored

Hardware requirement for C

C can be run on PC, PC/XT, PC/AT, PC/AT386, PC/AT486 or any latest PC.

Software requirements for C

You should use MS-DOS operating system, or greater. You will need any C
compiler like Borland C, Turbo C etc.

OM Institute of Engg. & Technology Junagadh 1


CP LAB MANUAL

Flow Chart
Symbol Name Purpose

It represents the beginning or the end of


Terminal
program.

Processing It represents calculation, initializing and other


processing function.

Input / Output It is used to represents the input/output in the


program

Decision It represents a decision, logical, arithmetical


or comparison and depicts the alternate paths
to be followed based on decision.

This indicates, in a long flowchart how


Onpage Connector
various branches or parts are connected

Flow It indicates the direction of program flow.

OM Institute of Engg. & Technology Junagadh 2


CP LAB MANUAL

Algorithm
In order to solve a problem using a computer it is necessary to develop step by step
method. Algorithm is nothing but a sequence of precise and unambiguous instructions
to solve a problem in a finite number of operations.

Program: Program to calculate area of a circle, get radius from the user.

Algorithm:
Step 1: Read radius.
Step 2: Let Area = 3.14 * radius * radius.
Step 3: Display Area.
Step 4: Stop.

Flow chart:
Start

Input

Area = 3.14 * r*r

Area

Stop

OM Institute of Engg. & Technology Junagadh 3


CP LAB MANUAL

1.2 Draw flowchart to find maximum number from two numbers.

OM Institute of Engg. & Technology Junagadh 4


CP LAB MANUAL

1.3 Draw flochart to find sum of 10 numbers.

OM Institute of Engg. & Technology Junagadh 5


CP LAB MANUAL

1.4 Draw flochart to multiply given three numbers.

OM Institute of Engg. & Technology Junagadh 6


CP LAB MANUAL

1.5 Write an algorithm to find average of three numbers.

OM Institute of Engg. & Technology Junagadh 7


CP LAB MANUAL

1.6 Write an algorithm to find whether given number is positive or negative.

OM Institute of Engg. & Technology Junagadh 8


CP LAB MANUAL

1.7 Write an algorithm to find out minimum number from two numbers.

OM Institute of Engg. & Technology Junagadh 9


CP LAB MANUAL

1.8 Write an algorithm to find perimeter of circle.

OM Institute of Engg. & Technology Junagadh 10


CP LAB MANUAL

Unit 2

How to Create a New File?


Step 1: Open Terminal and write
gedit fileName.c
How to Save the File?
Step 1: ctrl+s
How to Compile a program?
Step 1: gcc fileName.c or gcc -o fileName fileName.c
How to Run a program?
Step 1: ./a.out or ./fileName
How to Open an existing File?
Step 1: gedit filename.c

OM Institute of Engg. & Technology Junagadh 11


CP LAB MANUAL

Write a C program to print your college name.

collegename.c

#include<stdio.h>
int main()
{
printf("OM Institute of Engineering & Technology \n");
printf("Junagadh");
}

To Compile:
gcc -o collegename collegename.c

To Run:
./collegename

Output:
OM Institute of Engineering & Technology
Junagadh

OM Institute of Engg. & Technology Junagadh 12


CP LAB MANUAL

2.1 Write a program to print your name & address.

OM Institute of Engg. & Technology Junagadh 13


CP LAB MANUAL

2.2 Write a program to add given three numbers.

#include<stdio.h>
void main()
{
int no1,no2,no3,sum;
printf("Enter the value of no1::");
scanf("%d",&no1);
printf("Enter the value of no2::");
scanf("%d",&no2);
printf("Enter the value of no3::");
scanf("%d",&no3);
sum=no1+no2+no3;
printf(Sum of three Number::%d,sum);
}

Output:
Enter the value of no1:: 10
Enter the value of no2:: 20
Enter the value of no3:: 30
Sum of three Number:: 60

OM Institute of Engg. & Technology Junagadh 14


CP LAB MANUAL

2.3 Write a program to find area of circle.

OM Institute of Engg. & Technology Junagadh 15


CP LAB MANUAL

2.4 Write a program to perform addition,subtraction,multiplication & division.

OM Institute of Engg. & Technology Junagadh 16


CP LAB MANUAL

2.5 Write a program to find the area of rectangle.

OM Institute of Engg. & Technology Junagadh 17


CP LAB MANUAL

Unit:3
3.1 Write a c program to demonstrate various datatypes.

#include <stdio.h>
int main()
{
int integerType;
float floatType;
double doubleType;
char charType;

// Sizeof operator is used to evaluate the size of a variable


printf("Size of int: %ld bytes\n",sizeof(integerType));
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));

return 0;
}

Output:

Size of int: 4 bytes


Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

OM Institute of Engg. & Technology Junagadh 18


CP LAB MANUAL

3.2 Write a program to find the average from given three numbers.

OM Institute of Engg. & Technology Junagadh 19


CP LAB MANUAL

3.3 Write a program to swap value of two numbers without using third variable.

#include <stdio.h>
int main()
{
int x = 10, y = 5;

// Code to swap 'x' and 'y'


x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5

printf("After Swapping: x = %d, y = %d", x, y);


}

Output:
After Swapping: x = 5, y = 10

OM Institute of Engg. & Technology Junagadh 20


CP LAB MANUAL

3.4 Write a program which will calculate y=xn making use of a valid library
function.

OM Institute of Engg. & Technology Junagadh 21


CP LAB MANUAL

Unit: 4
4.1 Write a program to find whether given number is odd or even.

#include <stdio.h>
int main()
{
int number;

printf("Enter an integer: ");


scanf("%d",&number);

// True if the number is perfectly divisible by 2


if(number%2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);

return 0;
}

Output:
Enter an integer: 7
7 is odd.

OM Institute of Engg. & Technology Junagadh 22


CP LAB MANUAL

4.2 Write a program to find whether given number is positive,negative or zero.

OM Institute of Engg. & Technology Junagadh 23


CP LAB MANUAL

4.3 Write a program to find maximum from two numbers.

OM Institute of Engg. & Technology Junagadh 24


CP LAB MANUAL

4.4 Write a program to find maximum from three numbers.

OM Institute of Engg. & Technology Junagadh 25


CP LAB MANUAL

4.5 Write a program to check whether input year is leap year or not.

OM Institute of Engg. & Technology Junagadh 26


CP LAB MANUAL

4.6 Write a program to enter days and convert it into month and days.

OM Institute of Engg. & Technology Junagadh 27


CP LAB MANUAL

4.7 Write a program to display crud operation using switch case statement.

OM Institute of Engg. & Technology Junagadh 28


CP LAB MANUAL

4.8 Write a program to print first 10 even numbers.

#include <stdio.h>
void main ()
{
int i;
printf(" Program to print out the first 10 even numbers \n");
for (i=0; i<=20; i+=2)
{
printf (" %d ",i);
}

}
Output:
Program to print out the first 10 even numbers
0 2 4 6 8 10 12 14 16 18 20

OM Institute of Engg. & Technology Junagadh 29


CP LAB MANUAL

Unit: 5

5.1 Write a program to find factorial number of given number

#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");


scanf("%d",&n);

// show error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");

else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}

return 0;
}

Output:
Enter an integer: 10
Factorial of 10 = 3628800

OM Institute of Engg. & Technology Junagadh 30


CP LAB MANUAL

5.2 Write a program to display fibonaci series.

OM Institute of Engg. & Technology Junagadh 31


CP LAB MANUAL

5.3 Write a program to print the total of 1 to 50 using for loop.

OM Institute of Engg. & Technology Junagadh 32


CP LAB MANUAL

5.4 Write a program to print 1,2,4,8,16,32,64.

OM Institute of Engg. & Technology Junagadh 33


CP LAB MANUAL

5.5 Write a program to print 10 to 1.

OM Institute of Engg. & Technology Junagadh 34


CP LAB MANUAL

5.6 Write a program to make a following pattern.


1
12
123
1234
12345

OM Institute of Engg. & Technology Junagadh 35


CP LAB MANUAL

5.7 Write a program to find power of given number.

OM Institute of Engg. & Technology Junagadh 36


CP LAB MANUAL

5.8 Write a program to print 1 to 10 numbers using while loop.

OM Institute of Engg. & Technology Junagadh 37


CP LAB MANUAL

5.9 Write a program to print 1 to 10 numbers using do while loop.

OM Institute of Engg. & Technology Junagadh 38


CP LAB MANUAL

Unit 6:
6.1 Write a program to create an array of n integer.Enter for n elements and
display it.

#include <stdio.h>

int main()
{
int n,array[n],c;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Elements in array:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}

Output:
Enter number of elements in array
5
Enter 5 elements
10
20
30
40
50
Elements in array:
10
20
30
40
50

OM Institute of Engg. & Technology Junagadh 39


CP LAB MANUAL

6.2 Write a progam to find sum of all the elements of array.

OM Institute of Engg. & Technology Junagadh 40


CP LAB MANUAL

6.3 Write a program to find largest and smallest number from an array of n
elements.

OM Institute of Engg. & Technology Junagadh 41


CP LAB MANUAL

6.4 Write a program to input 5 character and print it in reverse order.

OM Institute of Engg. & Technology Junagadh 42

You might also like