0% found this document useful (0 votes)
16 views26 pages

C Programs

Uploaded by

madhavikurusetti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views26 pages

C Programs

Uploaded by

madhavikurusetti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 1:
Find the minimum, maximum and average in an array of integers
INPUT:
#include <stdio.h>
int main()
{
int a[50], avg , s=0,i,j,temp,n;
printf("Enter n\n");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
s=s+a[i];
}
avg=s/n;
printf("Average: %d\n",avg);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

a[i]=a[j];
a[j]=temp;
}
}
}
printf("MINIMUM: %d\n",a[0]);
printf("MAXIMUM: %d\n",a[n-1]);
}
OUTPUT:
Enter n
6
Enter elements
123456
Average: 3
MINIMUM: 1
MAXIMUM: 6
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 2:
Compute mean, variance, standard deviation, sorting of an array.
INPUT:
#include <stdio.h>
#include <math.h>
int main() {
float m = 0, v = 0, s = 0, s0 = 0, s1 = 0;
int i, n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
s0 = s0 + a[i];
}
m = s0 / n;
for (i = 0; i < n; i++) {
s1 = s1 + pow((a[i] - m), 2);
}
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

v = s1 / n;
s = sqrt(v);
printf("Mean: %f\nVariance: %f\nStandard Deviation: %f\n", m, v, s);
for(i=0;i<=n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{t=a[j];
a[j]=a[i];
a[i]=t;}
}
}
for(i=0;i<n;i++)
{
printf("sorted array:%d",a[i]);
}
return 0;
}
OUTPUT:
Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

