[go: up one dir, main page]

0% found this document useful (0 votes)
11 views5 pages

C Programs Assignment 3

Uploaded by

Khadija Mansuri
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)
11 views5 pages

C Programs Assignment 3

Uploaded by

Khadija Mansuri
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/ 5

20.

Sort Array in Ascending Order


Program:
#include <stdio.h>
int main() {
int a[5], i, j, temp;
printf("Enter 5 elements: ");
for (i = 0; i < 5; i++)
scanf("%d", &a[i]);

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


for (j = i+1; j < 5; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

printf("Array in Ascending Order: ");


for (i = 0; i < 5; i++)
printf("%d ", a[i]);

return 0;
}

Sample Output:
Enter 5 elements: 25 12 45 7 30
Array in Ascending Order: 7 12 25 30 45

21. Store Roll No and Marks of 20 Students


Program:
#include <stdio.h>
int main() {
int roll[20], marks[20], i;

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


printf("Enter Roll No and Marks for student %d: ", i+1);
scanf("%d %d", &roll[i], &marks[i]);
}

printf("\nRollNo\tMarks\n");
for (i = 0; i < 20; i++) {
printf("%d\t%d\n", roll[i], marks[i]);
}

return 0;
}

Sample Output:
Enter Roll No and Marks for student 1: 1 78
...
RollNo Marks
1 78
2 65
...

22. Maximum and Minimum from Array


Program:
#include <stdio.h>
int main() {
int a[5], i, max, min;
printf("Enter 5 elements: ");
for (i = 0; i < 5; i++)
scanf("%d", &a[i]);

max = min = a[0];


for (i = 1; i < 5; i++) {
if (a[i] > max) max = a[i];
if (a[i] < min) min = a[i];
}

printf("Maximum = %d, Minimum = %d\n", max, min);


return 0;
}

Sample Output:
Enter 5 elements: 23 67 12 89 45
Maximum = 89, Minimum = 12

23. Reverse a String


Program:
#include <stdio.h>
#include <string.h>
int main() {
char str[50], rev[50];
int i, len;
printf("Enter a string: ");
gets(str);

len = strlen(str);
for (i = 0; i < len; i++) {
rev[i] = str[len - i - 1];
}
rev[len] = '\0';

printf("Reversed String: %s\n", rev);


return 0;
}

Sample Output:
Enter a string: hello
Reversed String: olleh

24. Replace Character in String


Program:
#include <stdio.h>
#include <string.h>
int main() {
char str[50], ch1, ch2;
int i;
printf("Enter a string: ");
gets(str);
printf("Enter character to replace: ");
scanf("%c", &ch1);
getchar();
printf("Enter new character: ");
scanf("%c", &ch2);

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


if (str[i] == ch1)
str[i] = ch2;
}

printf("Modified String: %s\n", str);


return 0;
}

Sample Output:
Enter a string: hello
Enter character to replace: l
Enter new character: x
Modified String: hexxo

25. Convert String to Uppercase


Program:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[50];
int i;
printf("Enter a string: ");
gets(str);

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


str[i] = toupper(str[i]);
}

printf("Uppercase String: %s\n", str);


return 0;
}

Sample Output:
Enter a string: hello world
Uppercase String: HELLO WORLD

26. Print Address of Variable using Pointer


Program:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;

printf("Value of a = %d\n", a);


printf("Address of a = %p\n", p);

return 0;
}

Sample Output:
Value of a = 10
Address of a = 0x7ffeea12b3c

27. Swap Two Values using Pointers


Program:
#include <stdio.h>
int main() {
int a, b, temp;
int *p1, *p2;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

p1 = &a;
p2 = &b;

temp = *p1;
*p1 = *p2;
*p2 = temp;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}

Sample Output:
Enter two numbers: 10 20
After swapping: a = 20, b = 10

28. Access Elements using Pointer


Program:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *p, i;

p = arr;
for (i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, *(p+i));
}
return 0;
}

Sample Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50

29. Print Address and Character of String using Pointer


Program:
#include <stdio.h>
int main() {
char str[20] = "HELLO";
char *p = str;

while (*p != '\0') {


printf("Address: %p, Character: %c\n", p, *p);
p++;
}
return 0;
}

Sample Output:
Address: 0x7ffeea12a3b, Character: H
Address: 0x7ffeea12a3c, Character: E
Address: 0x7ffeea12a3d, Character: L
Address: 0x7ffeea12a3e, Character: L
Address: 0x7ffeea12a3f, Character: O

30. Sorting using Pointer


Program:
#include <stdio.h>
int main() {
int arr[5], i, j, temp;
int *p = arr;

printf("Enter 5 elements: ");


for (i = 0; i < 5; i++) {
scanf("%d", (p+i));
}

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


for (j = i+1; j < 5; j++) {
if (*(p+i) > *(p+j)) {
temp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = temp;
}
}
}

printf("Sorted Array: ");


for (i = 0; i < 5; i++) {
printf("%d ", *(p+i));
}

return 0;
}

Sample Output:
Enter 5 elements: 50 10 40 20 30
Sorted Array: 10 20 30 40 50

You might also like