C – Programming Lab
1.Find the area of a circle and area of a triangle given three sides.
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, s, area;
printf("Enter sides of a triangle\n");
scanf("%lf%lf%lf", &a, &b, &c);
s = (a+b+c)/2; // Semiperimeter
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of the triangle = %.2lf\n", area);
return 0;
}
2.Largest of three numbers.
#include <stdio.h>
int main(){
int a, b, c;
printf("Enter a,b,c: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) {
printf("a is Greater than b and c");
}
else if (b > a && b > c) {
[Type text] Page 1
C – Programming Lab
printf("b is Greater than a and c");
}
else if (c > a && c > b) {
printf("c is Greater than a and b");
}
else {
printf("all are equal or any two values are equal");
}
return 0;
}
3. Reversing the digits of an integer.
#include <stdio.h>
int main()
int n, num1,num2,num3, reverse ;
printf("Enter the number to reverse:\n");
scanf("%d", &n);
num1 = n / 100;
num2 = (n % 100) / 10;
num3 = n%10 ;
// num1 , num2 , num3 are digits only , to make a number use the below step
[Type text] Page 2
C – Programming Lab
reverse = 100*num3 + 10*num2 + num1;
printf(" The reverse is %d", reverse);
system("pause");
return 0;
}
4.GCD of two integers.
#include <math.h>
#include <stdio.h>
// Function to return gcd of a and b
int gcd(int a, int b)
{
// Find Minimum of a and b
int result = ((a < b) ? a : b);
while (result > 0) {
// Check if both a and b are divisible by result
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
// return gcd of a nd b
return result;
}
// Driver program to test above function
int main()
{
int a = 98, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
[Type text] Page 3
C – Programming Lab
5. Generating prime numbers.
#include<stdio.h>
void main(){
int i, num, n, count;
printf("Enter the range: ");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
}
6.Computing nth Fibonacci numbers.
#include
int main() {
[Type text] Page 4
C – Programming Lab
int a = 0, b = 1;
int nextTerm;
int n = 6;
// if asked to print 0th/1st term
// f(0) = 0 and f(1) = 1
if (n == 0 || n == 1){
printf("fib(%d) : %d",n, n);
return 0;
}
for (int i = 2; i <= n; ++i) {
nextTerm = a + b;
a = b;
b = nextTerm;
}
printf("fib(%d) : %d",n, nextTerm)
return 0;
}
7. Finding Even and Odd numbers.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
8. Exchanging the values of two variables.
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// value of first is assigned to temp
[Type text] Page 5
C – Programming Lab
temp = first;
// value of second is assigned to first
first = second;
// value of temp (initial value of first) is assigned to second
second = temp;
// %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
9. Counting:Print number from 100 to 200 which are divisible by 7 and display their sum and count
using for loop.
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
// Swapping values of a and b
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n", a, b);
return 0;
}
10.Summation of set of Numbers.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
11. Factorial Computation.
[Type text] Page 6
C – Programming Lab
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
12.Generation of Fibonacci sequence.
// C Program to print the fibonacci series using loops
#include <stdio.h>
void printFib(int n) {
// If the number of terms is smaller than 1
if (n < 1) {
printf("Invalid Number of terms\n");
return;
}
// First two terms of the series
int prev1 = 1;
int prev2 = 0;
// for loop that prints n terms of fibonacci series
for (int i = 1; i <= n; i++) {
// Print current term and update previous terms
if (i > 2) {
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
printf("%d ", curr);
}
else if (i == 1)
printf("%d ", prev2);
else (i == 2)
[Type text] Page 7
C – Programming Lab
printf("%d ", prev1);
}
}
int main() {
int n = 9;
// Printing first n fibonacci terms
printFib(n);
return 0;
}
13. Array Order Reversal.
// C Program to Reverse an Array by Printing it from The Last Element to the First Element
#include <stdio.h>
#define N 1000
int main() {
int arr[N];
int n;
// Inputting the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
// Inputting the array
printf("Enter an array: ");
for (int i = 0; i< n; i++){
scanf("%d", &arr[i]);
}
// Printing the reverse of the array
printf("Reversed array: ");
for (int i = n-1; i>=0; i--){
printf("%d ", arr[i]);
}
return 0;
}
14.Finding the Maximum Number in a Set.
#include<stdio.h>
int main()
{
int i,arr[100],maximum,size,index;
printf("\n Enter size of array:- ");
scanf("%d",&size);
printf("\n Enter array elements:- ");
[Type text] Page 8
C – Programming Lab
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
maximum=arr[0];
for(i=0;i<size;i++)
{
if(maximum<arr[i])
{
maximum=arr[i];
index=i;
}
}
printf("\n The maximum number %d is present at %d th location in the given array. \n \
n",maximum,index+1);
return 0;
15. Removal of Duplicates from an Ordered Array.
#include <stdio.h>
int removeDuplicates(int arr[], int N) {
// If array is empty, return 0
if (N == 0) {
return 0;
}
// Initialize a pointer to store unique elements
int i = 0;
// Loop through the array using the second pointer
for (int j = 1; j < N; j++)
{
// If current element is not equal to the previous unique element
if (arr[i] != arr[j])
{
// Increment the pointer and update the unique element
i++;
arr[i] = arr[j];
}
}
// Return the number of unique elements
return i + 1;
}
int main()
{
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5};
int N = sizeof(arr) / sizeof(arr[0]);
[Type text] Page 9
C – Programming Lab
// Get the new length after removing duplicates
int newLength = removeDuplicates(arr, N);
printf("Array after removing duplicates: ");
for (int i = 0; i < newLength; i++) {
printf("%d ", arr[i]);
}
return 0;
}
.16.Partitioning an Array.
#include <stdio.h>
int main(){
int a[] = {2, 89, 34, 16, 17, 10, 11, 78, 30, 19};
int n = sizeof(a)/sizeof(a[0]);
int low = 15, high = 20;
for(int i= 0; i<n; i++){
for(int j=i+1; j<n; j++){ if(a[i]> a[j]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=0; i<n; i++)
printf("%d ",a[i]);
Method 1 : Naive Approach
Method 2 : Efficient Approach
Method 1 :
Declare an array of n size and initialize the elements of the array .
Run
#include <stdio.h>
[Type text] Page 10
C – Programming Lab
int main(){
int a[] = {2, 89, 34, 16, 17, 10, 11, 78, 30, 19};
int n = sizeof(a)/sizeof(a[0]);
int low = 15, high = 20;
for(int i= 0; i<n; i++){
for(int j=i+1; j<n; j++){ if(a[i]> a[j]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=0; i<n; i++)
printf("%d ",a[i]);
}
Output :
2 10 11 16 17 19 30 34 89 78
17. Finding the Smallest Element.
#include <stdio.h>
int getSmallest(int arr[], int len)
{
// assign first array element as smallest
int min = arr[0];
// linearly search for the smallest element
for(int i=1; i < len; i++)
{
// if the current array element is smaller
if (arr[i] < min)
min = arr[i];
}
return min;
}
int main()
{
int arr[] = {5, 8, 7, 2, 12, 4};
[Type text] Page 11
C – Programming Lab
// get the length of the array
int len = sizeof(arr)/sizeof(arr[0]);
printf("The smallest : %d", getSmallest(arr, len));
}
18. Read N (minimum 5) students marks and find number of students passed and fail
depending on the marks.
#include <stdio.h>
int main()
{
int num_students, passed = 0, failed = 0; // Prompt for the number of students
printf("Enter the number of students (minimum 5): ");
scanf("%d", &num_students); // Ensure there are at least 5 students
if (num_students < 5)
{
printf("Please enter at least 5 students.\n");
return 1; // Exit the program with an error code }
float marks; // Loop through each student to get their marks
for (int i = 0; i < num_students; i++)
{
printf("Enter marks for student %d: ", i + 1);
scanf("%f", &marks); // Check if the student passed or failed
if (marks >= 50) { passed++; } else { failed++; } } // Display the results printf("Number of students
passed: %d\n", passed);
printf("Number of students failed: %d\n", failed);
return 0; // Successful execution }
19.Count the number of vowels, consonants and special characters in a given
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int vowels = 0, consonants = 0, specials = 0; // Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Process each character in the string
for (int i = 0; str[i] != '\0'; i++)
{
char ch = tolower(str[i]); // Convert to lowercase
if (isalpha(ch))
{ // Check if the character is a letter
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
vowels++;
}
Else
[Type text] Page 12
C – Programming Lab
{
consonants++;
}
} else
if (!isspace(ch)) { // Count special characters (excluding spaces) specials++;
}
} // Display the results
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
printf("Special characters: %d\n", specials);
return 0;
}
20. To find the addition and subtraction of two matrices using function.
#include <stdio.h>
#define MAX 10 // Maximum size for the matrices // Function prototypes
void inputMatrix(int matrix[MAX][MAX], int rows, int cols);
void printMatrix(int matrix[MAX][MAX], int rows, int cols);
void addMatrices(int matrix1[MAX][MAX], int matrix2[MAX][MAX], int result[MAX][MAX], int
rows, int cols);
void subtractMatrices(int matrix1[MAX][MAX], int matrix2[MAX][MAX], int result[MAX][MAX],
int rows, int cols);
int main()
{
int matrix1[MAX][MAX], matrix2[MAX][MAX], sum[MAX][MAX], difference[MAX][MAX];
int rows, cols; // Input size of the matrices
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols); // Input matrices
printf("Input elements for Matrix 1:\n");
inputMatrix(matrix1, rows, cols);
printf("Input elements for Matrix 2:\n");
inputMatrix(matrix2, rows, cols); // Calculate sum and difference addMatrices(matrix1, matrix2, sum,
rows, cols);
subtractMatrices(matrix1, matrix2, difference, rows, cols); // Print results printf("\nSum of the matrices:\
n");
printMatrix(sum, rows, cols); printf("\nDifference of the matrices:\n"); printMatrix(difference, rows,
cols);
return 0; } // Function to input matrix elements
void inputMatrix(int matrix[MAX][MAX], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
} // Function to print the matrix
void printMatrix(int matrix[MAX][MAX], int rows, int cols)
[Type text] Page 13
C – Programming Lab
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
} // Function to add two matrices
void addMatrices(int matrix1[MAX][MAX], int matrix2[MAX][MAX], int result[MAX][MAX], int
rows, int cols) {
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
} // Function to subtract two matrices
void subtractMatrices(int matrix1[MAX][MAX], int matrix2[MAX][MAX], int result[MAX][MAX], int
rows, int cols)
{
for (int i = 0; i < rows; i++)
{ for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
[Type text] Page 14