0% found this document useful (0 votes)
9 views17 pages

Programming Usingc Practical

The document is an index and detailed description of various programming experiments, primarily in C language, including tasks such as calculating the area of a triangle, finding the greatest among three numbers, and generating Fibonacci series. Each experiment includes source code, expected output, and submission details. The document serves as a comprehensive guide for executing and understanding basic programming concepts and operations.

Uploaded by

muskanrawat694
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)
9 views17 pages

Programming Usingc Practical

The document is an index and detailed description of various programming experiments, primarily in C language, including tasks such as calculating the area of a triangle, finding the greatest among three numbers, and generating Fibonacci series. Each experiment includes source code, expected output, and submission details. The document serves as a comprehensive guide for executing and understanding basic programming concepts and operations.

Uploaded by

muskanrawat694
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

Index

[Link]. Name of the Experiment Page No. Date of Submission Sign


Experiment Date
1 Find the area of the triangle 1 19/08/2025
2 Find greatest among 3number 2 26/08/2025
3 Perform the arithmetic 3-4 2/09/2025
expression using m switch
statement
4 Find the factorial of a given 5 09/09/2025
number

5 Generate all prime number upto 6 30/09/2025


nth number
6 Write a program to print 7 07/09/2025
Fibonacci series
7 Write the program to ind odd 8-9 14/10/2025
and prime number
8 Write a program to ind the total 10 28/10/2025
of even integers
9 Write the program to print 11-12 04/10/2025
product of two matrices 28
10 Concatenate two using without 10 08/11/2025
library function
11 Write the program to print the 14 18/11/2025
elements of array using pointer
12 Wirte the program to ind 15 02/12/2025
factorial of a given number using
function
13 Write the program to ind total 16 16/12/2025
mark of n statement
Experiment 1
Find the area of the triangle.
Source Code:
#include<stdio.h>
int main(){
float base, height, area;
printf("Enter base and height of the triangle: ");
scanf("%f%f", &base, &height);
area = 0.5 * (base*height);
printf("Area of the triangle is: %.2f", area);
return 0;
}

Output:
PS C:\Desktop\CPrograms\gcc -o aot aoT.c

PS C:\Desktop\CPrograms\./aot

Enter base and height of the triangle: 3 4

Area of the triangle is: 6.00

PS C:\Desktop\CPrograms\

pg. 1
Experiment 2
Write the program to ind the greatest among 3 numbers
Source Code:
#include <stdio.h>

int main() {
int a, b, c;

printf("Enter three numbers: ");


scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c) {


printf("Greatest number is: %d", a);
}
else if (b >= a && b >= c) {
printf("Greatest number is: %d", b);
}
else {
printf("Greatest number is: %d", c);
}

return 0;
}

Output:

PS C:\Desktop\Cprogram.c> gcc -o greatest greatest.c

PS C:\Desktop\Cprogram.c> ./greatest
Enter three numbers: 6

Greatest number is: 8

pg. 2
Experiment 3
Perform the arithmetic expression using m switch statement
Source code:

#include <stdio.h>

int main()
{
int a, b, choice;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("\nMenu:");
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division");
printf("\n5. Modulus");

printf("\n\nEnter your choice: ");


scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Addition = %d", a + b);
break;

case 2:
printf("Subtraction = %d", a - b);
break;

case 3:
printf("Multiplication = %d", a * b);
break;

case 4:
if (b != 0)
printf("Division = %d", a / b);
else
printf("Division by zero is not allowed");
break;

case 5:
if (b != 0)
printf("Modulus = %d", a % b);
else
pg. 3
printf("Modulus by zero is not allowed");
break;

default:
printf("Invalid choice");
}

return 0;
}

Output:
PS C:\Desktop\Cprogram.c> gcc -o arithmetic arithmetic,c

PS C:\Desktop\Cprogram.c> ./arithmetic
Enter the values of a & b: 20 15

Sum of 20 and 15 is : 35

Sub of 20 and 15 is: 5

