Sei sulla pagina 1di 33

1

O- LEVEL PROJECT
For NIELIT, New Delhi.

NATIONAL INSTITUTE OF ELECTRONICS AND


INFORMATION TECHNOLOGY

NAME OF STUDENT--- xxxxxxx

REGISTRATION NO--- xxxxxxxx

PROJECT ON ---“ PROGRAMMING IN C- LANGUAGE”


2

Proforma of the Project Completion Certificate

This is to certify that the Project work done at NIELIT, New Delhi, titled
‘PROGRAMMING IN C-LANGUAGE’ by Mr. KUNDAN KUMAR (NIELIT
Registration No- 1085660) in partial fulfillment of NIELIT ‘O’ Level
Examination has been found satisfactory. This report has not been submitted
for any other examination and does not form part of any other course
undergone by the candidate.

It is further certifies that he/she has appeared in all the four modules of
NIELIT ‘O’ Level Examination.

Signature
Name:
(Institute PROV No./FULL No.)
(or)
Head of the Organization/Division:
Name of the Organization:
Address:
(or)
Name of the Guide/Supervisor: xxxxxxxxx
Qualification: “Bachelor in Computer Science and Engineering”
3

ABOUT THE LANGUAGE


History of C:

C is a programming language developed at AT & T’s Bell Laboratories of USA


in 1972 . It was designed and written by a man named ”Dennis Ritchie”. In the
late seventies C began to replace the more familiar languages of that time like
PL/I, ALGOL, etc. No one pushed C. It wasn’t made the ‘official’ Bell Labs
language. Thus, without any advertisement, C’s reputation spread and its pool
of user grew . Ritchie seems to have been rather surprised that so many
programmer preferred C to older languages like FORTRAN or PL/I, or the
never one likes Pascal and APL. But, that’s what happened .

Possibly why C so popular is because it is reliable , simple and easy to use .


The increasing popularity of C is probably due to its many desirable qualities.
It is a robust language whose rich set of built –in functions and operations can
be used to write any complex program. The C compiler combines the
capabilities of an assembly language with the features of a high- level
language and therefore it is well suited for writing both system software and
business packages. In fact, many of the C compilers available in the market are
written in C.

Programs written in C are efficient and fast. This is due to its variety of data
types and powerful operations. It many times faster than Basic. For example, a
program to increment a variable from 0 to 15000 takes about one second in C
while it takes more than 50 seconds in an interpreter basic.

There are only 32 keywords in ANSI C and its strength lies in its built in
functions. Several standard functions are available which can be used for
developing programs.

C is highly portable . This means that C programs written for one computer
can be run on another with little or no modification. Portability is important if
we plan to use a new computer with different operating system.
4

C language is well suited for structured programming , thus requiring the user
to think of a problem in terms of function modules or blocks. A proper
collection of these modules would make a complete program. This modular
structure makes program debugging, testing and maintenance easier.

A C program is basically a collection of functions that are supported by the C


library. We can continuously add our own function to C library.
With the availability of a large number of functions, the programming task
becomes simple.

1- It emphasises on non-living & non real items . Algorithmic approach.


2- It divided a large program into function.
3- Function shares the global data.
4- Data can be moved from one function to another.
5- Dada can accessed by External function.
6- Data is in shareable mode.
7- Data cannot be hidden.
8- Top – down approach is used.
9- It is Procedure oriented Programming.
10- C is used to developing system software, Application software.
5

HARDWARE & SOFTWARE REQUIREMENT

(A) HARDWARE REQUIREMENT :

An Intel based central processing unit capable of running any sort of


windows operating system such as Pentium based workstation.

1- Minimum 64 MB Ram (128 MB Desirable) at server.


2- Minimum 60 MB of free disk space for files.
3- A CD Rom drive.
4- Minimum 48 MB of RAM at workstation.
5- VGA 15” color monitor for workstation.

(B) SOFTWARE REQUIREMENT:


The software requirements are as follows.

1-Windows Xp or Above

2-C editor

3-Microsoft word for documentation.


6

