SlideShare a Scribd company logo
1 of 7
Download to read offline
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.pdf

Similar to PCA-2 Programming and Solving 2nd Sem.pdf (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

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
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
 
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
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2
 
%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
 
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
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2
 
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 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
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
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
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
 

Recently uploaded (20)

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
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
 
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...
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
%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
 
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
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
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 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
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
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
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
 

PCA-2 Programming and Solving 2nd Sem.pdf

  • 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; }