0% found this document useful (0 votes)
4 views25 pages

XI Solution Array String Answer

The document contains multiple C programming examples demonstrating various functionalities such as array manipulation, matrix operations, string handling, and basic input/output. Key operations include sorting arrays, calculating sums, finding maximum and minimum values, and performing matrix addition, subtraction, and multiplication. Additionally, it includes string operations like copying, concatenation, comparison, and palindrome checking.

Uploaded by

Nathan Prajapati
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)
4 views25 pages

XI Solution Array String Answer

The document contains multiple C programming examples demonstrating various functionalities such as array manipulation, matrix operations, string handling, and basic input/output. Key operations include sorting arrays, calculating sums, finding maximum and minimum values, and performing matrix addition, subtraction, and multiplication. Additionally, it includes string operations like copying, concatenation, comparison, and palindrome checking.

Uploaded by

Nathan Prajapati
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

#include <stdio.

h>

int main() {
int numbers[50]; // Declare an array to store numbers from 1 to 50

// Initialize the array with values from 1 to 50


for (int i = 0; i < 50; i++) {
numbers[i] = i + 1;
}

// Display the numbers


printf("Numbers from 1 to 50:\n");
for (int i = 0; i < 50; i++) {
printf("%d ", numbers[i]);
}

printf("\n"); // New line for better readability


return 0;
}

CamScanner
#include <stdio.h>

void sortArray(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
} }
}
int main() {
int n;

// Asking user for number of elements


printf("Enter the number of elements: ");
scanf("%d", &n);

int numbers[n];

// Input elements
printf("Enter %d numbers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Sorting the array


sortArray(numbers, n);

// Display sorted numbers


printf("Sorted numbers in ascending order:\n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}

printf("\n"); // New line for better readability


return 0;
}

CamScanner
#include <stdio.h>

int main() {
int salaries[10];
int count = 0;

// Reading salaries of 10 employees


printf("Enter the salaries of 10 employees:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &salaries[i]);
}

// Counting employees with salary between 20000 and 30000


for (int i = 0; i < 10; i++) {
if (salaries[i] >= 20000 && salaries[i] <= 30000) {
count++;
}
}

// Displaying the count


printf("Number of employees with salary between 20000 and 30000: %d\n", count);

return 0;
}

CamScanner
#include <stdio.h>

int main() {
int numbers[10], sum = 0;

// Reading 10 numbers
printf("Enter 10 numbers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i];
}

// Displaying the sum


printf("The sum of the entered numbers is: %d\n", sum);

return 0;
}

CamScanner
#include <stdio.h>

int main() {
int numbers[20], max, min;

// Reading 20 numbers
printf("Enter 20 numbers:\n");
for (int i = 0; i < 20; i++) {
scanf("%d", &numbers[i]);
}

// Initializing max and min with the first element


max = min = numbers[0];

// Finding the greatest and smallest numbers


for (int i = 1; i < 20; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}

// Displaying the results


printf("Greatest number: %d\n", max);
printf("Smallest number: %d\n", min);

return 0;
}

CamScanner
#include <stdio.h>

int main() {
int rows, cols;

// Asking user for order of matrix


printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);

int matrix[rows][cols];

// Input elements of the matrix


printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Displaying the entered matrix


printf("The entered matrix is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}

CamScanner
#include <stdio.h>

int main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];

// Input first 3x3 matrix


printf("Enter the elements of the first 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input second 3x3 matrix


printf("Enter the elements of the second 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Adding the two matrices


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Displaying the sum matrix


printf("The sum of the two matrices is:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}

CamScanner
#include <stdio.h>

int main() {
int matrix1[2][2], matrix2[2][2], diff[2][2];

// Input first 2x2 matrix


printf("Enter the elements of the first 2x2 matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input second 2x2 matrix


printf("Enter the elements of the second 2x2 matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Subtracting the two matrices


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
diff[i][j] = matrix1[i][j] - matrix2[i][j];
}
}

// Displaying the difference matrix


printf("The difference of the two matrices is:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", diff[i][j]);
}
printf("\n");
}

return 0;
}

CamScanner
#include <stdio.h>

int main() {
int matrix[3][3], transpose[3][3];

// Input 3x3 matrix


printf("Enter the elements of the 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Finding the transpose


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

// Displaying the transpose matrix


printf("The transpose of the given matrix is:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}

return 0;
}

CamScanner
#include <stdio.h>
int main() {
int matrix1[3][3], matrix2[3][3], product[3][3];

// Input first 3x3 matrix


printf("Enter the elements of the first 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix1[i][j]);
} }

// Input second 3x3 matrix


printf("Enter the elements of the second 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix2[i][j]);
} }
// Initializing product matrix to zero
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
product[i][j] = 0;
}
}
// Multiplying the matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
} }

