0% found this document useful (0 votes)
29 views8 pages

BHUSHAN

The document contains 10 C programming examples: 1) Finding min and max of an array, 2) Passing arguments by reference, 3) Defining and using a structure, 4) Copying a string, 5) Writing data to a file, 6) Transposing a matrix, 7) Finding power using pointers, 8) Finding length of a string without library functions, 9) Finding size of a structure, 10) Adding 3D arrays.

Uploaded by

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

BHUSHAN

The document contains 10 C programming examples: 1) Finding min and max of an array, 2) Passing arguments by reference, 3) Defining and using a structure, 4) Copying a string, 5) Writing data to a file, 6) Transposing a matrix, 7) Finding power using pointers, 8) Finding length of a string without library functions, 9) Finding size of a structure, 10) Adding 3D arrays.

Uploaded by

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

1.

Min max from elements:

#include <stdio.h>

int main() {
int arr[] = {5, 3, 9, 1, 7, 2};
int size = sizeof(arr) / sizeof(arr[0]);
int min = arr[0];
int max = arr[0];

for (int i = 1; i < size; i++) {


if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}

printf("minimum value: %d\n", min);


printf("maximum value: %d\n", max);

return 0;
}

2. add numbers using call by reference

#include <stdio.h>

void addNumbers(int *a, int *b, int *sum) {


*sum = *a + *b;
}

int main() {
int num1 = 5, num2 = 10, result;

addNumbers(&num1, &num2, &result);

printf("sum: %d\n", result);

return 0;
}

3. Structure book
#include <stdio.h>

struct book {
char book_name[100];
char author_name[100];
float price;
};

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

struct book books[n];

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


printf("\nEnter details for Book %d:\n", i + 1);
printf("Enter book name: ");
scanf(" %[^\n]", books[i].book_name);
printf("Enter author name: ");
scanf(" %[^\n]", books[i].author_name);
printf("Enter price: ");
scanf("%f", &books[i].price);
}

printf("\nBook Information:\n");
for (int i = 0; i < n; i++) {
printf("\nBook %d:\n", i + 1);
printf("Book Name: %s\n", books[i].book_name);
printf("Author Name: %s\n", books[i].author_name);
printf("Price: %.2f\n", books[i].price);
}

return 0;
}

4. Copy string

#include <stdio.h>
#include <string.h>
int main() {
char s1[20];
char s2[20];
printf("enter any string: ");
fgets(s1, sizeof(s1), stdin);

strcpy(s2, s1);

printf("original string s1='%s'\n", s1);


printf("copied string s2='%s'", s2);

return 0;
}

5. Write data

#include <stdio.h>

int main() {
FILE *file = fopen("bhushan.txt", "w");

if (file == NULL) {
printf("Error!\n");
return 1;
}

fprintf(file, "this is a data i am storing/adding in file.\n");

fclose(file);

return 0;
}
6. Transpose of matrix

#include <stdio.h>

int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int transpose[3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
transpose[i][j] = matrix[j][i];
}
}

printf("Transpose of the matrix:\n");


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

return 0;
}
7. Find power using pointer

#include <stdio.h>

void power(int base, int exponent, int *result) {


*result = 1;
for (int i = 0; i < exponent; i++) {
*result *= base;
}
}

int main() {
int base = 2;
int exponent = 3;
int result;

power(base, exponent, &result);

printf("%d raised to the power %d is: %d\n", base, exponent, result);

return 0;
}
8. Length of string without string library functions:

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

void main() {
char str1[30];
int i, l = 0;

printf("To find length of string:\n ");


printf("Enter a string : ");
scanf("%s", str1);

for (i = 0; str1[i] != '\0'; i++) {


l++;
}

printf("string has %d number of characters.\n", l);


printf("length of string is %s : %d\n", str1, l);
}

9. Find size of structure:

struct student {
char *name;
int age;
};

int main() {
printf("the size of struct student is %d bytes\n", sizeof(struct student));
return 0;
}

10. Addition of 3d array:

#include <stdio.h>

int main() {
int a[3][3][3] = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {{10, 11, 12}, {13, 14, 15},
{16, 17, 18}}};
int b[3][3][3] = {{{19, 20, 21}, {22, 23, 24}, {25, 26, 27}}, {{28, 29, 30}, {31,
32, 33}, {34, 35, 36}}};
int c[3][3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
c[i][j][k] = a[i][j][k] + b[i][j][k];
}
}
}

printf("the sum of 3d array is:\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
printf("%d ", c[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}

You might also like