0% found this document useful (0 votes)
38 views

Assignment

1. The document discusses 10 programming assignments on C programming. The assignments cover topics like storing employee names in arrays, defining structures, passing structures as function parameters, using pointers, file handling operations like reading, writing and finding file size, and pointer arithmetic. 2. Code snippets are provided for each assignment question to demonstrate the concepts through programs. Sample inputs and outputs are also included for some programs. 3. The assignments help learn and practice important C programming concepts like arrays, structures, functions, pointers, file handling etc.

Uploaded by

Arpit Verma
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Assignment

1. The document discusses 10 programming assignments on C programming. The assignments cover topics like storing employee names in arrays, defining structures, passing structures as function parameters, using pointers, file handling operations like reading, writing and finding file size, and pointer arithmetic. 2. Code snippets are provided for each assignment question to demonstrate the concepts through programs. Sample inputs and outputs are also included for some programs. 3. The assignments help learn and practice important C programming concepts like arrays, structures, functions, pointers, file handling etc.

Uploaded by

Arpit Verma
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment

On
C Programming

Submitted By: Submitted To:


Arpit Verma Mrs. Preeti Malik
MCA- “B”
Student ID: 21271018
Roll No.: 1102190
1. The names of employees of an organization are stored in three arrays, namely
first_name, second_name, and last_name. Write a program to concatenate the three
parts into one string to be called name.

Code:-

#include<stdio.h>
struct employee
{
char first_name[100];
char second_name[100];
char last_name[100];
}n1,n2,n3;
int main()
{
printf(" ******ENTER YOUR FULL NAME******\n");
printf("enter first name: ");
scanf("%s",&n1.first_name);
printf("enter second name :");
scanf("%s",&n2.second_name);
printf("enter last name:");
scanf("%s",&n3.last_name);
printf("Name of employee:\n");
printf("\n\n*******Name of employee*******\n");
printf("Employee name : %s\t%s\t%s",n1.first_name,n2.second_name,n3.last_name);
return 0;
}

Output:-
2 . Define a structure type, struct personal that would contain person name, date of joining
and salary. Using this structure, write a program to read this information for one person from
the keyboard and print the same on the screen.

Code:-

#include<stdio.h>
struct personal{
char name[40];
int DOJ;
int salary;
}a1;
int main()
{
printf(".................YOUR PERSONAL INFORMATION.................\n");
printf("enter person name:");
scanf("%s",&a1.name);
printf("enter Date of joining:");
scanf("%d",&a1.DOJ);
printf("enter salary:");
scanf("%d",&a1.salary);
printf("...........THE DETAILS OF PERSONAL INFORMATION...........\n");
printf("Name:%s\n",a1.name);
printf("date of joining:%d\n",a1.DOJ);
printf("salary:%d",a1.salary);
}

Output:-
3. Write a simple program to illustrate the method of sending an entire structure as a
parameter to a function.

Code:-

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

struct student
{
int id;
char name[20];
float percentage;
};

void func(struct student record);

int main()
{
struct student record;

record.id=1;
strcpy(record.name, "XYZ");
record.percentage = 86.5;

func(record);
return 0;
}
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}

Output:-

4. Write a program using pointers to compute the sum of all elements stored in an array.

Code:-

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,sum=0;
int *a;
printf("Enter the size of array:");
scanf("%d",&n);
a=(int *)malloc(n * sizeof(int));
printf("Enter %d element in array:",n);
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
for(i=0;i<n;i++)
{
sum+=*(a+i);
}
printf("Sum of all element in array: %d",sum);
return 0;
}
Output:-

5. Write a program to read data from the keyboard, write it to a file called INPUT, again read
the same data from the INPUT file, and display it on the screen.

Code:-

#include<stdio.h>
int main()
{
FILE *ptr;
char n[100];
printf("Enter any sentence: ");
scanf("%[^\n]*c",n);
printf("%s",n);
ptr=fopen("Input1.txt","w");
fprintf(ptr,"%s", n);
return 0;
}

File:-
I am a student of Graphic era University.

