0% found this document useful (0 votes)
13 views40 pages

C Practical File

Uploaded by

Aditi Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views40 pages

C Practical File

Uploaded by

Aditi Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

INDEX

Sr.No Program Page No. Date Teacher


. Sign
9 Write a program using while
loop to print sum of first n
natural numbers.
10 Write a program to check a
number is Armstrong or not
using for loop.
11 Write a program to count the
digits in a number and then
print the reverse of number.
12 Write a program to generate
the Fibonacci series.
13 Write a program to print the
following pattern: a) *
**
** *
** * *
** * * *
** * * * *
b) *
*
*
**
*
ADITIBANSAL 2310990350 G5(A) Page 16
[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
***
*
****
*
*****
*
14 Write a program to print the
following pattern:
12 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
15 Write a program to check that
given number is prime,
perfect, Armstrong or perfect
by using the concept of
function.
16 Write a program to calculate
the area and circumference of
a circle using functions.
17 Write a program to swap two
variables using the concept of
call by value and call by
reference.
18 Write a program to perform
the following operations on
ADITIBANSAL 2310990350 G5(A) Page 17
[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
1D array:
 Insert
 Update
 Delete
 Display
 Search
19 Write a program to calculate
the sum of array elements by
passing it to a function.
20 Write a program to show the
use of passing pointer as
arguments to the functions.
21 Write a program matrix
multiplication using the
concept of 2D array.
22 Write a program to transpose a
given matrix.

Program 9:

ADITIBANSAL 2310990350 G5(A) Page 18


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

Aim: Write a program using while loop to print sum of first n natural
numbers:
Platform Used: DEV C

Implementation

#inclu
de<st
dio.h>
int
main(
)
{
int i=1,n;
int sum=0;
printf("Enter the value of
n"); scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("Sum of first n natural numbers = %d",sum);
return 0;
}

ADITIBANSAL 2310990350 G5(A) Page 19


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

Output:

Program 10:

Aim: Write a program to check a number is Armstrong or not using


for loop:

ADITIBANSAL 2310990350 G5(A) Page 20


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
Platform Used: DEV C

Implementation

#include<stdio.h>
int main(){
int n,r,sum=0,temp;
printf("ent
er a
number:");
scanf("%d
",&n);
temp=n;
while(n>0){
r=n%10;
sum=sum+
(r*r*r);
n=n/10; }
if(temp==sum){
printf("Armstrong number");
}
else{
printf("Not an armstrong number");
}
}

ADITIBANSAL 2310990350 G5(A) Page 21


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
Output:

Program 11:

ADITIBANSAL 2310990350 G5(A) Page 22


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
Aim: Write a program to count the digits in a number and then print
the reverse of number:
Platform Used: DEV C

Implementation

#include <stdio.h>
void countDigitsAndReverse(int number, int
*digitCount, int *reverseNumber)
{
int temp
=number;
*digitCoun
t = 0;
*reverseNu
mber = 0;
while
(temp != 0)
{ *dig
itCount +=
1;
temp /= 10;
}
temp =
number;
while
(temp !
= 0) {

ADITIBANSAL 2310990350 G5(A) Page 23


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
*reverseNumber =
*reverseNumber * 10 + (temp %
10);
temp /= 10;
}
}
int main() {
int number, digitCount, reverseNumber;
printf("Enter a
number: ");
scanf("%d",
&number);
countDigitsAndReverse(number, &digitCount,
&reverseNumber);
printf("Number of digits:
%d\n", digitCount);
printf("Reverse of the
number: %d\n",
reverseNumber);
return 0; }

ADITIBANSAL 2310990350 G5(A) Page 24


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
Output:

Program 12:
ADITIBANSAL 2310990350 G5(A) Page 25
[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

Aim: Write a program to generate the Fibonacci series:


Platform Used: DEV C

Implementation

#include <stdio.h>
void generateFibonacci(int n) {
int first = 0, second = 1, next;
for (int i = 0; i< n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
printf("\n");
}
int main() {
int terms;
printf("Enter the value of n: ");
scanf("%d", &terms);
generateFibonacci(terms);
return 0;
}

ADITIBANSAL 2310990350 G5(A) Page 26


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

Output:

ADITIBANSAL 2310990350 G5(A) Page 27


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

Program 13:

Aim: Write a program to print the following pattern:


a)
*

*
*
***
****
*****
******
Platform Used: DEV C

Implementation

#inc
lude
<std
io.h
> int
mai
n() {
ADITIBANSAL 2310990350 G5(A) Page 28
[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
int
rows
;
printf("Enter the number
of rows:");
scanf("%d", &rows);
for (int i = 1; i<= rows; i++)
{
for (int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}

Output:

ADITIBANSAL 2310990350 G5(A) Page 29


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

b)
*
**
***
****
*****
******

Platformused:DevC

Implementation:

#include<stdio.h>
int main()
{
int limit, i, j, k;
printf("Enterthelimit:");
scanf("%d", &limit);
for(i=1;i<=limit;i++)
{
for(k=1;k<=(limit-i);k++)
{
printf("");
}
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");}
return 0;
}

ADITIBANSAL 2310990350 G5(A) Page 30


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

ADITIBANSAL 2310990350 G5(A) Page 31


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

Output:

ADITIBANSAL 2310990350 G5(A) Page 32


[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003

ADITIBANSAL 2310990350 G5(A) Page 33


Fundamentals of C Programming (23CS003)

Program 14:

Aim: Write a program to print the following pattern:


23456
4 6 8 10 12
6 9 12 15 18
8 12 16 20 24
10 15 20 25 30
12 18 24 30 36
Platform Used: DEV C

Implementation:

#include<stdio.h>

int main(int argc,char

const *argv[]){

int n,m;

printf(“input the no of

rows and columns:”);

scanf(“%d

%d”,&n,&m);

for(i=1;i<=n;i++){
Fundamentals of C Programming (23CS003)

for(int j=1;j<=m;j++){

int mul=i*j;

printf(“%d”,mul);

Printf(“\n”);

Return 0;

Output:
Fundamentals of C Programming (23CS003)

Program 15:

Aim: Write a program to check that given number is prime, perfect,


Armstrong or perfect by using the concept of function
Platform Used: DEV C

Implementation:

#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i<= sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
bool isPerfect(int n) {
int sum = 1;
for (int i = 2; i * i<= n; i++) {
if (n % i == 0) {
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
Fundamentals of C Programming (23CS003)

}
}
if (sum == n &&n != 1)
return true;
return false;
}
bool isArmstrong(int n) {
int sum = 0;
int original = n;
int numDigits = (int)log10(n) + 1;
while (n != 0) {
int digit = n % 10;
sum += pow(digit, numDigits);
n /= 10;
}

return sum == original;


}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
if (isPerfect(num))
printf("%d is a perfect number.\n", num);
else
printf("%d is not a perfect number.\n", num);
if (isArmstrong(num))
Fundamentals of C Programming (23CS003)

printf("%d is an Armstrong number.\n", num);


else
printf("%d is not an Armstrong number.\n", num);
return 0;
}

Output:
Fundamentals of C Programming (23CS003)

Program 16:

Aim: Write a program to calculate the area and circumference of a circle


using functions.
Platform Used: DEV C

Implementation:

#include <stdio.h>
#include <math.h>
float calculate_area(float radius) {
return M_PI * radius * radius;
}
float calculate_circumference(float radius) {
return 2 * M_PI * radius;
}
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
float area = calculate_area(radius);
float circumference = calculate_circumference(radius);
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
return 0;
}
Fundamentals of C Programming (23CS003)

Output:
Fundamentals of C Programming (23CS003)

Program 17:

Aim: Write a program to swap two variables using the concept of call by
value and call by reference.
Platform Used: DEV C
Implementation:

#include <stdio.h>
void swap_by_value(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swap_by_reference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;

printf("Enter value for first variable: ");


scanf("%d", &num1);
printf("Enter value for second variable: ");
scanf("%d", &num2);
printf("\nBefore swapping:\n");
printf("First variable: %d\n", num1);
printf("Second variable: %d\n", num2);
Fundamentals of C Programming (23CS003)

swap_by_value(num1, num2);
printf("\nAfter swapping using call by value:\n");
printf("First variable: %d\n", num1);
printf("Second variable: %d\n", num2);
swap_by_reference(&num1, &num2);
printf("\nAfter swapping using call by reference:\n");
printf("First variable: %d\n", num1);
printf("Second variable: %d\n", num2);
return 0;
}

Output:
Fundamentals of C Programming (23CS003)

Program 18:

Aim: Write a program to perform the following operations on 1D-Array:


• Insert
• Update
• Delete
• Display
• Search
Platform Used: DEV C

Implementation:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100];
int element,i,loc,size,n=0,j=0;
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter %d array elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("List before Insertion: ");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to insert\n");
Fundamentals of C Programming (23CS003)

scanf("%d",&element);
printf("Enter a position to insert an element %d\n",element);
scanf("%d",&loc);
loc--;
for(i=size-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=element;
printf("\nList after Insertion: ");
for(i=0;i<size+1;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to delete\n");
scanf("%d",&n);
i=0;
for(i=0;i<size;i++)
{
if(a[i]==n)
{
for(j=i;j<(size-1);j++)
{
a[j]=a[j+1];
}
break;
}
}
printf("List after deletion\n");
for(i=0;i<(size-1);i++)
{
Fundamentals of C Programming (23CS003)

printf("%d ",a[i]);
}
return 0;
}

Output:
Fundamentals of C Programming (23CS003)

Program 19:

Aim: Write a program to calculate the sum of array elements by passing


it to a function.
Platform Used: DEV C

Implementation:

#include <stdio.h>
#define MAX_SIZE 100
int sumArray(int arr[], int size) {
int sum = 0;
int i;
for ( i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int arr[MAX_SIZE];
int size;
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
int i;
for ( i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int result = sumArray(arr, size);
Fundamentals of C Programming (23CS003)

printf("Sum of array elements: %d\n", result);


return 0;
}

Output:
Fundamentals of C Programming (23CS003)

Program 20 :

Aim: Write a program to show the use of passing pointer as arguments to


the functions.
Platform Used: DEV C

Implementation:

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void increment(int *num, int value) {
*num += value;
}
int main() {
int x = 5, y = 10;
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
int num = 7;
printf("Before incrementing: num = %d\n", num);
increment(&num, 3);
printf("After incrementing by 3: num = %d\n", num);
return 0;
}
Fundamentals of C Programming (23CS003)

Output:

Program 21:

Aim: Write a program matrix multiplication using the concept of 2D array.


Platform Used: DEV C
Fundamentals of C Programming (23CS003)

Implementation:

#include <stdio.h>
#define MAX_ROWS 100
#define MAX_COLS 100
void matrix_multiply(int mat1[][MAX_COLS], int mat2[][MAX_COLS], int
result[][MAX_COLS], int rows1, int cols1, int rows2, int cols2) {
if (cols1 != rows2) {
printf("Matrix multiplication not possible.\n");
return;
}
for (int i = 0; i< rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
void display_matrix(int mat[][MAX_COLS], int rows, int cols) {
for (int i = 0; i< rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter the number of rows and columns for first matrix: ");
Fundamentals of C Programming (23CS003)

scanf("%d %d", &rows1, &cols1);


int mat1[MAX_ROWS][MAX_COLS];
printf("Enter elements of first matrix:\n");
for (int i = 0; i< rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the number of rows and columns for second matrix: ");
scanf("%d %d", &rows2, &cols2);
int mat2[MAX_ROWS][MAX_COLS];
printf("Enter elements of second matrix:\n");
for (int i = 0; i< rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &mat2[i][j]);
}
}
int result[MAX_ROWS][MAX_COLS];
matrix_multiply(mat1, mat2, result, rows1, cols1, rows2, cols2);
printf("Result of matrix multiplication:\n");
display_matrix(result, rows1, cols2);
return 0;
}

Output:
Fundamentals of C Programming (23CS003)

Program 22:

Aim: Write a program to transpose a given matrix.


Platform Used: DEV C
Fundamentals of C Programming (23CS003)

Implementation:

#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d",&r,&c);
printf("\nEnter matrix elements:\n");
for (int i=0;i<r;++i)
{
for (int j=0;j<c;++j)
{
printf("Enter element a %d%d: ",i+1,j+1);
scanf("%d", &a[i][j]);
}
}
printf("\nEntered matrix: \n");
for (int i=0;i<r;++i)
{
for (int j=0;j<c;++j)
{
printf("%d ",a[i][j]);
if (j==c-1)
printf("\n");
}
}
for (int i=0;i<r;++i)
{
for (int j=0;j<c;++j)
{
Fundamentals of C Programming (23CS003)

transpose[j][i]=a[i][j];
}
}
printf("\nTranspose of the matrix:\n");
for (int i=0;i<c;++i)
{
for (int j=0;j<r;++j)
{
printf("%d ",transpose[i][j]);
if (j==r-1)
printf("\n");
}
}
return 0;
}

Output:
Fundamentals of C Programming (23CS003)

You might also like