Top Banner
INTRODUCTION TO PROGRAMMING PRACTICAL FILE RAHUL KHANNA IT - 2 760/IT/14
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

INTROduction to programming

INTRODUCTION TO C

C programming is an ANSI/ISO standard and powerful programming language for developing real time applications.C programming language was invented by Dennis Ritchie at the Bell Laboratories in 1972.It was invented for implementing UNIX operating system. C programming is most widely used programming language even today. All other programming languages were derived directly or indirectly from C programming concepts. C programming is the basis for all programming languages. This C programming tutorial explains all basic concepts in C like history of C language, data types, keywords, constants, variables, operators, expressions, control statements, array, pointer, string, library functions, structures and unions etc.

This C programming tutorial is designed for the new learners, students and also for the corporate level developers who want to learn and refresh their C programming skills.

C programming history

The C programming languageis a structure oriented programming language, developed at Bell Laboratoriesin 1972 by Dennis Ritchie

C programming language features were derived from an earlier language called B (Basic Combined Programming Language BCPL)

C language was invented for implementing UNIX operating system

In 1978, Dennis Ritchie and Brian Kernighan published the first edition The C Programming Language and commonly known as K&R C

In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or ANSI C, was completed late 1988.

Features of C programming language:

Reliability

Portability

Flexibility

Interactivity

Modularity

Efficiency and Effectiveness

Uses of C programming language:

The C programming language is used for developing system applications that forms a major portion of operating systems such as Windows, UNIX and Linux. Below are some examples of C being used.

Database systems

Graphics packages

Word processors

Spreadsheets

Operating system development

Compilers and Assemblers

Network drivers

Interpreters

Which level is C language belonging to?

S.no

High Level

Middle Level

Low Level

1

High level languages provide almost everything that the programmer might need to do as already built into the language

Middle level languages dont provide all the built-in functions found in high level languages, but provides all building blocks that we need to produce the result we want

Low level languages provides nothing other than access to the machines basic instruction set

2

Examples:Java, Python

C, C++

Assembler

The C languageis a structured language

S.no

Structure oriented

Object oriented

Non structure

1

In this type of language, large programs are divided into small programs called functions

In this type of language, programs are divided into objects

There is no specific structure for programming this language

2

Prime focus is on functions and procedures that operate on the data

Prime focus is in the data that is being operated and not on the functions or procedures

N/A

3

Data moves freely around the systems from one function to another

Data is hidden and cannot be accessed by external functions

N/A

4

Program structure follows Top Down Approach

Program structure follows Bottom UP Approach

N/A

5

Examples:

C,Pascal, ALGOL and Modula-2

C++, JAVA and C# (C sharp)

BASIC, COBOL, FORTRAN

Key points to remember in C language:

1. The C languageis structured, middle level programming language developed by Dennis Ritchie

2. Operating system programs such as Windows, Unix, Linux are writtenin C language

3. C89/C90 and C99 are two standardized editions of C language

4. C has been written in assembly language

Cprogrammingbasics

Below are few commands and syntax used in C programming to write a simple C program. Lets see all the sections of a simple C program line by line.

S.no

Command

Explanation

1

#include

This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program

2

int main()

This is the main function from where execution of any C program begins.

3

{

This indicates the beginning of the main function.

4

/*_some_comments_*/

whatever is given inside the command /* */ in any C program, wont be considered for compilation and execution.

5

printf(Hello_World! );

printf command prints the output onto the screen.

6

getch();

This command waits for any character input from keyboard.

7

return 0;

This command terminates C program (main function) and returns 0.

8

}

This indicates the end of the main function.

____________________________________________________________

C printf and scanf

printf() and scanf() functions are inbuilt library functions in C which are available in C library by default.These functions are declared and related macros are defined in stdio.h which is a header file.

We have to include stdio.h file as shown in below C program to make use of these printf() and scanf() library functions.

1. C printf() function:

printf() function is used to print the character, string, float, integer, octal and hexadecimal values onto the output screen.

We use printf() function with%dformat specifier to display the value of an integer variable.

Similarly%cis used to display character,%ffor float variable,%sfor string variable,%lffor double and%xfor hexadecimal variable.

To generate a newline,we use \n in C printf() statement.

2. C scanf() function:

scanf() function is used to read character, string, numeric data from keyboard

Consider below example program where user enters a character. This value is assigned to the variable ch and then displayed.

Then, user enters a string and this value is assigned to the variablestr and then displayed.

C Data Types

C data types are defined as the data storage format that a variable can store adata to perform a specific operation.Data types are used to define a variable before using in a program.Size of variable,const and array are determined by data types. There are four data types in the C language. They are

S.no

Types

Data Types

1

Basic data types

int, char, float, double

2

Enumeration data type

Enum

3

Derived data type

pointer, array, structure, union

4

Void data type

Void

C Tokens and keywords:

C tokens, Identifiers and Keywords are the basics in a C program. All are explained in this page with definition and simple example programs.C tokensare the basic building blocks in C language which are constructed together to write a C program.Each program element in a C program is given a name called identifiers.

Constants:

C Constants are also like normal variables. But, the only difference is,their values cant be modified by the program once they are defined.Constants refer to fixed values. They are also called as literals.Constantsmay be belonging to any of the data type.

Variables:

C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.The value of the C variable may get changed in the program. The C variable might be belonging to any of the data types like int, float, char etc.

Operators and Expressions:

The symbols which are used to perform logical and mathematical operations in a C program are called C operators.These C operators join individual constants and variables to form expressions. Operators, functions, constants and variables are combined together to form expressions.

Practicals

Q1. If a 5-digit number is input through keyboard write a program to print a new number by adding 1 to each of the digits.

