Pps Filefile
Pps Filefile
15. Write a program in C to read data from a text file and display it.
int main()
int i, largest;
largest = array[0];
largest = array[i];
return 0;
Output :
Program 2: Write a program in C to find the average of
elements in an array.
#include <stdio.h>
int main()
int n, i;
scanf("%d", &n);
scanf("%d", &n);
scanf("%f", &num[i]);
sum += num[i];
avg = sum / n;
return 0;
Output:
Program 3: Write a program in C to search for an element in
an array using linear search.
#include <stdio.h>
void linear_search(int a[], int n, int key)
{
int i, count = 0;
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
printf("The element is found at %d position\n", i+1);
count = count + 1;
}
}
if(count == 0)
printf("The element is not present in the array\n");
}
int main()
{
int i, n, key;
n = 6;
int a[10] = {12, 44, 32, 18, 4, 10};
key = 18;
linear_search(a, n, key);
key = 23;
linear_search(a, n, key);
return 0;
}
Output:
Program 4: Write a program in C to concatenate two strings
without using library functions.
#include<stdio.h>
void main()
{
char str1[25] , str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
Output:
Program 5: Write a program in C to swap two numbers
using pointers.
#include <stdio.h>
void swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("\nEnter Value of y ");
scanf("%d", &y);
swap(&x, &y);
printf("\nAfter Swapping: x = %d, y = %d", x, y);
return 0;
}
Output:
Program 6: Write a program in C to reverse an array using
pointers.
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void reverse(int array[], int array_size)
{
int *pointer1 = array,
*pointer2 = array + array_size - 1;
while (pointer1 < pointer2) {
swap(pointer1, pointer2);
pointer1++;
pointer2--;
}
}
void print(int* array, int array_size)
{
int *length = array + array_size,
*position = array;
printf("Array = ");
for (position = array; position < length; position++)
printf("%d ", *position);
}
int main()
{
int array[] = { 2, 4, -6, 5, 8, -1 };
printf("Original ");
print(array, 6);
printf("Reverse ");
reverse(array, 6);
print(array, 6);
return 0;
}
Output:
Program7: Write a program in C to find the sum of elements
in an array using pointers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sum(int arr[], int n)
{
if (n == 0) {
return 0;
}
else {
return arr[0] + sum(arr + 1, n - 1);
}
}
int main()
{
int arr[] = { 12, 3, 4, 15 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", sum(arr, n));
return 0;
}
Output:
Program 8: Write a program in C to define a structure
representing a student with attributes like name, roll
number, and marks.
#include <stdio.h>
struct Student {
char* firstName[50];
int roll_number;
float marks;
} s[5];
int main()
{
int i;
printf("Enter information of students:\n");
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
Output:
Program 9: Write a program in C to calculate the total marks
and average marks of students using structures.
#include <stdio.h>
struct student
{
int sub1;
int sub2;
int sub3;
};
void main()
{
struct student s[10];
int i,total=0,av;
for(i=0;i<=2;i++)
{
printf("\nEnter Marks in Three Subjects");
scanf("%d%d%d",&s[i].sub1,&s[i].sub2,&s[i].sub3);
total=s[i].sub1+s[i].sub2+s[i].sub3;
av= total/3;
printf(“\n average marks of students =%d”,av);
}
Output:
Program 10: Write a program in C to find the student with
the highest marks using structures.
#include<stdio.h>
struct student
{
char name[30];
int roll;
float marks;
};
int main()
{
/* Declaration of array of structure */
struct student s[20], lg;
int i,n;
printf("Enter n:\n");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter name, roll and marks of student:\n");
scanf("%s%d%f",s[i].name, &s[i].roll, &s[i].marks);
}
lg = s[0];
for(i=0;ilg.marks)
{
lg = s[i];
}
}
Output:
Program 11: Write a program in C to find the factorial of a
number using a recursive function.
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
Output:
Program 12: Write a program in C to check whether a
number is prime using a function.
#include <stdio.h>
void checkPrime(int N)
{
int flag = 1;
for (int i = 2; i <= N / 2; i++) {
if (N % i == 0) {
flag = 0;
break;
}
}
if (flag) {
printf("The number %d is a Prime Number\n", N);
}
else {
printf("The number %d is not a Prime Number\n", N);
}
return;
}
int main()
{
int N = 546;
checkPrime(N);
return 0;
}
Output:
Program 13: Write a program in C to calculate the area of a
circle using a function.
#include <math.h>
#include <stdio.h>
#define PI 3.142
// Driver code
int main()
{
printf("Area is %f", findArea(5));
return 0;
}
Output:
Program 14: Write a program in C to create and write data
to a text file.
#include <stdio.h>
main() {
FILE *fp;
char buff[255];
fp = fopen("/tmp/test.txt", "r");
fclose(fp);
Output:
Program 15: Write a program in C to read data from a text
file and display it.
#include <stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(){
char ch;
FILE *fp;
fp=fopen("std1.txt","w");
printf("enter the text.press cntrl Z:");
while((ch = getchar())!=EOF){
putc(ch,fp);
}
fclose(fp);
fp=fopen("std1.txt","r");
printf("text on the file:");
while ((ch=getc(fp))!=EOF){
if(ch == ',')
printf("\t\t");
else
printf("%c",ch);
}
fclose(fp);
return 0;
}
Output:
Program 16: Write a program in C to copy contents from
one file to another.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main(){
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading ");
scanf("%s",filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL){
printf("Cannot open file %s ", filename);
exit(0);
}
printf("Enter the filename to open for writing ");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL){
printf("Cannot open file %s ", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF){
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("Contents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;}
output:
Program 17: Write a program in C to dynamically allocate
memory for an array and input its elements.
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Memory allocated
printf("Memory successfully allocated using "
"malloc.\n");
Output:
Program 18: Write a program in C to use #define to define
constants for mathematical constants like pi.
#include <stdio.h>
#define PI 3.14159 // <-- Define constant PI
int main()
{
float radius, area;
printf("Enter radius: ");
scanf("%f", &radius);
return 0;
}
Output:
Program 19: Write a program in C to demonstrate the use of
#ifdef, #ifndef, #else, and #endif directives.
#include <stdio.h>
#define gfg 7
#if gfg > 200
#undef gfg
#define gfg 200
#elif gfg < 50
#undef gfg
#define gfg 50
#else
#undef gfg
#define gfg 100
#endif
void printValue(int value) { printf("%d", value); }
int main()
{
printValue(gfg); // gfg = 50
return 0;
}
Output: