SlideShare a Scribd company logo
1 of 7
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);
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;
// 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: ");
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;
}
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>
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
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;
}

More Related Content

Similar to PCA-2 Programming and Solving 2nd Sem.docx

Similar to PCA-2 Programming and Solving 2nd Sem.docx (20)

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
C lab
C labC lab
C lab
 
C Programming
C ProgrammingC Programming
C Programming
 
cpract.docx
cpract.docxcpract.docx
cpract.docx
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C programming
C programmingC programming
C programming
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C Programming
C ProgrammingC Programming
C Programming
 
Programming egs
Programming egs Programming egs
Programming egs
 
Najmul
Najmul  Najmul
Najmul
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
C
CC
C
 
Hargun
HargunHargun
Hargun
 
Progr3
Progr3Progr3
Progr3
 

Recently uploaded

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Eraconfluent
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2
 

Recently uploaded (20)

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
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; }