// Displaying the product matrix


printf("The product of the two matrices is:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}

return 0;
}

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

int main() {
char name[100];

// Input full name


printf("Enter your full name: ");
fgets(name, sizeof(name), stdin); // Reads the full name including spaces

// Find the length of the string


int length = strlen(name) - 1; // Subtract 1 to exclude the newline character added by fgets

// Display the length of the string


printf("The length of your full name is: %d\n", length);

return 0;
}

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

int main() {
char source[100], destination[100];

// Input string
printf("Enter a string: ");
fgets(source, sizeof(source), stdin); // Reads the string including spaces

// Copy the string to another variable


strcpy(destination, source);

// Display the copied string


printf("The copied string is: %s", destination);

return 0;
}

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

int main() {
char str1[] = "Kath";
char str2[] = "mandu";
char str3[100]; // To hold the concatenated result

// Copy str1 to str3


strcpy(str3, str1);

// Concatenate str2 to str3


strcat(str3, str2);

// Display the result


printf("The concatenated string is: %s\n", str3);

return 0;
}

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

int main() {
char str1[100], str2[100];

// Input two strings


printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);

// Remove the newline character added by fgets


str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';

// Compare the strings


if (strcmp(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}

return 0;
}

CamScanner
#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];

// Input a string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Reads the string including spaces

// Convert the string to uppercase


printf("The string in uppercase is: ");
for (int i = 0; str[i] != '\0'; i++) {
putchar(toupper(str[i])); // Converts each character to uppercase
}

printf("\n");

// Convert the string to lowercase


printf("The string in lowercase is: ");
for (int i = 0; str[i] != '\0'; i++) {
putchar(tolower(str[i])); // Converts each character to lowercase
}

printf("\n");

return 0;
}

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

int main() {
char str[100];

// Input a line of string


printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Reads the string including spaces

// Remove the newline character added by fgets


str[strcspn(str, "\n")] = '\0';

// Display the string in reverse order


printf("The reversed string is: ");
for (int i = strlen(str) - 1; i >= 0; i--) {
putchar(str[i]);
}
printf("\n");

return 0;
}

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

int main() {
char str[100];

// Input a string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Remove the newline character added by fgets


str[strcspn(str, "\n")] = '\0';

// Convert the string to lowercase


for (int i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
}
// Check for palindrome using a for loop
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
printf("The string is not a palindrome.\n");
return 0;
}
}
printf("The string is a palindrome.\n");
return 0;
}

CamScanner
#include <stdio.h>

int main() {
char str[100];

// Input a string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Reads the string including spaces

// Display ASCII code of each character


printf("ASCII codes of each character in the string:\n");
for (int i = 0; str[i] != '\0'; i++) {
printf("Character: %c, ASCII Code: %d\n", str[i], str[i]);
}

return 0;
}

CamScanner
#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];
int wordCount = 0;
int inWord = 0; // Flag to track if we are in a word or not

// Input a string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Reads the string including spaces

// Count the words


for (int i = 0; str[i] != '\0'; i++) {
// If the character is a space, set inWord to 0 (end of a word)
if (isspace(str[i])) {
inWord = 0;
} else if (!inWord) {
// If we are not in a word and encounter a non-space character, it
wordCount++;
inWord = 1; // Now we are in a word
}
}

// Display the result


printf("Total number of words: %d\n", wordCount);

return 0;
}

CamScanner
#include <stdio.h>
#include <string.h>
#define MAX_EMPLOYEES 10
#define MAX_NAME_LENGTH 100
int main() {
char employees[MAX_EMPLOYEES][MAX_NAME_LENGTH];

// Input names of 10 employees


printf("Enter the names of 10 employees:\n");
for (int i = 0; i < MAX_EMPLOYEES; i++) {
printf("Employee %d: ", i + 1);
fgets(employees[i], sizeof(employees[i]), stdin);
employees[i][strcspn(employees[i], "\n")] = '\0'; // Remove newline character
}
// Sort the names in alphabetical order
for (int i = 0; i < MAX_EMPLOYEES - 1; i++) {
for (int j = i + 1; j < MAX_EMPLOYEES; j++) {
if (strcmp(employees[i], employees[j]) > 0) {
char temp[MAX_NAME_LENGTH];
strcpy(temp, employees[i]);
strcpy(employees[i], employees[j]);
strcpy(employees[j], temp);
}
} }
// Display the sorted names
printf("\nEmployee names in alphabetical order:\n");
for (int i = 0; i < MAX_EMPLOYEES; i++) {
printf("%s\n", employees[i]);
}

return 0;
}