PROGRAM CODING :
SOME EXAMPLE OF C PROGRAMMING-

/* Write a program to addition two numbers and displays the result.*/

#include <stdio.h>

#include<conio.h>

Main()

int number;

float amount;

number= 100;

amount=30.75+75.25;

printf(“%d\n”, number);

printf(“%5.2f”,amount);

Out put: 100

106.10
7

/*Factorial program in c using for loop*/

#include<stdio.h>

int main(){

int i,f=1,num;

printf("Enter a number: ");

scanf("%d",&num);

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

f=f*i;

printf("Factorial of %d is: %d",num,f);

return 0;

output:

Enter a number: 8

Factorial of 8 is: 40320


8

/* Write C program to convert temperature from degree celsius to


Fahrenheit*/

#include <stdio.h>

int main()

float celsius, fahrenheit;

/* Input temperature in celsius */

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

/* celsius to fahrenheit conversion formula */

fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;

output:

Enter temperature in Celsius: 100

100 Celsius = 212.00 Fahrenheit


9

/*Write a program to Multiplication Table Up to a range (entered by the


user)*/

#include <stdio.h>

int main()
{
int n, i, range;
printf("Enter an integer: ");
scanf("%d",&n);
printf("Enter the range: ");
scanf("%d", &range);
for(i=1; i <= range; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}

return 0;
}

output:
Enter an integer: 12
Enter the range: 8
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
10

/* Write a C Program to reverse a given number*/

#include<stdio.h>
int main()
{
int num, rem, rev = 0;

printf("\nEnter any no to be reversed : ");


scanf("%d", &num);

while (num >= 1) {


rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}

printf("\nReversed Number : %d", rev);


return (0);
}
output:
Enter any no to be reversed : 123
Reversed Number : 321
11

/* Write a c program to Check Whether Given Number is


Palindrome or Not*/

#include<stdio.h>
#include<string.h>

int main() {
int num, i, count = 0;
char str1[10], str2[10];

printf("nEnter a number:");
scanf("%d", &num);

//Convert Number to String


sprintf(str1, "%d", num);

//Copy String into Other


strcpy(str2, str1);

//Reverse 2nd Number


strrev(str2);

count = strcmp(str1, str2);

if (count == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);

return 0;
}
12

/* C Program to generate the Fibonacci Series starting from


any two numbers*/

#include<stdio.h>

int main() {
int first, second, sum, num, counter = 0;
printf("Enter the term : ");
scanf("%d", &num);
printf("\nEnter First Number : ");
scanf("%d", &first);

printf("\nEnter Second Number : ");


scanf("%d", &second);

printf("\nFibonacci Series : %d %d ", first, second);

while (counter < num) {


sum = first + second;
printf("%d ", sum);
first = second;
second = sum;
counter++;
}

return (0);
}

output:
Enter the term : 5
Enter First Number : 1
Enter Second Number : 3
Fibonacci Series : 1 3 4 7 11 18 29
13

/* write a c program that displays the recommended actions depending


on the color of a traffic using the switch statements.*/

#include <stdio.h>

#include<conio.h>

void main ()

enum light {

RED=1, YELLOW, GREEN

};

int ch;

printf(“\n1. Red\n2. Yellow\n3.Green”);

printf(“\n Enter your choice “);

scanf(“%d”, &ch);

switch (ch)

Case RED:

Printf(“Stop”);

Break;

case YELLOW:

printf(“Wait”);

break;

case GREEN:
14

printf(“Go”);

break;

getch();

/*C a program to count the number of words in a given string. Two


words are separated by one or more blank spaces.*/

#include <stdio.h>

#include <conio.h>

void main()

char a[100];

int len,i,word=1;

clrscr();

printf("\nENTER A STRING: ");

gets(a);

len=strlen(a);

for(i=0;i<len;i++)

if(a[i]!=' ' && a[i+1]==' ')

word=word+1;

}
15

printf("\nTHERE ARE %d WORDS IN THE STRING",word);

getch();

output:

ENTER A STRING: CHANDAN KUMAR

THERE ARE 2 WORDS IN THE STRING