Multiple of 20 and 15 is: 300


Div of 20 and 15 is: 1.33

pg. 4
Experiment 4
Find the factorial of a given number.
Source Code:
#include <stdio.h>

int main() {
int n, i;
// Use 'unsigned long long' to handle larger factorial values
unsigned long long factorial = 1;

printf("Enter a non-negative integer: ");


scanf("%d", &n);

// Check if the number is negative


if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (i = 1; i <= n; ++i) {
factorial *= i; // Multiply 'factorial' by the current loop counter 'i'
}
printf("Factorial of %d is %llu\n", n, factorial);
}

return 0;
}

Output;
PS C:\Desktop\Cprogram.c> gcc -o factorial factorial.c

PS C:\Desktop\Cprogram.c> ./factorial

Enter a non-negative integer: 8


Factorial of 8 is 40320

pg. 5
Experiment 5
Generate all prime number upto nth number.
Source Code:
#include <stdio.h>

int main() {
int n, count = 0, num = 2, i, isPrime;

printf("Enter the number of prime terms: ");


scanf("%d", &n);

printf("First %d prime numbers are:\n", n);

while (count < n) {


isPrime = 1;

for (i = 2; i <= num / 2; i++) {


if (num % i == 0) {
isPrime = 0;
break;
}
}

if (isPrime == 1) {
printf("%d ", num);
count++;
}
num++;
}

return 0;
}
Output:
PS C:\Desktop\Cprogram.c> gcc -o allprime allprime.c

PS C:\Desktop\Cprogram.c> ./allprime

Enter the number of prime terms: 10

First 10 prime numbers are:


2 3 5 7 11 13 17 19 23 29

pg. 6
Experiment 6
Write a program to print Fibonacci series.
Source Code:
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next, i;

// Ask the user for the number of terms


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

printf("Fibonacci Series: ");

// Handle cases where the user wants 0 or negative terms


if (n <= 0) {
printf("Please enter a positive number of terms.\n");
} else {
for (i = 0; i < n; i++) {
if (i <= 1) {
// The first two terms are 0 and 1
next = i;
} else {
// Subsequent terms are the sum of the previous two
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n"); // Print a newline character at the end
}

return 0;
}

Output:
PS C:\Desktop\Cprogram.c> gcc -o ibonacci ibonacci.c

PS C:\Desktop\Cprogram.c> ./ibonacci
Enter the number of terms: 4

Fibonacci Series: 0 1 1 2

pg. 7
Experiment 7
Write the program to ind odd and prime number .
Source Code:
#include <stdio.h>
#include <math.h>

// Function to check if a number is prime


int isPrime(int n) {
if (n <= 1) {
return 0; // 0 and 1 are not prime numbers
}
// Check for factors up to the square root of n for efficiency
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0; // Not prime if divisible by any number other than 1 and itself
}
}
return 1; // Is prime
}

int main() {
int number;

// Get input from the user


printf("Enter a positive integer: ");
scanf("%d", &number);

// Check if the number is odd or even


if (number % 2 == 0) {
printf("%d is an **even** number.\n", number);
} else {
printf("%d is an **odd** number.\n", number);
}

// Check if the number is prime


if (isPrime(number)) {
printf("%d is a **prime** number.\n", number);
} else {
printf("%d is not a **prime** number.\n", number);
}

return 0;
}
Output:
PS C: \Desktop\Cprogram.c> gcc -o oddandprime oddandprime.c

PS C: \Desktop\Cprogram.c> ./oddandprime

Enter a positive integer: 4


pg. 8
4 is an **even** number.

4 is not a **prime** number.

pg. 9
Experiment 8
Write a program to ind the total of even integers.
Source Code:
#include <stdio.h>

int main()
{
int i, number, Sum = 0;

printf("Please Enter the Maximum Limit Value: ");


// Read the upper limit from the user
scanf("%d", &number);

printf("Even numbers up to %d are: ", number);

// Loop from 1 up to the entered number


for (i = 1; i <= number; i++)
{
// Check if the current number is even using the modulo operator
if (i % 2 == 0)
{
printf("%d ", i); // Print the even number
Sum = Sum + i; // Add the even number to the total sum
}
}

printf("\n\nThe Sum of All Even Numbers up to %d = %d\n", number, Sum);

return 0;
}

