[go: up one dir, main page]

0% found this document useful (0 votes)
15 views27 pages

C Final

The document contains solutions to programming problems and exercises related to fundamentals of programming using C language. It includes problems on loops, arrays, functions, patterns and more. Each problem has the aim, solution code and output. The solutions demonstrate different programming concepts through examples.

Uploaded by

Kartik Joshi
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)
15 views27 pages

C Final

The document contains solutions to programming problems and exercises related to fundamentals of programming using C language. It includes problems on loops, arrays, functions, patterns and more. Each problem has the aim, solution code and output. The solutions demonstrate different programming concepts through examples.

Uploaded by

Kartik Joshi
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
You are on page 1/ 27

Fundamentals of Programming(22CS002)

INDEX

Sr. No. Practical Name Page No. Date Teacher Sign

9. Write a program using while loop to print the 18 19-02-24


sum of first n natural numbers.
10. Write a program to check a number is 19 21-02-24
Armstrong or not using For loop.

11. Write the program to count the digits in a 20 22-02-24


number and then print the reverse of the
number also.

12. Write a program to generate the Fibonacci 21 28-02-24


series.
13. Write a program to print the following
patterns:
a)
*
**
***
****
*****
******
22-23 01-03-24
b) *
* *
* * *
* * * *
* * * * *
* * * * * *

14. Write the program to print the following 24 06-03-24


pattern:
1 2 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

Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

15. Write a program to check that the given 25-26 11-03-24


number is prime, Armstrong or perfect using
the concept of functions.
16. Write a program to calculate the area and 27-28 15-03-24
circumference of a circle using functions.

17. Write a program to swap two variables using 29-30 21-03-24


the concept of call by value and call by
reference.
18. Write a program to perform the following 31-36 28-03-24
operations on 1D-Array:
• Insert
• Update
• Delete
• Display
• Search for linear
• Search for binary
19. Write a program to calculate the sum of array 37 29-03-24
elements by passing it to a function.

20. Write a program to show the use of passing 38 01-04-24


pointer as arguments to the functions.

21. Write a program matrix multiplication using 39-40 03-04-24


the concept of 2D array

22. Write a program to transpose a given matrix. 41-42 04-04-24

Page
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write a program using while loop to print the sum of first n natural
numbers.

Solution :-
#include <stdio.h>
int main() {
int n, i = 1, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
while (i <= n){
sum += i;
i++;
}
printf("The sum of first %d natural numbers is: %d\n", n, sum);
return 0;
}

Output:

Page 18
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write a program to check a number is Armstrong or not using For loop.

Solution :-
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, n = 0, result = 0, power;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0){
originalNum /= 10;
++n;}
originalNum = num;
for (; originalNum != 0; originalNum /= 10){
remainder = originalNum % 10;
power = round(pow(remainder, n));
result += power;}
if (result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}

Output:

Page 19
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write the program to count the digits in a number and then print the
reverse of the number also.

Solution :-
#include <stdio.h>
int main() {
int num, originalNum, digit, reversedNum = 0, count = 0;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
while (num != 0){
num /= 10;
++count;
}
num = originalNum;
printf("Number of digits in %d: %d\n", originalNum, count);
while (num != 0){
digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
printf("Reversed number of %d: %d\n", originalNum, reversedNum);
return 0;
}

Output:

Page 20
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write a program to generate the Fibonacci series.

Solution :-
#include <stdio.h>
int main() {
int n, firstTerm = 0, secondTerm = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 1; i <= n; ++i){
printf("%d, ", firstTerm);
nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
return 0;
}

Output:

Page 21
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write a program to print the following patterns:

(a)
*
**
***
****
*****
******

Solution :-

#include <stdio.h>
int main() {
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");
}
return 0;
}

Output:

Page 22
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

(b)
*
* *
* * *
* * * *
* * * * *
* * * * * *

Solution :-

#include <stdio.h>
int main(){
int rows = 6;
for (int i = 1; i <= rows; i++){
for (int j = 1; j <= rows - i; j++){
printf(" ");
}
for (int k = 1; k <= i; k++)
{
printf(" * ");
} printf("\n");
} return 0;
}

Output:

Page 23
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write the program to print the following pattern:


1 2 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
Solution :-