/*Write a program to Count the number of vowels in a given string using


C program*/

#include <stdio.h>

#include <conio.h>

void main()

char a[100];

int len,i,vow=0;

clrscr();

printf("\nENTER A STRING: ");

gets(a);

len=strlen(a);

for(i=0;i<len;i++)

{
16

if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' || a[i]=='I'


|| a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U')

vow=vow+1;

printf("\nTHERE ARE %d VOWELS IN THE STRING",vow);

getch();

output:

ENTER A STRING: Garurav sahu

THERE ARE 5 VOWELS IN THE STRING

/*Write a c program to print full pyramid using (*).

#include <stdio.h>

int main()

int i, space, rows, k=0;

printf("Enter number of rows: ");

scanf("%d",&rows);

for(i=1; i<=rows; ++i, k=0)

for(space=1; space<=rows-i; ++space)

printf(" ");
17

while(k != 2*i-1)

printf("* ");

++k;

printf("\n");

return 0;

output:

***

*****

*******

*********
18

/* Write a c program to print half pyramid using alphabets*/

#include <stdio.h>

int main()

int i, j;

char input, alphabet = 'A';

printf("Enter the uppercase character you want to print in last row: ");

scanf("%c",&input);

for(i=1; i <= (input-'A'+1); ++i)

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

printf("%c", alphabet);

++alphabet;

printf("\n");

return 0;

}
19

output:

BB

CCC

DDDD

EEEEE

/* write a c program to print half pyramid a using numbers*/

#include <stdio.h>

int main()

int i, j, rows;

printf("Enter number of rows: ");

scanf("%d",&rows);

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

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

printf("%d ",j);
20

printf("\n");

return 0;

output:

12

123

1234

12345

/* Write a c program to Transpose of a matrix */

#include<stdio.h>

#include<conio.h>

#define ROW 3

#define COL 3

void dis(int a[][COL],int,int);

void trans(int a[][COL],int,int);

void main()

int a[ROW][COL];
21

a[0][0] = 1; a[0][1] = 2; a[0][2] = 3;

a[1][0] = 4; a[1][1] = 5; a[1][2] = 6;

a[2][0] = 7; a[2][1] = 8; a[2][2] = 9;

printf("\nThe matrix is \n");

dis(a,ROW,COL);

trans(a,ROW,COL);

printf("The tranpose of the matrix is\n");

dis(a,ROW,COL);

getch();

void dis(int d[3][3 ],int i,int k)

int j,l;

for(j=0;j<i;j++)

for(l=0;l<k;l++)

printf("%d ",d[j][l]);

printf("\n");

}
22

void trans(int mat[][3],int k ,int l)

int i,j,temp;

for(i=0;i<k;i++)

for(j=i+1;j<l;j++)

temp=mat[i][j];

mat[i][j]=mat[j][i];

mat[j][i]=temp;

output:

The matrix is

123

456

789

The tranpose of the matrix is

147

258

369
23

/* W RITE A PROGRAM TO INVERSE OF THE LIST*/

#include<stdio.h>

#include<conio.h>

void read(int *,int);

void dis(int *,int);

void inverse(int *,int);

main(){

int a[5],i;

read(a,5);

dis(a,5);

inverse(a,5);

dis(a,5);

getch();

void read(int c[],int i)

int j;

printf("Enter the list \n");

for(j=0;j<i;j++)

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

fflush(stdin);

void dis(int d[],int i)

int j;

printf("The list is \n");

for(j=0;j<i;j++)

printf("%d ",d[j]);

printf("\n");

void inverse(int inver_a[],int j)

int i,temp;

j--;

for(i=0;i<(j/2);i++)

temp=inver_a[i];

inver_a[i]=inver_a[j];

inver_a[j]=temp;

j--;

}
25

output:

Enter the list

The list is

12345

The list is

52341

/*Write a C Program to read and print name and other details of 50


students using Structure*/

#include<stdio.h>

#define SIZE 50

struct student {

char name[30];

int rollno;

int sub[3];

};

