Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

C Programming

The document contains 11 C programs written by Shailav Malik to perform various operations on arrays such as inputting and printing an array, finding the smallest element, swapping the smallest and largest elements, finding the second largest element, checking for duplicate elements, inserting an element at a specific location, inserting in a sorted array, deleting an element from a specific position, deleting from a sorted array, merging two sorted arrays, and implementing linear search. Each program includes the necessary header files, main function, other functions, comments, and sample input/output.

Uploaded by

Alok Jangra
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)
67 views

C Programming

The document contains 11 C programs written by Shailav Malik to perform various operations on arrays such as inputting and printing an array, finding the smallest element, swapping the smallest and largest elements, finding the second largest element, checking for duplicate elements, inserting an element at a specific location, inserting in a sorted array, deleting an element from a specific position, deleting from a sorted array, merging two sorted arrays, and implementing linear search. Each program includes the necessary header files, main function, other functions, comments, and sample input/output.

Uploaded by

Alok Jangra
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/ 57

(Program 1)

Program to input and print an array

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
// input array
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// print array
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
getch();
return 0;
}

// code written by Shailav Malik

1|Page
(Program 2)
Program to find smallest element in an array of
integers

#include <stdio.h>

int smallestElement(int arr[], int size)


{
int smallest = 0;
for (int i = 0; i < size; i++)
{
if (arr[i] < arr[smallest])
smallest = i;
}
return arr[smallest];
}

int main()
{
int arr[] = {2, 5, -5, 15, 0};
int n = sizeof(arr) / sizeof(int);

printf("Smallest Element: %d", smallestElement(arr,


n));

return 0;
}

// code written by Shailav Malik

2|Page
(Program 3)
Program to swap the smallest and the largest
element in an array of integers

#include <stdio.h>

void swapLargestSmallest(int *arr, int n)


{
// find smallest
int smallest = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] < arr[smallest])
smallest = i;
}

// find largest
int largest = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] > arr[largest])
largest = i;
}
// swap smallest and largest element
int temp = arr[smallest];
arr[smallest] = arr[largest];
arr[largest] = temp;
}

int main()
{
int arr[] = {9, 4, 8, -10, 1, 21};
int n = sizeof(arr) / sizeof(int);

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

3|Page
{
printf("%d ", arr[i]);
}
printf("\n");

swapLargestSmallest(arr, n);

printf("After Swap\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

// code written by Shailav Malik

4|Page
(Program 4)
Program to Find second largest element in an array
of integers

#include <stdio.h>
#include <limits.h>

int secondLargest(int arr[], int size)


{
// find largest
int largest = 0;
for (int i = 0; i < size; i++)
{
if (arr[i] > arr[largest])
largest = i;
}

// now find second largest by neglecting 'largest'


index of array
int secondLargest = INT_MIN;
for (int i = 0; i < size; i++)
{
// skip largest element
if (i == largest)
continue;

if (arr[i] > arr[secondLargest])


secondLargest = i;
}
return arr[secondLargest];
}

int main()
{
int arr[] = {3, 1, 19, 2, 15};

5|Page
int n = sizeof(arr) / sizeof(int);

printf("Second Largest: %d", secondLargest(arr, n));

return 0;
}

// code written by Shailav Malik

6|Page
(Program 5)
Program to Find whether an array of integers
contains a duplicate element

#include <stdio.h>
#include <stdbool.h>

bool hasduplicateElements(int arr[], int size)


//...O(n^2)
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
return true;
}
}
return false;
}

int main()
{
int arr[] = {2, 3, 1, 9, 4, 1};
int n = sizeof(arr) / sizeof(int);

if (hasduplicateElements(arr, n))
printf("Array has duplicate Elements");
else
printf("Array doesn't have duplicate Elements");

return 0;
}

// code written by Shailav Malik

7|Page
(Program 6)
Program to Insert an element at a specific location
in an array of integers

#include <stdio.h>
#include <stdlib.h>

void insertElement(int arr[], int *n, int pos, int val)


// 8
{
for (int i = *n - 1; i >= pos - 1; i--)
{
arr[i + 1] = arr[i];
}
arr[pos - 1] = val;
++*n;
}

