Set A C Program Solutions
Set A C Program Solutions
1. Write a program to read two integers using pointers and perform all
arithmetic operations on them.
#include <stdio.h>
int main() {
int num1, num2, sum, diff, prod, quot, rem;
int *ptr1, *ptr2;
ptr1 = &num1;
ptr2 = &num2;
return 0;
}
#include <stdio.h>
int main() {
int num;
checkEvenOrOdd(&num);
return 0;
}
#include <stdio.h>
int main() {
int num1, num2;
findMax(&num1, &num2);
return 0;
}
#include <stdio.h>
printf("\n");
}
int main() {
int n;
int arr[n];
displayReverse(arr, n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
printf("\n");
}
int main() {
int n;
displayReverse(arr, n);
free(arr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
free(arr);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], concat[200];
if (cmp > 0) {
strcpy(concat, str1);
strcat(concat, str2);
printf("The concatenated string is: %s\n", concat);
}
else if (cmp < 0) {
strcpy(concat, str2);
strcat(concat, str1);
printf("The concatenated string is: %s\n", concat);
}
else {
printf("The length of the strings is: %ld\n", strlen(str1));
}
return 0;
}
3.Write a program to read a string and one character. Check whether given
character is
present in the given string or not? (Hint: use strchr or strrchr)
#include <stdio.h>
#include <string.h>
int main() {
char str[100], ch, *result;
if (result == NULL) {
printf("The character '%c' is not present in the string '%s'.\n", ch,
str);
}
else {
printf("The character '%c' is present in the string '%s'.\n", ch,
str);
}
return 0;
}
1. Write a program to accept a string and find its length using user
defined function.
(Don’t use pointers)
#include <stdio.h>
int main() {
char str[100];
return 0;
}
return len;
}
#include <stdio.h>
#include <ctype.h>
str_to_upper(str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
reverse_string(str);
return 0;
}
#include <stdio.h>
struct employee {
int id;
char name[50];
float salary;
};
int main() {
int n, i;
printf("\nEmployee Information:\n");
printf("ID\tName\tSalary\n");
for (i = 0; i < n; i++) {
printf("%d\t%s\t%.2f\n", emp[i].id, emp[i].name, emp[i].salary);
}
return 0;
}
#include <stdio.h>
int main() {
char filename[50], ch;
int count = 0;
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
fclose(fp);
return 0;
}
#include <stdio.h>
#include <ctype.h>
int main() {
char filename1[50], filename2[50];
char ch;
FILE *fp1, *fp2;
fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
#include <stdio.h>
int main() {
char filename[50], ch;
FILE *fp;
int wordCount = 0, lineCount = 0, charCount = 0;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
if (ch == '\n') {
lineCount++;
}
fclose(fp);
return 0;
}
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, area, perimeter;
return 0;
}
#include <stdio.h>
return 0;
}
#include <stdio.h>
#include <stdlib.h>
if (argc != 4) {
printf("Error: Invalid number of arguments.\n");
printf("Usage: %s <integer1> <integer2> <integer3>\n", argv[0]);
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
min = a;
max = a;
avg = (a + b + c) / 3.0;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return 0;
}
4.Define a macro MIN which gives the minimum of two numbers. Use this
macro to find
the minimum of nnumbers.
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of integers: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int min = arr[0];
for (i = 1; i < n; i++) {
min = MIN(min, arr[i]);
}
printf("Minimum: %d\n", min);
return 0;
}