Output:
PS C:\Desktop\Cprogram.c> gcc -o totaleven totaleven.c

PS C:\Desktop\Cprogram.c> ./totaleven
Enter the Maximum Limit Value: 10

Even numbers up to 10 are: 2 4 6 8 10

The Sum of All Even Numbers up to 10 = 30

pg. 10
Experiment 9
Write the program to print product of two matrices 28.
Source Code:
#include <stdio.h>

int main() {
int A[2][2], B[2][2], C[2][2];
int i, j, k;

printf("Enter elements of first 2x2 matrix:\n");


for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &A[i][j]);
}
}

printf("Enter elements of second 2x2 matrix:\n");


for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &B[i][j]);
}
}

// Matrix multiplication
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
C[i][j] = 0;
for (k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}

printf("Product of the two matrices is:\n");


for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}

return 0;
}

Output:
PS C \Desktop\Cprogram.c> gcc -o matrics matrics.c

PS C: \Desktop\Cprogram.c> ./matrics

pg. 11
Enter elements of irst 2x2 matrix:

11

34

Enter elements of second 2x2 matrix:

56

78

Product of the two matrices is:

12 14

43 50

pg. 12
Experiment 10
Concatenate two using without library function.
Source Code:
#include <stdio.h>

int main() {
char str1[100], str2[100];
int i = 0, j = 0;

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);

// Move to the end of first string


while (str1[i] != '\0') {
i++;
}

// Copy second string to the end of first string


while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}

// Add null character at the end


str1[i] = '\0';

printf("Concatenated string: %s", str1);

return 0;
}
Output:
PS C :\Desktop\Cprogram.c> gcc -o library library.c

PS C: \Desktop\Cprogram.c> ./library

Enter irst string: Hello

Enter second string: World

Concatenated string: HelloWorld

pg. 13
Experiment 11
Write the program to print the elements of array using pointer.
Source Code:
#include <stdio.h>

int main() {

int arr[] = {10, 20, 30, 40, 50};


int size = sizeof(arr) / sizeof(arr[0]); //
int *ptr = arr;

printf("Array elements using pointers:\n");

for (int i = 0; i < size; i++) {

printf("%d ", *(ptr + i));


}

printf("\n");
return 0;
}

Output:
PS C \Desktop\Cprogram.c> gcc -o array array.c

PS C: \Desktop\Cprogram.c> ./array

Array elements using pointers:


10 20 30 40 50

pg. 14
Experiment 12
Wirte the program to ind factorial of a given number using function.
Source Code:
#include <stdio.h>

int factorial(int n);

int main() {
int num, result;

printf("Enter a number: ");


scanf("%d", &num);

result = factorial(num);

printf("Factorial of %d is: %d", num, result);

return 0;
}

int factorial(int n) {
int fact = 1, i;

for (i = 1; i <= n; i++) {


fact = fact * i;
}

return fact;
}
Output:
PS C: \Desktop\Cprogram.c> gcc -o factorial factorial.c

PS C: \Desktop\Cprogram.c> ./factorial
Enter a number: 5

Factorial of 5 is: 120

pg. 15
Experiment 13
Write the program to ind total mark of n statement.
Source Code:
#include <stdio.h>

int main() {
int m1, m2, m3, m4, m5;
int total;

printf("Enter marks of 5 subjects:\n");


scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);

total = m1 + m2 + m3 + m4 + m5;

printf("Total Marks = %d", total);

return 0;
}
Output:
PS C: \Desktop\Cprogram.c> gcc -o marks marks.c

PS C: \Desktop\Cprogram.c> ./marks

Enter marks of 5 subjects:


29

45

63

45

96

Total Marks = 278

pg. 16

You might also like