int main()
{
int n = 7;
int *arr = (int *)calloc(n, sizeof(int));

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


{
arr[i] = i + 1;
}

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


{
printf("%d ", arr[i]);
}

int pos;

8|Page
int val;
printf("\nEnter position & number to be inserted:
");
scanf("%d", &pos);
scanf("%d", &val);
if (pos < 1 || pos > n + 1)
{
printf("Enter valid position!!");
exit(1);
}
insertElement(arr, &n, pos, val);
printf("\nAfter Inserting element\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

free(arr);

return 0;
}

// code written by Shailav Malik

9|Page
(Program 7)
Program to Insert an element in a sorted array of
integers

// Program to insert a number in a sorted array


#include <stdio.h>
#include <stdlib.h>

void insertNumSorted(int *arr, int *size, int num)


{
int pos = 0;
for (int i = 0; i < *size; i++)
{
if (num < arr[i])
{
pos = i + 1;
break;
}
}
if (pos == 0)
pos = *size + 1;

for (int i = *size - 1; i >= pos - 1; i--)


{
arr[i + 1] = arr[i];
}

arr[pos - 1] = num;
++*size;
}

int main()
{
int n = 6;
int *arr = (int *)malloc(n * sizeof(int));

10 | P a g e
arr[0] = 2;
arr[1] = 9;
arr[2] = 11;
arr[3] = 15;
arr[4] = 18;
arr[5] = 20;
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

int num;
printf("\nEnter the value to be inserted: ");
scanf("%d", &num);
insertNumSorted(arr, &n, num);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

free(arr);

return 0;
}

// code written by Shailav Malik

11 | P a g e
(Program 8)
Program to Delete an element from a specific
position in an array

#include <stdio.h>
#include <stdlib.h>

void deleteElement(int *arr, int *n, int pos)


{
for (int i = pos; i < *n; i++)
{
arr[i - 1] = arr[i];
}

--*n;
}

int main()
{
int arr[] = {2, 8, 9, 1, 5, 14};
int n = sizeof(arr) / sizeof(int);

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


{
printf("%d ", arr[i]);
}

int pos;
printf("\nEnter position of element to be deleted:
");
scanf("%d", &pos);
if (pos < 1 || pos > n)
{
printf("Enter valid position!!");
exit(1);

12 | P a g e
}
deleteElement(arr, &n, pos);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

// code written by Shailav Malik

13 | P a g e
(Program 9)
Program to Delete an element in a sorted array of
integers

#include <stdio.h>
#include <stdlib.h>

void deleteElement(int *arr, int *n, int num)


{
int pos = 0;
for (int i = 0; i < *n; i++)
{
if (num == arr[i])
{
pos = i + 1;
break;
}
}

if (pos == 0)
pos = *n;

for (int i = pos; i < *n; i++)


{
arr[i - 1] = arr[i];
}

--*n;
}

int main()
{
int arr[] = {1, 2, 5, 8, 9, 14};
int n = sizeof(arr) / sizeof(int);

14 | P a g e
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

int num;
printf("\nEnter the number to be deleted: ");
scanf("%d", &num);

deleteElement(arr, &n, num);


for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

// code written by Shailav Malik

15 | P a g e
(Program 10)
Program to merge two sorted arrays

// Program to merge two sorted arrays


#include <stdio.h>

void mergeArrays(int *arr1, int n, int *arr2, int m, int


*arr3)
{
int i = 0, j = 0;
int index = 0;
while (i != n && j != m)
{
if (arr1[i] <= arr2[j])
arr3[index++] = arr1[i++];
else
arr3[index++] = arr2[j++];
}

// copy remaining elements to arr3 as it is


while (i != n)
{
arr3[index++] = arr1[i++];
}

while(j!=m)
{
arr3[index++]=arr2[j++];
}
}

int main()
{
int arr1[] = {1, 4, 7, 9, 11};
int n = sizeof(arr1) / sizeof(int);

16 | P a g e
printf("Array1: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr1[i]);
}

int arr2[] = {7, 9, 13, 15, 19};


int m = sizeof(arr2) / sizeof(int);
printf("\nArray2: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr2[i]);
}

int arr3[10];
printf("\n\nMerged Array: ");
mergeArrays(arr1, n, arr2, m, arr3);
for (int i = 0; i < n + m; i++)
{
printf("%d ", arr3[i]);
}

return 0;
}

// code written by Shailav Malik

17 | P a g e
(Program 11)
Program to Implement Linear Search
// program to search an element in an array
#include <stdio.h>

int searchTarget(int *arr, int n, int target)


{
for (int i = 0; i < n; i++)
{
if (arr[i] == target)
return i + 1;
}

return -1;
}

int main()
{
int arr[] = {2, 3, 9, 1, 13, 5};
int n = sizeof(arr) / sizeof(int);
int target;
printf("Enter the target to be searched: ");
scanf("%d", &target);
int targetIndex = searchTarget(arr, n, target);

if (targetIndex == -1)
printf("Element doesn't exist");
else
printf("%d is at %dth position", target,
targetIndex);

return 0;
}

// code written by Shailav Malik

18 | P a g e
(Program 12)
Program to read and print an array of N numbers
using function

#include <stdio.h>
#include <stdlib.h>

void inputArray(int arr[], int size)


{
printf("\nInputArray function callled:\n");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
}

void printArray(int arr[], int size)


{
printf("\nPrintArray function callled:\n");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}

int main()
{
int n;
printf("Enter the size of array: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));

inputArray(arr, n);
printArray(arr, n);

19 | P a g e
return 0;
}

// code written by Shailav Malik

20 | P a g e
(Program 13)
Program to swap the largest and the smallest
numbers in an array using function

#include <stdio.h>

void swapLargestSmallest(int *arr, int n)


{
printf("\nSwapLargestSmallest function called:\n");
// find smallest
int smallest = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] < arr[smallest])
smallest = i;
}

// find largest
int largest = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] > arr[largest])
largest = i;
}
// swap smallest and largest element
int temp = arr[smallest];
arr[smallest] = arr[largest];
arr[largest] = temp;
}

