C Practical File
C Practical File
INDEX
Program 9:
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;
}
Output:
Program 10:
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");
}
}
Program 11:
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) {
Program 12:
ADITIBANSAL 2310990350 G5(A) Page 25
[Type text] FUNDAMENTALSOFCPROGRAMMING-23CS003
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;
}
Output:
Program 13:
*
*
***
****
*****
******
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:
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;
}
Output:
Program 14:
Implementation:
#include<stdio.h>
const *argv[]){
int n,m;
printf(“input the no of
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:
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;
}
Output:
Fundamentals of C Programming (23CS003)
Program 16:
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;
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:
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:
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)
Output:
Fundamentals of C Programming (23CS003)
Program 20 :
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:
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)
Output:
Fundamentals of C Programming (23CS003)
Program 22:
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)