#include<stdio.h>
int main(){
int i,j;
for(i=1;i<=6;i++){
for(j=1;j<=6;j++){
printf("%d",(i*j));
printf("\t");
}
printf("\n");
}
return 0;
}

Output:

Page 24
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write a program to check that the given number is prime, Armstrong or
perfect using the concept of functions.

Solution :-
#include <stdio.h>
#include <math.h>
int isPrime(int num) {
if (num <= 1)
return 0;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0)
return 0;
} }
int isArmstrong(int num) {
int originalNum = num;
int digits = 0;
int result = 0;
while (originalNum != 0) {
originalNum /= 10;
digits++;
}
originalNum = num;
while (originalNum != 0) {
int remainder = originalNum % 10;
result += pow(remainder, digits);
originalNum /= 10;
}
if (result == num)
return 1;
}
int isPerfect(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}

if (sum == num) {
return 1;}
else{
return 0;}
}
int main() {

Page 25
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

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 (isArmstrong(num)) {
printf("%d is an Armstrong number.\n", num);}
else {
printf("%d is not an Armstrong number.\n", num);}
if (isPerfect(num)) {
printf("%d is a perfect number.\n", num);}
else {
printf("%d is not a perfect number.\n", num);}
return 0;
}

Output:

Page 26
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

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


using functions.
Solution :-
For Area:
#include<stdio.h>

const float pi=3.14;


float for_area(float r)
{
return pi * r * r;
}

int main(){
float radius;
printf("Enter the radius of circle: ");
scanf("%f",&radius);
printf("your area is %0.2f",for_area(radius));

return 0;
}

Output:

Page 27
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

For Circumference:

#include<stdio.h>
const float pi=3.14;
float for_perimeter(float r){
return 2*pi*r;
}
int main(){
float radius;
printf("Enter the radius of circle: ");
scanf("%f",&radius);
printf("Circumference of the circle is %.2f",for_perimeter(radius));
return 0;
}

Output:

Page 28
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write a program to swap two variables using the concept of call by value
and call by reference.

Solution :-
Call by reference:
#include <stdio.h>
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1 = 5, num2 = 10;
printf("Before swapping:\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
swapByReference(&num1, &num2);
printf("After swapping by reference:\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
return 0;
}

Output:

Page 29
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

Call by value:
#include <stdio.h>
void swapByValue(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1 = 5, num2 = 10;
printf("Before swapping:\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
swapByValue(&num1, &num2);
printf("After swapping by value:\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
return 0;
}

Output:

Page 30
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

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

(a) Insert

Solution :-
#include <stdio.h>
int main() {
int array[100], size, element, position;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);}
printf("Enter the element to insert: ");
scanf("%d", &element);
printf("Enter the position to insert: ");
scanf("%d", &position);
if (position < 0 || position > size) {
printf("Invalid position\n");
return 0;}
for (int i = size; i > position; i--) {
array[i] = array[i - 1];}
array[position] = element;
size++;
printf("Array after insertion:");
for (int i = 0; i < size; i++) {
printf(" %d", array[i]);
}
return 0;}

Output:

Page 31
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

(b) Update

Solution :-

#include <stdio.h>
int main() {
int array[100], size, index, new_value;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
printf("Enter the index to update: ");
scanf("%d", &index);
printf("Enter the new value: ");
scanf("%d", &new_value);
if (index < 0 || index >= size) {
printf("Invalid index\n");
return 0;}
array[index] = new_value;
printf("Array after update:");
for (int i = 0; i < size; i++) {
printf(" %d", array[i]);
}
return 0;
}

Output:

Page 32
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

(c) Delete
Solution :-
#include <stdio.h>
int main() {
int array[100], size, element, i, j;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of array:\n");
for (i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
printf("Enter the element to delete: ");
scanf("%d", &element);
for (i = 0; i < size; i++) {
if (array[i] == element) {
for (j = i; j < size - 1; j++) {
array[j] = array[j + 1];
}
size--;
i--;
} }
printf("Array after deletion:");
for (i = 0; i < size; i++) {
printf(" %d", array[i]);
}
return 0; }

Output:

Page 33
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

(d) Display

Solution :-
#include <stdio.h>
int main() {
int array[100], size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
printf("Array elements:");
for (int i = 0; i < size; i++) {
printf(" %d", array[i]);
}
return 0;
}

Output:

Page 34
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

(e) 1. Search for linear

Solution:-

#include <stdio.h>
int main() {
int array[100], size, element, found = 0;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
printf("Enter the element to search: ");
scanf("%d", &element);
for (int i = 0; i < size; i++) {
if (array[i] == element) {
printf("Element found at index %d", i);
found = 1;
break;
} }
if (!found) {
printf("Element not found in the array.");
}
return 0;
}

Output:

Page 35
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

(e)2. Search for Binary

Solution:-
#include <stdio.h>
int binarySearch(int arr[], int size, int key) {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key) {
low = mid + 1; }
else {
high = mid - 1;}
}
return -1;}
int main() {
int size, key;
printf("Enter size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements in ascending order:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter key to search: ");
scanf("%d", &key);
int result = binarySearch(arr, size, key);
if (result != -1) {
printf("Element found at index %d.\n", result);}
else {
printf("Element not found in the array.\n");}
return 0;
}