Output:-
6. A file named DATA contains a series of integer numbers. Code a program to read these
numbers and then write all 'odd' numbers to a file to be called ODD and all `even' numbers to
a file to be called EVEN.

Code:-

#include<stdio.h>

int main()
{
FILE *f1,*f2,*f3;
int number,i, n=10;

printf("Contents of DATA file\n\n");

f1 = fopen("DATA","w");

for(i=0;i<n;i++)
{
scanf("%d",&number);
if(number==-1)
{
break;
}
putw(number,f1);
}
fclose(f1);
f1 = fopen("DATA","r");
f2 = fopen("ODD","w");
f3 = fopen("EVEN","w");

while((number = getw(f1)) != EOF)


{
if(number%2==0)
{
putw(number,f3);
}
else
{
putw(number,f2);
}
}

fclose(f1);
fclose(f2);
fclose(f3);

f2 = fopen("ODD","r");
f3 = fopen("EVEN","r");

printf("\n\n Contents of ODD file \n\n");

while((number = getw(f2)) != EOF)


{
printf("%d ",number);
}

printf("\n\nContents of EVEN file \n\n");

while((number = getw(f3)) != EOF)


{
printf("%d ",number);
}

fclose(f2);
fclose(f3);
return 0;
}

Output:-
7. Write a program to count number of lines in a file “input.txt”.

Code:-

#include <stdio.h>
int main()
{
FILE *fp;
int count = 0;
char filename[100];
char c; // To store a character read from file

// Open the file

fp = fopen("input.txt", "r");

// Check if file exists


if (fp == NULL)
{
printf("Could not open file %s",filename);
return 0;
}
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(fp);
printf("----------COUNT NUMBER OF LINES IN FILE----------\n\n");
printf("***********************************************\n");
printf("The file has %d lines.",count);
printf("\n\n***********************************************");
return 0;
}

FILE:-
C was invented to write an operating system.
C is a successor of B language.
C is a Structured language.
It produces efficient programs.
It can handle low-level activities.
It can be compiled on a variety of computers.
Denis Retiche was the developer of C Language.

Output:-

8. Design a structure book with at least four data members. Write a program to read details
of five books from user (using array of structure) and write same in a file output.txt.

Code:-

#include<stdio.h>
int main()
{
struct book{
char Book_name[50];
int No_of_page;
char author[30];
float prices;
}b[100];
int i;
FILE *ptr=NULL;
printf("*************Enter details of 5 books*************\n");
printf("--------------------------------------------------\n");
printf("\n");
for(i=0;i<5;i++){
printf("\n");
printf("enter %d book detail:\n",i+1);
printf("---------------------------\n");
printf("enter book name:");
scanf("%s",b[i].Book_name);
printf("Enter number of pages:");
scanf("%d",&b[i].No_of_page);
printf("Enter author name:");
scanf("%s",b[i].author);
printf("Enter book price:");
scanf("%f",&b[i].prices);
}
fprintf(ptr,"-------DETAILS OF FIVE BOOKS-------\n");
ptr=fopen("output.txt","w");
for(i=0;i<5;i++)
{
fprintf(ptr,"\nDetails of %d book\n",i+1);
fprintf(ptr,"----------------------\n");
printf("Book name:%s\n",b[i].Book_name);
printf("Number of pages in Book: %d\n",b[i].No_of_page);
printf("Author name: %s\n",b[i].author);
printf("Book prices: %f\n",b[i].prices);
fprintf(ptr,"Book name:%s\n",b[i].Book_name);
fprintf(ptr,"Number of pages in Book: %d\n",b[i].No_of_page);
fprintf(ptr,"Author name: %s\n",b[i].author);
fprintf(ptr,"Book prices: %f\n",b[i].prices);
fclose(ptr);
}
return 0;
}

File:-

-------DETAILS OF FIVE BOOKS-------


Details of 1 book
----------------------
Book name:Arpit
Number of pages in Book: 35
Author name: verma
Book prices: 15.000000

Details of 2 book
----------------------
Book name:harsh
Number of pages in Book: 45
Author name: dubey
Book prices: 56.000000

Details of 3 book
----------------------
Book name:nilesh
Number of pages in Book: 23
Author name: singh
Book prices: 45.000000

Details of 4 book
----------------------
Book name:naman
Number of pages in Book: 12
Author name: jain
Book prices: 89.000000

Details of 5 book
----------------------
Book name:gautam
Number of pages in Book: 35
Author name: singh
Book prices: 67.000000

Output:-
9. Write program to find size of file (in bytes).

Code:-

include <stdio.h>
int findfileSize(char f_n[]) {
FILE* fp = fopen(f_n, "r"); // opening a file in read mode
if (fp == NULL){ // checking whether the file exists or not
printf("File Not Found!\n");
return -1;
}
fseek(fp,'0L', SEEK_END);
int res = ftell(fp); //counting the size of the file
fclose(fp); //closing the file
return res;
}
int main() {
char f_n[] = { "b.txt" }; //file name is “b.txt” whose size is to be determined
int result = findfileSize(f_n);
if (result != -1){
printf("Size of the file is %d bytes \n", result); //printing the file size
}
return 0;
}

Output:-

10. Write a program to demonstrate pointer arithmetic.

Code:-

#include <stdio.h>
int main()
{
int N = 4;
int *ptr1, *ptr2;
ptr1 = &N;
ptr2 = &N;

printf("Pointer ptr1\nbefore Increment: ");


printf("%p \n", ptr1);

// Incrementing pointer ptr1;


ptr1++;

printf("\nafter Increment: ");


printf("%p \n\n", ptr1);

printf("Pointer ptr1\nbefore Decrement: ");


printf("%p \n", ptr1);

// Decrementing pointer ptr1;


ptr1--;

printf("\nafter Decrement: ");


printf("%p \n\n", ptr1);

return 0;
}

Output:-

You might also like