void main() {
26

int i, j, max, count, total, n, a[SIZE], ni;

struct student st[SIZE];

clrscr();

printf("Enter how many students: ");

scanf("%d", &n);

/* for loop to read the names and roll numbers*/

for (i = 0; i < n; i++) {

printf("\nEnter name and roll number for student %d : ", i);

scanf("%s", &st[i].name);

scanf("%d", &st[i].rollno);

/* for loop to read ith student's jth subject*/

for (i = 0; i < n; i++) {

for (j = 0; j <= 2; j++) {

printf("\nEnter marks of student %d for subject %d : ", i, j);

scanf("%d", &st[i].sub[j]);

/* (i) for loop to calculate total marks obtained by each student*/

for (i = 0; i < n; i++) {

total = 0;

for (j = 0; j < 3; j++) {


27

total = total + st[i].sub[j];

printf("\nTotal marks obtained by student %s are %dn",


st[i].name,total);

a[i] = total;

/* (ii) for loop to list out the student's roll numbers who

have secured the highest marks in each subject */

/* roll number who secured the highest marks */

for (j = 0; j < 3; j++) {

max = 0;

for (i = 0; i < n; i++) {

if (max < st[i].sub[j]) {

max = st[i].sub[j];

ni = i;

printf("\nStudent %s got maximum marks = %d in Subject :


%d",st[ni].name, max, j);

max = 0;

for (i = 0; i < n; i++) {

if (max < a[i]) {


28

max = a[i];

ni = i;

printf("\n%s obtained the total highest marks.", st[ni].name);

getch();

/* Write a program to print a given string in upper case using C*/

#include <stdio.h>

#include <conio.h>

void main()

char s[100];

int len,i;

clrscr();

printf("\nENTER A SENTENSE: ");

gets(s);

len=strlen(s);

printf("\n");

for(i=0;i<len;i++)
29

if(s[i]>=97 && s[i]<=122)

printf("%c",s[i]-32);

else

printf("%c",s[i]);

getch();

output:

ENTER A SENTENSE: chandan

CHANDAN

/*Write a program to Find Fibonacci Series Using C Program*/

#include<stdio.h>

int main()

int i,range;

long int arr[40];

printf("Enter the number range: ");

scanf("%d",&range);
30

arr[0]=0;

arr[1]=1;

for(i=2;i<range;i++){

arr[i] = arr[i-1] + arr[i-2];

printf("Fibonacci series is: ");

for(i=0;i<range;i++)

printf("%ld ",arr[i]);

return 0;

output:

Enter the number range: 20

Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597


2584 4181
31

/* Write a c program to compute the average of n given numbers using


pointers*/

#include <stdio.h>

#include <conio.h>

void main()

int n,*p,sum=0,i;

float avg;

clrscr();

printf("\nHOW MANY NUMBERS: ");

scanf("%d",&n);

p=(int *) malloc(n*2);

if(p==NULL)

printf("\nMEMORY ALLOCATION UNSUCCCESSFUL");

exit();

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

printf("\nENTER NUMBER %d: ",i+1);

scanf("%d",(p+i));

}
32

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

sum=sum+*(p+i);

avg=(float)sum/n;

printf("\nTHE AVERAGE OF THE NUMBERS IS %0.2f",avg);

getch();

output:

HOW MANY NUMBERS: 2

ENTER NUMBER 1: 23

ENTER NUMBER 2: 67

THE AVERAGE OF THE NUMBERS IS 45.00


33

/* Write a c program to find out the sum of series 1 + 2 + …. + n= n


(n+1)/2 */

#include<stdio.h>

int main() {

int n,i;

int sum=0;

printf("Enter the n i.e. max values of series: ");

scanf("%d",&n);

sum = (n * (n + 1)) / 2;

printf("Sum of the series: ");

for (i =1;i <= n;i++) {

if (i!=n)

printf("%d + ",i); else

printf("%d = %d ",i,sum);

return 0;

OUTPUT:

Enter the n i.e. max values of series: 5

Sum of the series: 1 + 2 + 3 + 4 + 5 = 15

Potrebbero piacerti anche