Output:

Page 36
Rudraksh Bhandhari(2310992512)
Fundamentals of Programming(22CS002)

AIM:- Write a program to calculate the sum of array elements by


passing it to a function

Solution :-
#include <stdio.h>
int calculateSum(int array[], int size);
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int array[size];
printf("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]); }
int sum = calculateSum(array, size);
printf("Sum of array elements: %d\n", sum);
return 0;
} int calculateSum(int array[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}
return sum;
}

Output:

Page 37
Kartik Joshi(2310992115)
Fundamentals of Programming(22CS002)

AIM:- Write a program to show the use of passing pointer as


arguments to the functions.

Solution :-
#include <stdio.h>
void modifyValue(int *num);
int main() {
int number = 10;
printf("Before function call: number = %d\n", number);
modifyValue(&number);
printf("After function call: number = %d\n", number);
return 0; }
void modifyValue(int *num) {
*num = 20;
}

Output:

Page 38
Kartik Joshi(2310992115)
Fundamentals of Programming(22CS002)

AIM:- Write a program matrix multiplication using the concept


of 2D array.

Solution :-
#include <stdio.h>
#define MAX_SIZE 100

void multiplyMatrix(int mat1[][MAX_SIZE], int mat2[][MAX_SIZE],


int result[][MAX_SIZE], int rows1, int cols1, int rows2, int
cols2);

int main() {
int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE],
result[MAX_SIZE][MAX_SIZE];
int rows1, cols1, rows2, cols2;

printf("Enter the number of rows and columns of the first


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

printf("Enter the elements of the 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 of the second


matrix: ");
scanf("%d %d", &rows2, &cols2);

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


for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &mat2[i][j]);
}
}

if (cols1 != rows2) {
printf("Matrix multiplication is not possible.\n");
return 1;
}

multiplyMatrix(mat1, mat2, result, rows1, cols1, rows2,


cols2);

Page 39
Kartik Joshi(2310992115)
Fundamentals of Programming(22CS002)

printf("Result of matrix multiplication:\n");


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

void multiplyMatrix(int mat1[][MAX_SIZE], int mat2[][MAX_SIZE],


int result[][MAX_SIZE], int rows1, int cols1, int rows2, int
cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
}
}

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


for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

Output:

Page 40
Kartik Joshi(2310992115)
Fundamentals of Programming(22CS002)

AIM:- Write a program to transpose a given matrix.

Solution :-
#include <stdio.h>
#define MAX_SIZE 100

void transposeMatrix(int mat[][MAX_SIZE], int rows, int cols);

int main() {
int mat[MAX_SIZE][MAX_SIZE];
int rows, cols;

printf("Enter the number of rows and columns of the matrix:


");
scanf("%d %d", &rows, &cols);

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat[i][j]);
}
}

transposeMatrix(mat, rows, cols);

printf("Transposed matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}

return 0;
}
void transposeMatrix(int mat[][MAX_SIZE], int rows, int cols) {
int temp[MAX_SIZE][MAX_SIZE];

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


for (int j = 0; j < cols; j++) {
temp[j][i] = mat[i][j];
}
}

Page 41
Kartik Joshi(2310992115)
Fundamentals of Programming(22CS002)

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


for (int j = 0; j < rows; j++) {
mat[i][j] = temp[i][j];
}
}
}

Output:

Page 42
Kartik Joshi(2310992115)

You might also like