0% found this document useful (0 votes)
37 views8 pages

Lab1 2

Uploaded by

jenithkudupudi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views8 pages

Lab1 2

Uploaded by

jenithkudupudi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

WEEK 1

Objective: Getting familiar with the programming environment on the computer and writing the first program.
Suggested Experiments/Activities:
Tutorial 1: Problem-solving using Computers.
Lab1: Familiarization with programming environment

i. Basic Linux environment and its editors like Vi, Vim & Emacs etc.
ii. Exposure to Turbo C, gcc
iii. Writing simple programs using printf(), scanf()

Linux text editors:

Linux text editors can be used for editing text files, writing codes, updating user
instruction files, and more. A Linux system supports multiple text editors. A text editor
plays an important role while coding. So, it is important to select the best text editor. A
text editor should not only be simple but also functional and should be good to work
with.

Vi/VIM editor:

Vim editor is one of the most used and powerful command-line based editor of the Linux
system. By default, it is supported by most Linux distros. It has enhanced functionalities
of the old Unix Vi editor. It is a user-friendly editor and provides the same environment
for all the Linux distros. It is also termed as programmer's editor because most
programmers prefer Vi editor.

Vi editor has some special features such as Vi modes and syntax highlighting that
makes it powerful than other text editors. Generally, it has two modes:

Command Mode: The command mode allows us to perform actions on files. By default,
it starts in command mode. In this mode, all types of words are considered as
commands. We can execute commands in this mode.

Insert Mode: The insert mode allows to insert text on files. To switch from command
mode to insert mode, press the Esc key to exit from active mode and 'i' key.

To invoke the vi editor, execute the vi command with the file name as follows:

1. vi <file name>

It will look like below image:


How to start with Turbo C and GCC

Install Turbo C in your computer’s C Drive and follow the below shown commands to
open it.

METHOD 1: Using command prompt.

Step1: Win + R ; will open a run command prompt.

Step2: Type cmd and hit Enter ; will open command prompt.

Step3: Now type cd\ and hit Enter (will takes you to root directory i.e C drive)

Step4: Type cd TC and hit Enter (Now you are in TC folder)

Step5: Type cd bin and hit Enter ( Now you are redirected to bin folder, a subfolder in tc)

Step6: Type TC and hit Enter (will open TC window now you can write edit and compile
your programs, through this command you are accessing an application called TC.exe )
How to Compile a C program using GCC

● GCC – GNU compiler collection is a GNU project which supports C , C++,


Fortran, Java, Ada and Go.
● GCC for windows is available .
● Well Ubuntu provides inbuilt GCC, it is not required to install it externally so I
prefer to use Ubuntu, it is available for free.
● In Ubuntu follow below commands to write, edit and compile your programs.

Step1: Go to Dashboard.

Step2: Type Terminal

Step3: Click on Terminal

Step4: Now type vim <your filename with extension .c> Ex. vi prog1.c (This will create
a file prog1.c and opens it in a editor called vim)

Step5: Press i to get into insert mode and now you can type your program here.

Step6: After typing your program press escape to get out of insert mode and type ‘:wq’
without quotes and hit enter (This will make you to come out of vim editor by saving the
file)

Step7: Now type gcc <your file name with .c extension> and hit enter to compile your
file.

Step8: To execute your file type ./a.out and hit enter. This will run the file.
//Writing simple programs using printf(), scanf()

Program1:

//print “hello world”

#include<stdio.h>

int main()

printf(“Hello world\n”);

return 0;

Program2:

//print postal address using printf() statement

#include<stdio.h>

int main()

printf(“Name:N.Prasanna\n”);

printf(“D/o N.Suresh\n”);

printf(“Eluru\n W.G.Dt”);

printf(“Andhra Pradesh\n”);

return 0;

Program3:

//Read character,integer,float using scanf() statement and print values

#include<stdio.h>

int main()
{

char c;

int a;

float b;

printf(“Enter a character, integer, float values\n”);

scanf(“%c %d %f”,&c,&a,&b);

printf(“Character is %c\n,c);

printf(“Integer is %d\n”,a);

printf(“Float value is %f”,b);

return 0;

WEEK 2
Objective: Getting familiar with how to formally describe a solution to a problem in a
series of finite steps both using textual notation and graphic notation.
Suggested Experiments /Activities:
Tutorial 2: Problem-solving using Algorithms and Flow charts.
Lab 1: Converting algorithms/flow charts into C Source code.
Developing the algorithms/flowcharts for the following sample programs

i. Sum and average of 3 numbers


ii. Conversion of Fahrenheit to Celsius and vice versa
iii. Simple interest calculation

//Sum and Average of 3 Numbers:


#include <stdio.h>
int main()
{
int num1, num2, num3;
float sum, avg;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
sum = num1 + num2 + num3;
avg= sum / 3.0;
printf("Sum is %.2f, and the average is %.2f.\n", sum, avg);
return 0;
}
Output:
Enter three numbers: 30
50
70
Sum is 150.00, and the average is 50.00.

//Conversion of Fahrenheit to Celsius and Vice Versa:


#include <stdio.h>

int main()

double temp1,temp2,temp3,temp4;

printf("1. Convert Fahrenheit to Celsius\n");

printf("Enter temperature in Fahrenheit: ");

scanf("%lf", &temp1);

temp2 = (temp1- 32) * 5 / 9;

printf("That's %.2lf degrees Celsius. ", temp2);

printf("\n 2. Convert Celsius to Fahrenheit\n");

printf("Enter temperature in Celsius: ");

scanf("%lf", &temp3);

temp4 = (temp3 * 9 / 5) + 32;


printf("That's %.2lf degrees Fahrenheit. ", temp4);

return 0;

Output:

1. Convert Fahrenheit to Celsius

Enter temperature in Fahrenheit: 99

That's 37.22 degrees Celsius.

2. Convert Celsius to Fahrenheit

Enter temperature in Celsius: 36

That's 96.80 degrees Fahrenheit.

//Simple Interest Calculation:


#include <stdio.h>
int main() {
double P,T,R, SI;
printf("Enter the principal amount: ");
scanf("%lf", &P);
printf("Enter the annual interest rate (in percentage): ");
scanf("%lf", &R);
printf("Enter the time period (in years)");
scanf("%lf", &T);
SI = (P * T * R)/100;
printf("simple interest is %.2lf", SI);
return 0;
}
Output:
Enter the principal amount: 100000
Enter the annual interest rate (in percentage): 10
Enter the time period (in years)2
simple interest is 20000.00

You might also like