C Programs
C Programs
#include <stdio.h>
int main()
{
int a, b, c, largest;
largest = a;
if (b > largest)
{
largest = b;
}
if (c > largest)
{
largest = c;
}
return 0;
}
Output:
63
56
#include <stdio.h>
#include <string.h>
// Implementing the logic in a function
// Initializing indexes
int l = 0;
int h = strlen(str) - 1;
}
}
// Driver program
int main()
{
isPalindrome("level");
isPalindrome("radar");
isPalindrome("Simplilearn");
return 0;
Output:
#include <stdio.h>
int main()
if (num % i == 0)
{
temp++;
break;
}
} // check for the value of temp and num.
}
else
{
return 0;
}
Output:
#include <stdio.h>
int main()
{
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0)
{
// remainder contains the last digit
remainder = originalNum % 10;
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Output:
// Driver code
int main()
{
int n = 5;
printPascal(n);
return 0;
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
5. Write C program to find sum and average of n integer using linear array.
#include<stdio.h>
int main()
{
float a[100], sum=0, avg;
int i, n;
printf("Enter n: ");
scanf("%d", &n);
/* Reading array */
printf("Enter numbers:\n");
for(i=0; i< n; i++)
{
printf("a[%d] = ", i);
scanf("%f", &a[i]);
}
/* Finding sum */
for(i=0; i< n; i++)
{
sum = sum + a[i];
}
/* Calculating average */
avg = sum/n;
/* Displaying Result */
printf("Sum is %f\n", sum);
printf("Average is %f", avg);
return 0;
}
Output:
Enter n: 4
Enter numbers:
a[0] = 25
a[1] = 42
a[2] = 12
a[3] = 12
Sum is 91.000000
Average is 22.750000
6. Write C program to perform addition , multiplication, transpose on matrices.
#include<stdio.h>
#include<stdlib.h>
switch (choice) {
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
subtract(a, b, c);
printf("Subtraction of matrix: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;
case 4:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}
while(1);
return 0;
}
Output:
First Matrix:
5 6 7
8 9 10
3 1 2
Second Matrix:
1 2 3
4 5 6
7 8 9
#include <stdio.h>
int main()
{
int i, n;
return 0;
}
Output:
#include<stdio.h>
long int fact(int x);
int main()
{
int x;
printf("Enter A Number To Find Factorial: ");
scanf("%d",&x);
printf("The Factorial of %d = %ld", x, fact(x));
return 0;
}
Output:
#include <stdio.h>
int main()
{
char s1[100] = "programming ", s2[] = "is awesome";
int length, j;
// concatenate s2 to s1
for (j = 0; s2[j] != '\0'; ++j, ++length)
{
s1[length] = s2[j];
}
return 0;
}
Output:
After concatenation: programming is awesome
(ii) Reverse
// Reversing str
reverse(str);
printf("%s\n", str);
return 0;
}
Output:
dlroW olleH
return 0;
}
Output:
Comparison of str1 and str2: 1
Comparison of str2 and str3: -1
Comparison of str1 and str1: 0
#include <stdio.h>
int fact(int);
void main()
{
int sum;
sum=fact(1)/1+fact(2)/2+fact(3)/3+fact(4)/4+fact(5)/5;
printf("\n\n Function : find the sum of 1!/1+2!/2+3!/3+4!/4+5!/5 :\n");
printf("----------------------------------------------------------\n");
printf("The sum of the series is : %d\n\n",sum);
}
int fact(int n)
{
int num=0,f=1;
while(num<=n-1)
{
f =f+f*num;
num++;
}
return f;
}
Output:
#include <stdio.h>
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
int main()
{
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(x,y);
printf("Values after swap: x = %d, y = %d", x,y);
}
Output:
Values before swap: x = 10, y = 11
Values after swap: x = 10, y = 11
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(&x,&y);
printf("Values after swap: x = %d, y = %d", x,y);
}
Output:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a,n,i,j,t;
printf("How many numbers you want to be sorted: ");
scanf("%d",&n);
a=(int *)malloc(n *sizeof(int));
printf("\nEnter %d Numbers: \n\n",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", (a+i));
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(*(a+i)<*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
printf("\nAfter Sorting in Ascending Order: \n");
for(i=0;i<n;i++)
printf("\n%d",*(a+i));
return 0;
}
Output:
Enter 5 Numbers:
23
12
74
15
75
After Sorting in Ascending Order:
12
15
23
74
75
13. Write c program to display the marks sheet of a student using structure.
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
Output:
Enter information:
Enter name: veeru
Enter roll number: 678
Enter marks: 778
Displaying Information:
Name: veeru
Roll number: 678
Marks: 778.0
14. Write C program to perform following operations on the data files read from data file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char filename[] = "data.txt";
char data[MAX_LINE_LENGTH];
char query[MAX_LINE_LENGTH];
int choice;
while (1)
{
printf("\nFile Operations Menu:\n");
printf("1. Read file\n");
printf("2. Write to file\n");
printf("3. Append to file\n");
printf("4. Search in file\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear newline character from input buffer
switch (choice)
{
case 1:
readFile(filename);
break;
case 2:
printf("Enter data to write: ");
fgets(data, MAX_LINE_LENGTH, stdin);
data[strcspn(data, "\n")] = '\0'; // Remove newline character
writeFile(filename, data);
break;
case 3:
printf("Enter data to append: ");
fgets(data, MAX_LINE_LENGTH, stdin);
data[strcspn(data, "\n")] = '\0'; // Remove newline character
appendFile(filename, data);
break;
case 4:
printf("Enter data to search: ");
fgets(query, MAX_LINE_LENGTH, stdin);
query[strcspn(query, "\n")] = '\0'; // Remove newline character
searchFile(filename, query);
break;
case 5:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
void readFile(const char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL)
{
printf("Could not open file %s for reading.\n", filename);
return;
}
char line[MAX_LINE_LENGTH];
printf("Contents of %s:\n", filename);
while (fgets(line, sizeof(line), file) != NULL)
{
printf("%s", line);
}
fclose(file);
}
void writeFile(const char *filename, const char *data)
{
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Could not open file %s for writing.\n", filename);
return;
}
fprintf(file, "%s\n", data);
fclose(file);
printf("Data written to %s successfully.\n", filename);
}
void appendFile(const char *filename, const char *data)
{
FILE *file = fopen(filename, "a");
if (file == NULL) {
printf("Could not open file %s for appending.\n", filename);
return;
}
fprintf(file, "%s\n", data);
fclose(file);
printf("Data appended to %s successfully.\n", filename);
}
void searchFile(const char *filename, const char *query)
{
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s for searching.\n", filename);
return;
}
char line[MAX_LINE_LENGTH];
int found = 0;
if (!found)
{
printf("Query '%s' not found in file.\n", query);
}
fclose(file);
}
Output:
15. Write C program to copy the content of one file to another file using command line argument.
#include <stdio.h>
#include <stdlib.h>
return 0;
}
Output: