Question 13. Develop a program to calculate the sum of array elements in C.
#include<stdio.h>
int main () {
int i , n , sum = 0 ;
printf(“Enter the number of elements in array: “);
scanf(“%d”,&n);
int arr[n];
printf(“Enter the Elements of array: “ );
for( int i = 0 ; i < n ; i++) {
scanf(“%d”,&arr[i]);
}
for(int i = 0 ; i < n ; i++) {
sum += arr[i];
}
printf(“Sum of the array: %d\n “, sum);
return 0 ;
}
Output :
Question 14. Develop a program to find the largest array element in C.
#include<stdio.h>
int main () {
int arr[ ] = {2,5,1,8,45,7};
int n= sizeof(arr)/4;
int mx= arr[0];
for (int i = 1; i < n; i++) {
if (mx<arr[i])
mx=arr[i];
}
printf(“max of elements is : %d” , mx);
return 0 ;
}
Output :
Question 22. Write a program to read and display student information using structure.
#include <stdio.h>
struct Student {
char name [50];
int roll;
float marks;
};
int main() {
struct Student s;
printf("Enter student details: \n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("\nDisplaying Student Information:\n");
printf("Name: %s", s.name);
printf("Roll Number: %d\n", s.roll);
printf("Marks: %.2f\n", s.marks);
return 0;
}
Output :
Question 19. Write a C program to calculate the sum of two numbers using a pointer.
#include <stdio.h>
int main() {
int num1, num2, sum;
int *ptr1, *ptr2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
ptr1 = &num1;
ptr2 = &num2;
sum = *ptr1 + *ptr2;
printf("Sum of %d and %d is: %d\n", *ptr1, *ptr2, sum);
return 0;
}
Output :
Question 18. WAP to find the factorial of a number using recursion.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) // Base case: 011 and 11 = 1
return 1;
else
return n factorial(n-1);
int main() {
int num;
printf("Enter a number to find its factorial: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial of a negative number is not defined.\n");
} else {
printf("Factorial of %d is: %d\n", num, factorial(num));
}
return 0;
}
Output :
Question 17. WAP to calculate the power of a number using a function.
#include <stdio.h>
int power(int base, int exponent) {
int result = 1;
for (int i=0; i< exponent; i++) {
result * = base;
}
Return result;
}
int main () {
int base , exponent;
printf("Enter the base: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
printf("%d raised to the power of %d is: %d\n", base, exponent, power (base , exponent));
return 0;
}
Output :
Question 20. Create a function to swap the value of to two variable demonstrating a call-by-
value and a call-by-reference concept.
#include <stdio.h>
void swapByValue(int a, int b) {
int temp a;
a = b;
b = temp;
printf("Inside swapByValue: a=%d, b %d (Values swapped locally)\n", a , b);
}
void swapByReference(int *a, int *b) {
int temp *a;
*a = *b;
*b = temp;
printf("Inside swapByReference: a %d, b %d (Values swapped globally)\n”, *a, *b);
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Before swap: num1= %d, num2= %d\n", num1, num2);
swapByValue(num1, num2);
printf("After swapByValue : num1 = %d, num2 = %d (No change) \n”, num1, num2);
swapByReference(&num1 , num2);
printf(“After swapByReference: num1 = %d , num2 = %d (value swapped)\n”, num1, num2);
return 0;
Output :
Question 16. Write the C program to demonstrate the use of the following string functions:
Using : gets ( ) and puts ( )
#include <stdio.h>
int main() {
char str[100];
// Using gets() to read a string
printf("Enter a string: ");
gets(str); // Note: gets() is unsafe, use fgets() in production code.
// Using puts() to display the string
puts("You entered:");
puts(str);
return 0;
}
Output:
Using: strlen( )
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // safer than gets()
str[strcspn(str, "\n")] = 0; // Remove newline character
printf("Length of the string: %zu\n", strlen(str));
return 0;
}
Output:
Using: strcmp ( )
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Remove newline
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Remove newline
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result < 0) {
printf("First string is less than second string.\n");
} else {
printf("First string is greater than second string.\n");
}
return 0;
}
Output:
Using: strcpy ( )
#include <stdio.h>
#include <string.h>
int main() {
char source[100], destination[100];
printf("Enter a string: ");
fgets(source, sizeof(source), stdin);
source[strcspn(source, "\n")] = 0; // Remove newline
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
Output:
Using: strcat( )
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Remove newline
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Remove newline
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:
Using: strchr ( )
#include <stdio.h>
#include <string.h>
int main() {
char str[100], ch;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline
printf("Enter a character to search for: ");
scanf(" %c", &ch);
char *result = strchr(str, ch);
if (result) {
printf("Character '%c' found at position: %ld\n", ch, result - str);
} else {
printf("Character '%c' not found.\n", ch);
}
return 0;
}
Output:
Using: strstr( )
#include <stdio.h>
#include <string.h>
int main() {
char str[100], sub[50];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline
printf("Enter substring to search for: ");
fgets(sub, sizeof(sub), stdin);
sub[strcspn(sub, "\n")] = 0; // Remove newline
char *result = strstr(str, sub);
if (result) {
printf("Substring found at position: %ld\n", result - str);
} else {
printf("Substring not found.\n");
}
return 0;
}
Output:
Using: strrev ( )
#include <stdio.h>
#include <string.h>
void strrev(char *str) {
int n = strlen(str);
for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline
strrev(str);
printf("Reversed string: %s\n", str);
return 0;
}
Output:
Using: strlwr ( )
#include <stdio.h>
#include <ctype.h>
void strlwr(char *str) {
while (*str) {
*str = tolower(*str);
str++;
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline
strlwr(str);
printf("Lowercase string: %s\n", str);
return 0;
}
Output:
Using: strupr ( )
#include <stdio.h>
#include <ctype.h>
void strupr(char *str) {
while (*str) {
*str = toupper(*str);
str++;
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline
strupr(str);
printf("Uppercase string: %s\n", str);
return 0;
}
Output: