The document contains 14 code snippets that provide solutions to common programming problems in C including: printing patterns like numbers, stars, triangles using loops; finding sum of digits, checking palindrome, Armstrong number; sorting arrays using bubble sort, selection sort; string operations like concatenation; matrix addition; factorial calculation; checking even/odd number. Each code snippet includes the header files, function definitions and main function to accept input, call functions and print output.
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
PCA-2 Programming and Solving 2nd Sem.docx
1. 1. Write a C program to print 1 to 100 using for loop
Ans. #include <stdio.h>
int main() {
int i;
for (i = 1; i <= 100; i++) {
printf("%d n", i);
}
return 0;
}
2. Write a C program to generate the Fibonacci Series
0 1 1 2 3 5 8… n
Ans. #include <stdio.h>
int main() {
int n, first = 0, second = 1, next, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}
3. Write a C program to find sum of digit given integer
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter an integer: ");
scanf("%d", &n);
2. while (n > 0) {
sum += n % 10;
n /= 10;
}
printf("The sum of digits is %dn", sum);
return 0;
}
4. Write a C program to check given number palindrome or not
Ans. #include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter an integer: ");
scanf("%d", &n);
while (n > 0) {
sum += n % 10;
n /= 10;
}
printf("The sum of digits is %dn", sum);
return 0;
}
5. Write a C program to check given number is a Armstrong or not.
153, 370, 407.
Ans. #include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result = 0, n = 0 ;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
// count the number of digits in the number
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
3. // calculate the sum of cubes of digits
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// check if the given number is an Armstrong number or not
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
6. Write a C program to implement bubble sort.
Ans. #include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {5, 2, 1, 8, 4};
int n = sizeof(arr)/sizeof(arr[0]);
int i;
printf("Original array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("n");
bubble_sort(arr, n);
printf("Sorted array: ");
4. for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("n");
return 0;
}
7. Write a C program to implement Selection sort.
Ans. #include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, min_index, temp;
for (i = 0; i < n-1; i++) {
min_index = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {5, 2, 1, 8, 4}; // an example array
int n = sizeof(arr)/sizeof(arr[0]); // calculate the length of the array
int i;
printf("Original array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("n");
selectionSort(arr, n); // sort the array using selection sort
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("n");
return 0;
}
5. 8. Write a C program to concatenate two string.
Ans. #include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[100] = " world!";
strcat(str1, str2); // concatenate str2 onto str1
printf("Concatenated string: %sn", str1);
return 0;
}
9. Write a C program to adding two matrix.
Ans. #include <stdio.h>
int main() {
int A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int B[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int C[3][3];
int i, j;
// Add the matrices element-wise
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Print the result matrix
printf("Result matrix:n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", C[i][j]);
}
printf("n");
}
return 0;
}
10. Write a C program to find factorial of given positive number.
Ans. #include <stdio.h>
6. int main() {
int n, i;
unsigned long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
// Calculate the factorial of n
for (i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d = %llun", n, factorial);
return 0;
}
11. Write a C program to check whether a number is even or odd.
Ans. #include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// Check if num is even or odd
if (num % 2 == 0) {
printf("%d is even.n", num);
}
else {
printf("%d is odd.n", num);
}
return 0;
}
12. Write a C program to print Floyd’s triangle.
Ans. #include <stdio.h>
int main() {
int rows, i, j, num = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Print Floyd's triangle
7. for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("n");
}
return 0;
}
13. Write a C program to print the asterisk graph.
Ans. #include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("n");
}
return 0;
}
14. Write a C program to print following inverted triangle pattern.
Ans. #include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("n");
}
return 0;
}