CamScanner
#include <stdio.h>
#include <ctype.h>

int main() {
char str[100]; // Declare a string to store the input
int totalChars = 0, upperCase = 0, lowerCase = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin); // Input a string (including spaces)

// Loop through each character in the string


for(int i = 0; str[i] != '\0'; i++) {
totalChars++; // Count the total characters

if (isupper(str[i])) {
upperCase++; // Count uppercase characters
} else if (islower(str[i])) {
lowerCase++; // Count lowercase characters
}
}

// Output the results


printf("Total number of characters: %d\n", totalChars);
printf("Total number of uppercase characters: %d\n", upperCase);
printf("Total number of lowercase characters: %d\n", lowerCase);

return 0;
}

CamScanner
#include <stdio.h>
#include <ctype.h>

int main() {
char str[100]; // Declare a string to store the input
int digits = 0, whiteSpaces = 0, otherChars = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin); // Input a string (including spaces)

// Loop through each character in the string


for (int i = 0; str[i] != '\0'; i++) {
if (isdigit(str[i])) {
digits++; // Count the digits
} else if (isspace(str[i])) {
whiteSpaces++; // Count white space characters
} else {
otherChars++; // Count other characters
}
}

// Output the results


printf("Total number of digits: %d\n", digits);
printf("Total number of white space characters: %d\n", whiteSpaces);
printf("Total number of other characters: %d\n", otherChars);

return 0;
}

CamScanner
#include <stdio.h>

int main() {
char str[100], ch;
int count = 0;

// Asking for the sentence


printf("Enter a sentence: ");
fgets(str, sizeof(str), stdin); // Reading a line of input including spaces

// Asking for the character


printf("Enter a character to search for: ");
scanf("%c", &ch); // Reading the character to search for

// Loop through the string to count occurrences of the character


for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
count++; // Increment count if character matches
}
}

// Output the result


printf("The character '%c' occurred %d times in the sentence.\n", ch, count);

return 0;
}

CamScanner
#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];
int vowels = 0, consonants = 0, spaces = 0;

// Asking the user to input a string


printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Reading a line of input

// Loop through each character in the string


for (int i = 0; str[i] != '\0'; i++) {
// Convert to lowercase for case-insensitive comparison
char ch = tolower(str[i]);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {


vowels++; // Count vowels
} else if (isalpha(ch)) { // Check if the character is a letter
consonants++; // Count consonants
} else if (ch == ' ') {
spaces++; // Count spaces
}
}

// Output the results


printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
printf("Number of blank spaces: %d\n", spaces);

return 0;
}

CamScanner
#include <stdio.h>
#include <ctype.h> // for isalpha and tolower
struct Student {
char name[50];
char address[100];
int roll;
char mobile[15];
};
int main() {
struct Student students[10]; // Array to store data of 10 students
// Input data for 10 students
for (int i = 0; i < 10; i++) {
printf("Enter details for student %d\n", i + 1);
printf("Name: ");
fgets(students[i].name, sizeof(students[i].name), stdin);

printf("Address: ");
fgets(students[i].address, sizeof(students[i].address), stdin);

printf("Roll Number: ");


scanf("%d", &students[i].roll);

getchar(); // To consume the newline character left by scanf

printf("Mobile Number: ");


fgets(students[i].mobile, sizeof(students[i].mobile), stdin);
printf("\n");
}
// Display the students whose name's first letter is between 'A' and 'M'
printf("\nStudents whose name starts with a letter between A and M:\n");
for (int i = 0; i < 10; i++) {
char firstLetter = toupper(students[i].name[0]); // Convert first letter to uppercase
if (firstLetter >= 'A' && firstLetter <= 'M') {
// Display the data of the student
printf("\nStudent %d\n", i + 1);
printf("Name: %s", students[i].name);
printf("Address: %s", students[i].address);
printf("Roll Number: %d\n", students[i].roll);
printf("Mobile Number: %s\n", students[i].mobile);
}
}
return 0;
}

CamScanner

You might also like