R23 C Lab Record
R23 C Lab Record
i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
Vi Editor:
The vi editor was developed by William Joy as a more visual version of his own
command line editor program called ex, which was becoming popular as a text editor.
The vi editor uses the terminal window for editing a file. For an example, run the
following command to open a new file or an existing file of the same name:
vi filename
There are two ways of working in vi:
Command Mode
Insert Mode
o Command Mode: In command mode, actions are taken on the file. The vi editor starts in
command mode. Here, the typed words will act as commands in vi editor. To pass a
command, you need to be in command mode.
o Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will
take you to the command mode from insert mode.
o By default, the vi editor starts in command mode. To enter text, you have to be in insert
mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will
appear on the screen but you'll be in insert mode. Now you can type anything.
o To exit from insert mode press Esc key, you'll be directed to command mode.
Quitting vi:
Press zz or ‘:wq’ in command mode.
ii) Exposure to Turbo C, gcc
Turbo C:
Turbo C was an integrated development environment (IDE) for programming in the C language.
It was developed by Borland and first introduced in 1987. At the time, Turbo C was known for
its compact size, comprehensive manual, fast compile speed and low price.
How to install C
There are many compilers available for c and c++. You need to download any one. Here, we are
going to use Turbo C++. It will work for both C and C++. To install the Turbo C software, you
need to follow following steps.
You can download turbo c++ from many sites. download Turbo c++
Now, you need to create a new directory turboc inside the c: drive. Now extract the tc3.zip file in
c:\truboc directory.
We will recommend you to use Turbo C or Turbo C++ IDE, which is the oldest IDE for C
programming. It is freely available over the Internet and is good for a beginner.
Step 1: Open turbo C IDE(Integrated Development Environment), click on File, and then click
on New
Step 2: Write a Hello World program
Step 3: Click on Compile menu and then on Compile option, or press the keys and press Alt +
F9 to compile the code.
Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs are first compiled to
generate the object code and then that object code is Run.
Step 5: Output is here.
GCC:
The original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU
Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-like
operating system as free software, to promote freedom and cooperation among computer users
and programmers.
// hello.c
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
>gcc hello.c
// Compile and link source file hello.c into executable a.exe (Windows) or a (Unixes)
The printf() and scanf() functions are used for input and output in C language. Both functions are
inbuilt library functions, defined in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given statement to the console.
printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the console.
scanf("format string",argument_list);
Program to print sum of 2 numbers
#include<stdio.h>
int main()
{
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
Output
Algorithm:
Step 1: Start
Step 4: avg=(a+b+c)/3;
Step 6: Stop.
Flowchart
Program:
#include<stdio.h>
int main()
clrscr();
printf("a:");
printf("b:");
printf("c:");
scanf("%d", & c); //Reading Variable c
avg = (a + b + c) / 3;
getch();
return 0;
Output:
a: 25
b: 35
c: 30
Algorithm:
Step 1: Start
Flowchart:
Program:
#include<stdio.h>
int main()
scanf("%f",&fahrenheit);
return0;
Output:
Enter Fahrenheit:
100
Celsius: 37.777779
Algorithm:
Step 1: Start
Step 3: Convert the temperature Celsius to Fahrenheit using the formula F = (C * 9/5) + 32.
Step 5: Stop
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
voidmain()
float c, f;
scanf("%f",&c);
f=(c*9/5)+32;
getch();
Output
Enter the Celsius:
37
Algorithm:
Step 1: Start
Step 4: SI = (P * N* R) /100.0
Step 5: Print SI
Step 6: Stop
Flowchart:
Program:
#include<stdio.h>
int main()
scanf("%f", &principle);
scanf("%f", &time);
scanf("%f", &rate);
return 0;
Output:
Enter time: 2
Program:
#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
double num;
printf("Enter a number : ");
scanf("%lf", &num);
printf("The square root is : %lf\n", sqrt(num));
getch();
return 0;
}
Output:
ii) Finding compound interest
Program:
#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
double principle, rate, time, amount, CI;
printf("Enter the principle amount:\n ");
scanf("%lf",&principle);
printf("Enter the rate of interest:\n ");
scanf("%lf",&rate);
printf("Enter the time duration: \n");
scanf("%lf",&time);
amount = principle*(pow((1+rate/100),time));
CI = amount - principle;
printf(" The compound interest is: %lf",CI);
getch();
return 0;
}
Output:
iii) Area of a triangle using heron’s formulae
Program:
# include<stdio.h>
#include<conio.h>
# include <math.h>
int main()
float a,b,c,s,area;
scanf("%f%f%f", &a,&b,&c);
s = (a+b+c)/2;
getch();
return 0;
Output:
Program:
#include <stdio.h>
void main()
float u, a, t,dist;
scanf("%f",&a);
scanf("%f",&u);
scanf("%f",&t);
dist = (u * t) + (a * t * t) / 2;
getch();
}
Output:
Week 4: Simple computational problems using the operator’ precedence and associativity
a. A+B*C+(D*E)+F*G
= 1 + 2 * 3 + (4 * 5) + 6 * 7
= 1 + 2 * 3 + 20 + 6 * 7
= 1 + 6 + 20 + 6 * 7
= 1 + 6 + 20 + 42
= 7 + 20 + 42
= 27 + 42
= 69
b.A/B*C-B+A*D/3
= 1 / 2 * 3– 2 + 1 * 4 / 3
= 0 * 3 -2 + 1 * 4 / 3
=0-2+1*4/3
=0-2+4/3
= 0– 2 + 1
= -2 + 1
= -1
c. A+++B---A
= 1+++2---1
= 2+2---1
= 2+1-1
= 3-1
=2
d. J=(i++)+(++i)
J = (3++) + (++3)
J = (4) + (4)
J=8
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
J = A+B*C+(D*E)+F*G;
printf("Result=%d\n",J);
J = A/B*C-B+A*D/3;
printf("Result=%d\n",J);
J = A+++B---A;
printf("Result=%d\n",J);
J = (i++)+(++i);
printf("Result = %d",J);
getch();
Output:
ii) Find the maximum of three numbers using conditional operator
Program:
# include <stdio.h>
#include<conio.h>
void main()
int a, b, c, big ;
getch();
Output:
Case 1:
Case 2:
Case 3:
iii) Take marks of 5 subjects in integers, and find the total, average in float
Program:
#include <stdio.h>
#include<conio.h>
int main()
clrscr();
printf("Enter the marks of English, Maths, Chemistry, Computer and Physics: \n");
average = total / 5;
getch();
return 0;
Output:
Week 5: Problems using control statements
i) Write a C program to find the max and min of four numbers using if-else.
i) Write a C program to find the max and min of four numbers using if-else.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
int a, b, c, d,min,max;
clrscr();
max = min = a;
if (b > max)
max = b;
min = b;
if (c > max)
max = c;
if (d > max)
max = d;
min = d;
getch();
return 0;
CASE 1:
OUTPUT:
CASE 2:
OUTPUT:
CASE 3:
OUTPUT:
CASE 4:
OUTPUT:
ii) Write a C program to generate electricity bill.
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
int unit,serviceno;
char cname[20];
clrscr();
scanf("%d",&serviceno);
scanf("%s",cname);
scanf("%d", &unit);
}
else if(unit <= 150)
else
getch();
return 0;
CASE 1:
OUTPUT:
CASE 2:
OUTPUT:
CASE 3:
OUTPUT:
CASE 4:
OUTPUT:
iii) Find the roots of the quadratic equation.
PROGRAM:
#include <math.h>
#include <stdio.h>
#include<conio.h>
int main() {
clrscr();
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
else if (discriminant == 0) {
else {
realPart = -b / (2 * a);
getch();
return 0;
CASE 1:
OUTPUT:
CASE 2:
OUTPUT:
iv) Write a C program to simulate a calculator using switch case
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
float result;
clrscr();
scanf("%d",&num1);
scanf("%d",&num2);
scanf(" %c",&ch);
result=0;
switch(ch)
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
getch();
return 0;
CASE 1:
OUTPUT:
CASE 2:
OUTPUT:
CASE 3:
OUTPUT:
CASE 4:
OUTPUT:
CASE 5:
OUTPUT:
CASE 6:
OUTPUT:
#include <stdio.h>
#include<conio.h>
int main() {
int year;
clrscr();
scanf("%d", &year);
if (year % 400 == 0) {
else if (year % 4 == 0)
else
getch();
return 0;
}
CASE 1:
OUTPUT:
CASE 2:
OUTPUT:
Week 6
#include<stdio.h>
int main(){
int x,fact=1,n;
printf("Enter a number to find factorial: ");
scanf("%d",&n);
for(x=1;x<=n;x++)
fact=fact*x;
printf("Factorial of %d is: %d",n,fact);
return 0;
}
Output: Enter a number to find factorial:6
factorial of 6 is:720
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
}
Output:
#include <stdio.h>
#include<conio.h>
int main()
{
int rows, space, i, j;
printf("Enter number of rows: ");// enter a number for generating the pyramid
scanf("%d",&rows);
for(j=0-i; j <= i; j++) //inner loop for displaying the pyramid of numbers
{
}
printf("\n"); // every line in different row
}
}
Output:
Enter number of rows: 5
0
101
21012
3210123
432101234
Week 7
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
return 0;
}
Output:
Enter size of the array: 5
Enter elements in array: 1
2
3
4
5
minimum of an array is: 1
maximum of an array is: 5
Output:
Enter the binary number
1000010
Ones' complement of binary number 1000010 is 0111101
Two's complement of binary number 1000010 is 0111110
Week 8
i) Multiplication two matrices in C
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;
Output:
Week 9
#include<stdio.h>int
main() {
0;
return 0;
}
Output:
For student 1
sum=111
Average=37.00
For student 2
sum=112
Average=37.33
For student 3
sum=115
Average=38.33
Week 10
#include <stdio.h>int
main() {
scanf("%d", &a[i][j]);
if (j == c - 1)
printf("\n");
= 0; j < c; ++j) {
transpose[j][i] = a[i][j];
== r - 1)
printf("\n");
return 0;
}
Output:
Entered matrix:
14 0
-5 2 7
1 -5
4 2
0 7
Week 11
#include<stdio.h>
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
int main(){
int n;
scanf("%d",&n);
printf("%d %d ",0,1);
return 0;
}
Output:
#include<stdio.h>
int fact(int);
int main()
int x,n;
x=fact(n);
int fact(int n)
if(n==0) return(1);
return(n*fact(n-1));
}
Output:
Factorial of 5 is 120
Week 12
Write a C program to find no of lowercase, uppercase, digits and other characters using pointers.
#include <stdio.h>
#include <stdlib.h>
int main()
char str[100];
int i;
int upper=0,lower=0,num=0,special=0;;
gets(str);
upper++;
num++;
else{
special++;
printf("\nNumbers: %d",num);
getch();
return 0;
Output:
To_Learn_Code:code4coding.com
Upper case letters: 3
Numbers: 1
Special characters: 4