Assignment
Assignment
On
C Programming
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;
};
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;
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");
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen("ODD","r");
f3 = fopen("EVEN","r");
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
fp = fopen("input.txt", "r");
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 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:-
Code:-
#include <stdio.h>
int main()
{
int N = 4;
int *ptr1, *ptr2;
ptr1 = &N;
ptr2 = &N;
return 0;
}
Output:-