Mean: 3.000000
Variance: 2.000000
Standard Deviation: 1.414214
Sorted array:1 2 3 4 5
PROGRAM 3:
Functions: Matrices
I. Addition II. Multiplication III. Transpose
I. Addition of Matrices
INPUT:
#include <stdio.h>
int main() {
int n, i, j;
int a[50][50], b[50][50], c[50][50];
printf("Enter the size of the matrix (n x n): ");
scanf("%d", &n);
printf("Enter elements of the first matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

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


for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
printf("Resultant matrix after addition:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the size of the matrix (n x n): 2
Enter elements of the first matrix:
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

12
34
Enter elements of the second matrix:
56
78
Resultant matrix after addition:
68
10 12
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

II. Multiplication of matrices


INPUT:
#include <stdio.h>
int main() {
int n, i, j, k, s = 0;
int a[50][50], b[50][50], c[50][50];
printf("Enter the size of the matrix (n x n): ");
scanf("%d", &n);
printf("Enter elements of the first matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

s = 0;
for (k = 0; k < n; k++) {
s += a[i][k] * b[k][j];
}
c[i][j] = s;
}
}
printf("Resultant matrix after multiplication:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
Enter the size of the matrix (n x n): 2
Enter elements of the first matrix:
12
34
Enter elements of the second matrix:
56
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

78
Resultant matrix after multiplication:
19 22
43 50
III. Transpose of the matrix
INPUT:
#include <stdio.h>
int main() {
int n, i, j;
int a[50][50], b[50][50], transpose[50][50];
printf("Enter the size of the matrix (n x n): ");
scanf("%d", &n);
printf("Enter elements of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
transpose[j][i] = a[i][j];
}
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

}
printf("Transpose of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the size of the matrix (n x n): 3
Enter elements of the matrix:
123
456
789
Transpose of the matrix:
147
258
369
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 4:
Recursive and non-recursive Functions:
I. Find the factorial of a given integer II. Find the GCD III. Find x^n
I. Find the factorial of a given integer.
Recursive:
INPUT:
#include<stdio.h>
int fact(int x);
int main(){
int a;
printf("Enter number: ");
scanf("%d", &a);
int f = fact(a);
printf("Factorial of the number is: %d", f);
}
int fact(int x){
if(x >= 1){
return x * fact(x - 1);
} else {
return 1;
}
}
OUTPUT:
Enter number: 5
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

Factorial of the number is: 120


I. Find the factorial of a given integer.
Non-Recursive:
INPUT:
#include<stdio.h>
int fact(int);
int main(){
int n,a;
printf("Enter a value");
scanf("%d",&n);
a=fact(n);
printf("%d",a);
return 0;
}
int fact(int n)
{
int f=1;
while (n>0)
{
f=f*n;
n - -;
}
}
OUTPUT:
Enter a value: 5
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

120
II. Find the GCD
Recursive:
INPUT:
#include <stdio.h>
int gcd(int x, int y);
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
int g = gcd(a, b);
printf("The GCD of the two numbers is: %d\n", g);
return 0;
}
int gcd(int x, int y) {
if (y == 0) {
return x;
} else {
return gcd(y, x % y);
}
}
OUTPUT:
Enter two numbers: 56 98
The GCD of the two numbers is: 14
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

II. Find the GCD


Non-Recursive:
INPUT:
#include<stdio.h>
int gcd(int,int);
int main(){
int n1,n2,t;
printf("Enter 2 numbers:\n");
scanf("%d%d",&n1,&n2);
t=gcd(n1,n2);
printf("GCD of the two numbers is %d",t);
return 0;
}
int hcf(int n1,int n2)
{ while(n2!=0){
int t=n2;
n2=n1%n2;
n1=t;
}
return n1;
}
OUTPUT:
Enter 2 numbers:
48 18
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

GCD of the two numbers is 6


III. Find x^n
Recursive:
INPUT:
#include <stdio.h>
int power(int x, int n);
int main() {
int x, n;
printf("Enter the base and exponent: ");
scanf("%d %d", &x, &n);
int result = power(x, n);
printf("%d^%d = %d\n", x, n, result);
return 0;
}
int power(int x, int n) {
if (n == 0) {
return 1;
} else {
return x * power(x, n - 1);
}
}
OUTPUT:
Enter the base and exponent: 2 5
2^5 = 32
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

III. Find x^n


Non-Recursive:
INPUT:
#include<stdio.h>
#include<math.h>
int power(int n1,int n2);
int main()
{
int base,a,result;
printf("Enter base number:");
scanf("%d",&base);
printf("Enter a positive power:");
scanf("%d",&a);
result=power(base,a);
printf("%d^%d=%d",base,a,result);
return 0;
}
int power(int base, int a)
{ int t;
t=pow(base,a);
return t;
}
OUTPUT:
Enter base number:3
Enter a positive power:4
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

3^4=81
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 5:
Display the values of array using pointers.
INPUT:
#include<stdio.h>
int main() {
int a[10], i;
int *p;
printf("Enter array value:");
for(i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
p = a;
for(i = 0; i < 10; i++) {
printf("%d", *(p + i));
printf("\t");
}
return 0;
}
OUTPUT:
Enter array value: 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 6:
Reverse the array using pointers.
INPUT:
#include<stdio.h>
#include<stdlib.h>
int main() {
int n;
printf("Enter size of array: ");
scanf("%d", &n);
int *ptr;
ptr = (int*)malloc(n * sizeof(int));
printf("Enter the array elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &ptr[i]);
}
printf("The reverse of the array is: ");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", ptr[i]);
}
return 0;
}
OUTPUT:
Enter size of array: 5
Enter the array elements: 1 2 3 4 5
The reverse of the array is: 5 4 3 2 1
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 7:
Sum of array contents using pointers.
INPUT:
#include<stdio.h>
int main() {
int n, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int *ptr = &arr[0];
for (int i = 0; i < n; i++) {
sum += *(ptr + i);
}
printf("The sum of the array elements is: %d\n", sum);
return 0;
}
OUTPUT:
Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
The sum of the array elements is: 15
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 8:
Display the contents of a file to screen.
INPUT:
#include<stdio.h>
int main() {
FILE *fptr;
char ch;
fptr = fopen("first.txt", "r");
printf("The content of the file is: ");
while(1) {
ch = fgetc(fptr);
if (ch != EOF) {
printf("%c", ch);
} else {
break;
}
}
fclose(fptr);
return 0;
}
OUTPUT:
first.txt: Hello, this is a sample file.
The content of the file is: Hello, this is a sample file.
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 9:
Copy one file to another, replacing all lowercase characters with their
uppercase.
INPUT:
#include<stdlib.h>
#include<stdio.h>
int main() {
FILE *fp1, *fp2;
fp1 = fopen("source.txt", "r");
if (fp1 == NULL) {
printf("ERROR");
exit(0);
}
printf("File opened successfully\n");
fp2 = fopen("destination.txt", "w");
if (fp2 == NULL) {
printf("ERROR");
exit(0);
}
printf("File opened successfully\n");
char ch;
while ((ch = fgetc(fp1)) != EOF) {
fputc((ch >= 'a' && ch <= 'z') ? ch - 32 : ch, fp2);
}
fclose(fp1);
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

fclose(fp2);
return 0;
}
OUTPUT:
Content of source.txt:
Hello World
This is a Test
Content of destination.txt:
HELLO WORLD
THIS IS A TEST
BVRIT HYDERABAD College of Engineering for Women

TITLE: DATE:
PAGE NO.:

PROGRAM 10:
Count the number of times a character occurs in a text file using command line
arguments.
INPUT:
#include<stdio.h>
int main(int argc, char*argv[])
{
FILE *fp
fp=fopen(argv[1],"r");
char ch,t=argv[2][0];
int count=0;
while((ch=fgetc(fp))!=EOF)
{ if(ch==t)
{
count++;
}
}
printf("Count=%d\n",count);
fclose(fp);
return 0;
}
OUTPUT:
Input File (example.txt):
This is a sample text file.
It contains several characters.
Count = 5

You might also like