int main()
{
int arr[] = {9, 4, 8, -10, 1, 21};
int n = sizeof(arr) / sizeof(int);

21 | P a g e
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");

swapLargestSmallest(arr, n);

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


{
printf("%d ", arr[i]);
}

return 0;
}

// code written by Shailav Malik

22 | P a g e
(Program 14)
Program to print the elements of a 2-D array

#include <stdio.h>

int main()
{
int arr[][4] = {{1, 2, 3, 4},
{5, 9, 4, 4},
{0, -1, 6, 4}};

int col = sizeof(arr[1]) / sizeof(int);


int row = sizeof(arr) / (sizeof(int) * col);
printf("\nPrinting 2-D Array:\n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}

// code written by Shailav Malik

23 | P a g e
(Program 15)
Program to generate pascal triangle

#include <stdio.h>

int nCr(int n, int r)


{
if (r > n)
return -1;
if (r > n - r)
r = n - r;
int numr = 1;
int denm = 1;
for (int i = 1; i <= r; i++)
{
numr *= n;
n--;

denm *= i;
}
return (numr / denm);
}

int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

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


{
for (int s = 0; s < n - i; s++)
{
printf(" ");
}

24 | P a g e
for (int j = 0; j <= i; j++)
{
printf("%d ",nCr(i,j));
}
printf("\n");
}

return 0;
}

// code written by Shailav Malik

25 | P a g e
(Program 16)
Program to find the length of a string

// program to find length of a string


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

int main()
{
char string1[5];
printf("Enter the string: ");
gets(string1);
printf("\nstring1: %s\n", string1);
printf("\nLength of string string1: %d\n",
strlen(string1));

return 0;
}

// code written by Shailav Malik

26 | P a g e
(Program 17)
Program to convert characters of a string to upper
case

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

int main()
{
char string1[100];
printf("Enter the string: ");
gets(string1);
printf("\nstring1: %s\n", string1);

strupr(string1);
printf("\nconverting string1 to uppercase: \n");
puts(string1);

return 0;
}

// code written by Shailav Malik

27 | P a g e
(Program 18)
Program to convert characters of a string to lower
case

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

int main()
{
char string1[100];
printf("Enter the string: ");
gets(string1);
printf("\nstring1: %s\n", string1);

strlwr(string1);
printf("\nconverting string1 to lowercase: \n");
puts(string1);

return 0;
}

// code written by Shailav Malik

28 | P a g e
(Program 19)
Program to concatenate two strings

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

int main()
{
char string1[20] = "Shailav ";
printf("string1: %s\n", string1);
char string2[20] = "Malik";
printf("string2: %s\n", string2);

strcat(string1, string2);
printf("\nAfter concatenation,\nstring1: %s",
string1);

return 0;
}

// code written by Shailav Malik

29 | P a g e
(Program 20)
Program to append a string to another string

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

int main()
{
char string1[20] = "Ram";
printf("string1: %s\n", string1);
char string2[20] = "aman";
printf("string2: %s\n", string2);

strncat(string1, string2, 1);


printf("\nAfter appending first character of second
string to first string,\nstring1: ");

puts(string1);

return 0;
}

// code written by Shailav Malik

30 | P a g e
(Program 21)
Program to compare two strings

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

int main()
{
char string1[20] = "Shailav";
printf("string1: %s\n", string1);
char string2[20] = "Shailove";
printf("string2: %s\n", string2);

int c=strcmp(string1,string2);

if (!c)
printf("\nBoth strings are same");
else
printf("\nstring1 & string2 are different");

return 0;
}

31 | P a g e
(Program 22)
Program to reverse the given string

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

int main()
{
char string1[10];
printf("Enter the string: ");
scanf("%s", &string1);
printf("string1: %s\n", string1);

strrev(string1);
printf("\nAter reversing string,\nstring1: %s",
string1);

return 0;
}

32 | P a g e
(Program 23)
Program to read and print the names of n students
of a class

#include <stdio.h>

int main()
{
int n;
printf("Enter the no. of students: ");
scanf("%d", &n);

char name[10][10];
printf("Now enter the names: \n");
for (int i = 0; i < n; i++)
{
scanf("%s", &name[i]);
}

printf("\nprinting names: \n");


for (int i = 0; i < n; i++)
{
printf("%s\n", name[i]);
}

return 0;
}

33 | P a g e
(Program 24)
Program to read a sentence and count number of
words in it

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

int noOfWords(char string[])


{
int words = 1;
for (int i = 0; string[i] != '\0'; i++)
{
if (string[i] == ' ')
words++;
}

return words;
}

int main()
{
char string1[50];
printf("Enter the string: ");
gets(string1);

printf("No of words: %d", noOfWords(string1));

return 0;
}

34 | P a g e
(Program 25)
Program to delete last character of a string

// program to delete last char of a string


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

int main()
{
char string1[50];
printf("Enter the string: ");
gets(string1);
int prevChar = -1;
for (int i = 0; string1[i] != '\0'; i++)
{
prevChar++;
}

string1[prevChar] = '\0';
printf("\nstring1 after deleting last character:
\n");
puts(string1);

return 0;
}

35 | P a g e
(Program 26)
Program to delete first character of a string

#include <stdio.h>

void deleteFirstChar(char *string1)


{
int i;
for (i = 0; string1[i + 1] != '\0'; i++)
{
string1[i] = string1[i + 1];
}
string1[i] = '\0';
}

int main()
{
char string1[50];
printf("Enter the string: ");
gets(string1);

deleteFirstChar(string1);
printf("\nstring1 after deleting first
character:\n%s", string1);

return 0;
}

36 | P a g e
(Program 27)
Program to print “Hello World” using pointers

// program to print hello world using pointers


#include <stdio.h>

int main()
{
char st[] = "Hello World";
char *ptr = &st[0];

printf("\nPrinting the string using pointers: \n") ;


for (int i = 0; st[i] != '\0'; i++)
{
printf("%c", *ptr);
ptr++;
}
return 0;
}

37 | P a g e
(Program 28)
Program to convert a floating-point number into an
integer using pointer

#include <stdio.h>

int main()
{
float num = 4.55;
float *ptr = &num;
printf("num = %f\n", *ptr);

printf("\nAfter typecasting num to int:\nnum = %d",


(int)num);

return 0;
}

38 | P a g e
(Program 29)
Program to find biggest of three numbers using
pointer

#include <stdio.h>

int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
int *p1 = &a;
int *p2 = &b;
int *p3 = &c;

if (*p1 >= *p2 && *p1 >= *p3)


printf("%d is biggest", *p1);
else if (*p2 > *p3)
printf("%d is biggest", *p2);
else
printf("%d is biggest", *p3);

return 0;
}

39 | P a g e
(Program 30)
Program to test whether a number is positive,
negative or zero using pointer

#include <stdio.h>

int main()
{
int num, *ptr;
printf("Enter the number: ");
scanf("%d", &num);
ptr = &num;

if (*ptr > 0)
printf("positive", num);
else if (*ptr < 0)
printf("negative", num);
else
printf("zero", num);

return 0;
}

40 | P a g e
(Program 31)
Program to print all even numbers from m to n
using pointer

#include <stdio.h>

int main()
{
int m, n, *ptr,firstEven;
printf("Enter the starting and ending number: ");
scanf("%d%d", &m, &n);
ptr=&m;
// find first even number and then keep adding 2 to
it
// until it is less than n
if (*ptr & 1) // condition for odd
firstEven = *ptr + 1;
else
firstEven = *ptr;

printf("\nPrinting even numbers from %d to %d: \n",


m, n);
for (int i = firstEven; i <= n; i = i + 2)
{
printf("%d ", i);
}

return 0;
}

41 | P a g e
(Program 32)
Program to read and display information of a
student using structure

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

struct student
{
char name[15];
int marks;
float percent;
int rollNo;
} student1;

int main()
{
printf("Enter name: ");
gets(student1.name);
printf("Enter rollNo: ");
scanf("%d", &student1.rollNo);
printf("Enter marks: ");
scanf("%d", &student1.marks);
student1.percent = ((float)student1.marks / 600) *
100;

printf("\nPrinting student details: \n");


printf("Name: %s\n", student1.name);
printf("RollNo: %d\n", student1.rollNo);
printf("Marks: %d\n", student1.marks);
printf("Percent: %f%%", student1.percent);

return 0;
}

42 | P a g e
(Program 33)
Program to find biggest of three numbers using
structure

#include <stdio.h>

struct comparison
{
int a;
int b;
int c;
};

int main()
{
struct comparison comp;
printf("Enter three numbers: ");
scanf("%d%d%d", &comp.a, &comp.b, &comp.c);

printf("\nprinting biggest of three no. using


structure:\n");
if (comp.a >= comp.b && comp.a >= comp.c)
printf("%d is biggest", comp.a);
else if (comp.b > comp.c)
printf("%d is biggest", comp.b);
else
printf("%d is biggest", comp.c);

return 0;
}

43 | P a g e
(Program 34)
Program to read and display information of a
student using structure within a structure.

#include <stdio.h>

struct student
{
char name[15];
int rollNo;

// nesting structure
struct marks
{
int totalMarks;
int maxMarks;
float percent;
} m;
};

int main()
{
struct student s1;
// inputing student information
printf("Enter name: ");
gets(s1.name);
printf("Enter rollNo: ");
scanf("%d", &s1.rollNo);

printf("Enter Max Marks: ");


scanf("%d", &s1.m.maxMarks);
printf("Enter Total marks obtained: ");
scanf("%d", &s1.m.totalMarks);

44 | P a g e
// calculating percentage
s1.m.percent = ((float)s1.m.totalMarks /
s1.m.maxMarks) * 100;

printf("\nprinting student information: \n");


printf("Name: %s\n", s1.name);
printf("Roll No: %d\n", s1.rollNo);
printf("Max Marks: %d\n", s1.m.maxMarks);
printf("Obtained Marks: %d\n", s1.m.totalMarks);
printf("Percentage: %f%%\n", s1.m.percent);

return 0;
}

45 | P a g e
(Program 35)
Program to read and display information of all
students in the class using array of structures.

#include <stdio.h>

struct students
{
char name[10];
int rollNo;
char phone[10];
};

int main()
{
int n;
printf("Enter the no. of students: ");
scanf("%d", &n);
struct students s[2];
// inputing data
for (int i = 0; i < n; i++)
{
printf("Student %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", &s[i].name);;
printf("Enter Roll No: ");
scanf("%d", &s[i].rollNo);
printf("Enter Mobile No: ");
scanf("%s", &s[i].phone);
printf("\n");
}
// printing data
printf("\nPrinting students details: \n");
for (int i = 0; i < n; i++)
{

46 | P a g e
printf("Student %d:\n", i + 1);
printf("Name: %s\n", s[i].name);
printf("Roll No: %d\n", s[i].rollNo);
printf("Mobile No: %s\n", s[i].phone);
printf("\n");
}

return 0;
}

47 | P a g e
(Program 36)
Program to read a file character by character, and
display it simultaneously on the screen

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fptr = fopen("36.txt", "r");
if (fptr == NULL)
{
printf("File doesn't exist\n");
return -1;
}

printf("Printing file 36.txt: \n\n\"");


while (1)
{
char ch = fgetc(fptr);
if (ch == EOF)
break;
printf("%c", ch);
}
fclose(fptr);
printf("\"\n\n36.txt closed successfully");

return 0;
}

48 | P a g e
(Program 37)
Program to count number of characters and lines
in a file

#include <stdio.h>

int main()
{
FILE *fptr = fopen("36.txt", "r");
if (fptr == NULL)
{
printf("File doesn't exist\n");
return -1;
}
int characters = 0;
int lines = 1;

printf("Reading file 36.txt:\n\n");


while (1)
{
char ch = fgetc(fptr);
if (feof(fptr))
break;

characters++;
if (ch == '\n')
lines++;
}

printf("No. of characters: %d\n", characters);


printf("No. of lines: %d\n", lines);

return 0;
}

49 | P a g e
(Program 38)
Program to print the text of a file on screen by
printing the text line by line and displaying line
numbers before the text in each line & Using
command line argument to enter the filename

#include <stdio.h>

int main(int argc, char *file[])


{
FILE *ptr = fopen(file[1], "r");
if (ptr == NULL)
{
printf("Error in opening file");
return -1;
}
int line = 0;
while (1)
{
if (feof(ptr))
break;
char s[100];
fgets(s, 100, ptr);
line++;
printf("%d) ", line);
puts(s);
}

return 0;
}

50 | P a g e
(Program 39)
Program to copy one file into another (copy one
character at a time)
// Program to copy 36.txt to new.txt
#include <stdio.h>

int main()
{
printf("Opening file 36.txt in read mode \n");
FILE *fptr1 = fopen("36.txt", "r");
if (fptr1 == NULL)
{
printf("File doesn't exist\n");
return -1;
}
printf("Opening file new.txt in write mode \n");
FILE *fptr2 = fopen("new.txt", "w");

printf("\nCopying 36.txt to new.txt: \n\n");


while (1)
{
char ch = fgetc(fptr1);
if (feof(fptr1))
break;
fputc(ch, fptr2);
}

fclose(fptr1);
printf("file 36.txt closed successfully\n");
fclose(fptr2);
fclose(fptr2);
printf("file new.txt closed successfully\n");

return 0;
}

51 | P a g e
(Program 40)
Program to print half pyramid of *

#include <stdio.h>

int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

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


{
for (int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}

return 0;
}

//code written by Shailav Malik

52 | P a g e
(Program 41)
Program to print half pyramid of numbers

#include <stdio.h>

int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

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


{
for (int j = 1; j <= i; j++)
{
printf("%d ", i);
}
printf("\n");
}

return 0;
}

//code written by Shailav Malik

53 | P a g e
(Program 42)
Program to print half pyramid of alphabets

#include <stdio.h>

int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

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


{
for (int j = 1; j <= i; j++)
{
printf("%c ", (char)('A' + i - 1));
}
printf("\n");
}

return 0;
}

//code written by Shailav Malik

54 | P a g e
(Program 43)
Program to print inverted half pyramid of numbers

#include <stdio.h>

int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

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


{
for (int j = 1; j <= n - i + 1; j++)
{
printf("%d ", j);
}
printf("\n");
}

return 0;
}

//code written by Shailav Malik

55 | P a g e
(Program 44)
Program to print full pyramid of *

#include <stdio.h>

int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

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


{
for (int s = 1; s <= n - i; s++)
{
printf(" ");
}

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


{
printf("* ");
}
printf("\n");
}

return 0;
}

//code written by Shailav Malik

56 | P a g e
(Program 45)
Program to print inverted full pyramid of *

#include <stdio.h>

int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

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


{
for (int s = 1; s <= i - 1; s++)
{
printf(" ");
}

for (int j = 1; j <= n - i + 1; j++)


{
printf("* ");
}
printf("\n");
}

return 0;
}

//code written by Shailav Malik

57 | P a g e

You might also like