ALGORITHM:

1.Start

2.Declare variables for number, sum, remainder and check

3.Input variables

4.While( number>0)

{rem = number%10;

If(rem!=9)

If(check == 0)

sum = 10*sum + rem +1;

Else

sum = 10*sum + rem +2;

check = 0;

Else

sum = 10*sum;

check = 1;

}number = number/10;

5. number = sum, sum =0;

6. while(number>0)

{

rem = no%10;

sum = 10*sum + rem;

number = number/ 10;

}

7.Print Sum;

8.Stop;

FLOWCHART:

OUTPUT:

SOURCE CODE:

#include

int main()

{

long int no,sum=0;

int rem,check=0;

printf("Enter the required number:");

scanf("%ld",&no);

printf("\nGiven Number: %d",no);

while(no>0)

{

rem=no%10;

if(rem!=9)

{

if(check==0)

sum=(10*sum)+(rem+1);

else{

sum=(10*sum)+(rem+2);

check=0;

}

}

else

{

sum=(10*sum)+0;

check=1;

}

no=no/10;

}

no=sum; sum=0;

while(no>0)

{

rem=no%10;

sum=(10*sum)+rem;

no=no/10;

}

printf("\nAfter Adding one: %ld",sum);

return 0;

}

Q2. Basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

ALGORITHM:

1.Start

2.Declare variables for Gross salary,hr,bs.

3.Input Basic Salry

4.Calculations:

Da = 0.4*basic salary

Hra = 0.2*basic salary

Gross salary = HRA + DA + Basic Salary;

5.Print Gross Salary

6.Stop

FLOWCHART:

OUTPUT:

SOURCE CODE:

#include

int main()

{

float bs,da,hr,gs;

printf("Enter basic salary : ");

scanf("%f",&bs);

da=(40*bs)/100;

hr=(20*bs)/100;

gs=bs+da+hr;

printf("DA:%f\nHR:%f\nGross Salary:%f\n",da,hr,gs);

return 0;

}

Q3. Enter marks of 5 subjects(out of 100), and calculate percentage

ALGORITHM:

1.Start

2.Declare an array for marks and a variable for sum (float type)

3.Input marks in array

4.Calculate sum of marks

5.Output sum/5

6.Stop

FLOWCHART:

OUTPUT:

SOURCE CODE:

#include

int main()

{

float marks[5],sum=0;

int i;

printf("Enter marks of :\n");

for(i=0;i25)

printf ("Driver is insured");

else

printf ("Driver is not insured");

}

}

return 0;

}

Q6. Write a program that takes coefficients of a quadratic equation as input and outputs the roots of the quadratic equation.

SOURCE CODE :

#include

#include

#include

int main() {

float a, b, c, D ,x1, x2, rp , ip;

printf("\nEnter the coefficients of quadratic equation in the form of ax^2+bx+c = \n");

scanf("%f %f %f", &a, &b,&c);

D=b*b-4*a*c;

if(D>=0) {

x1= (-b+sqrt(D))/(2*a);

x2= (-b-sqrt(D))/(2*a);

printf("The roots are %f %f", x1, x2);

}

else {

rp=b/(2*a);

ip=sqrt(-D)/(2*a);

printf("The roots are %f +i%f ,%f -i%f", rp, ip, rp,ip);

}

return 0 ;

getch();

}

OUTPUT:

Q7.Write a menu-driven program (using switch statement) that takes two input integers and performs addition, subtraction, division and multiplication based on the menu option selected by the user.

SOURCE CODE:

#include

#include

int main() {

float a,b,result = 0.0;

int choice;

printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\nEnter your choice:");

scanf("%d", &choice) ;

printf("\nEnter the numbers:");

scanf("%f %f", &a, &b);

switch (choice) {

case 1:

result=a+b;

break;

case 2:

result = a-b;

break;

case 3:

result = a*b;

break;

case 4:

result = a/b;

break;

}

printf("\nThe result is = %f", result);

return 0;

getch();

}

OUTPUT:

Q8.A certain grade of steel is graded according to the following conditions:

i. Hardness must be greater than 50

ii. Carbon content must be less than 0.7

iii. Tensile strength must be greater than 5600

The grades are as follows:

Grade is 10 if all three conditions are met

Grade is 9 if conditions (i) and (ii) are met

Grade is 8 if conditions (ii) and (iii) are met

Grade is 7 if conditions (i) and (iii) are met

Grade is 6 if only one condition is met

Grade is 5 if none of the conditions are met

SOURCE CODE:

#include

#include

int main() {

int hn, ts;

float carbon;

printf("Enter the hardness, carbon content and tensile strength of the steel : ");

scanf("%d %f %d",&hn, &carbon ,&ts);

if (hn>50&&carbon5600)

printf("The grade of steel is 10");

else if(hn>50&&carbon50&&ts>5600)

printf("The grade of steel is 7");

else if (hn>50||carbon5600)

printf("The grade of steel is 6");

else

printf("The grade of steel is 5");

return 0 ;

getch();

}

OUTPUT:

Q9.Write a program to find the greatest of three number s input through keyboard.

SOURCE CODE:

#include

#include

int main() {

int a,b,c;

printf("Enter any three different numbers:" );

scanf("%d%d%d",&a,&b,&c);

if(a>b&&a>c)

printf("The greatest number is %d " , a );

else if(b>c)

printf("The greatest number is %d " , b);

else

printf("The greatest number is %d " , c);

return(0);

getch();

}

OUTPUT:

Q10..Write a program to find sum of the following series:-

1 + + + +

SOURCE CODE:

#include

#include

int main() {

float i;

float a;

float sum=0;

for(i=1;i