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

Ass 04

The document contains the answers to 4 questions on C programming assignments. It includes code samples to print unique elements in an array, delete an element from an array at a desired position, perform matrix multiplication of two square matrices, and find the transpose of a given matrix. For each question, it provides the question prompt, code sample as the answer, and sample input/output of the code.

Uploaded by

Neer Jain
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)
42 views17 pages

Ass 04

The document contains the answers to 4 questions on C programming assignments. It includes code samples to print unique elements in an array, delete an element from an array at a desired position, perform matrix multiplication of two square matrices, and find the transpose of a given matrix. For each question, it provides the question prompt, code sample as the answer, and sample input/output of the code.

Uploaded by

Neer Jain
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/ 17

Jyotirmay SINGH PARIHAR- 23H027

Assignment 04

Question1: Write a program in C to print all unique elements in an array.


Answer1:

Input: #include <stdio.h>

// Main function

int main()

int arr1[100], n, ctr = 0; // Declare an array to store integer values, n for array size, and ctr for
counting duplicates

int i, j, k; // Declare loop counters

// Prompt the user to input the number of elements to be stored in the array

printf("\n\nPrint all unique elements of an array:\n");

printf("------------------------------------------\n");

printf("Input the number of elements to be stored in the array: ");

scanf("%d", &n);

// Prompt the user to input n elements into the array

printf("Input %d elements in the array :\n", n);

for (i = 0; i < n; i++)

printf("element - %d : ", i);

scanf("%d", &arr1[i]); // Read the input and store it in the array

// Print unique elements in the array

printf("\nThe unique elements found in the array are: \n");

for (i = 0; i < n; i++)

ctr = 0; // Reset the counter for each element

for (j = 0, k = n; j < k + 1; j++)

{ /* Increment the counter when the search value is duplicate. */

if (i != j)
Jyotirmay SINGH PARIHAR- 23H027

{ if (arr1[i] == arr1[j])

{ ctr++;

if (ctr == 0)

printf("%d ", arr1[i]); // Print the unique element

printf("\n\n");

return 0; // Return 0 to indicate successful execution

}
Output:Print all unique elements of an array:
------------------------------------------
Input the number of elements to be stored in the array: 4
Input 4 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 2
element - 3 : 5

The unique elements found in the array are:


3 5

Question2: Write a program in C to delete an element at desired position from an array.

Answer2: #include <stdio.h>

void main() {
int arr1[50], i, pos, n;

// Prompt user for input


printf("\n\nDelete an element at the desired position from an array:\n");
printf("---------------------------------------------------------\n");
printf("Input the size of the array : ");
scanf("%d", &n);
Jyotirmay SINGH PARIHAR- 23H027

// Input values for the array


printf("Input %d elements in the array in ascending order:\n", n);
for (i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &arr1[i]);
}

// Input the position to delete


printf("\nInput the position where to delete: ");
scanf("%d", &pos);

// Locate the position in the array


i = 0;
while (i != pos - 1)
i++;

// Shift elements to the left to replace the deleted element


while (i < n) {
arr1[i] = arr1[i + 1];
i++;
}
n--;

// Display the new list after deletion


printf("\nThe new list is : ");
for (i = 0; i < n; i++) {
printf(" %d", arr1[i]);
}
printf("\n\n");
}
Jyotirmay SINGH PARIHAR- 23H027

delete an element at desired position from an array :


---------------------------------------------------------
Input the size of array : 5
Input 5 elements in the array in ascending order:
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5

Input the position where to delete: 3

The new list is : 1 2 4 5

Question3: Write a program in C for multiplication of two square Matrices.


Answer3: #include <stdio.h>

int main() {
// Declare matrices and variables
int arr1[50][50], brr1[50][50], crr1[50][50], i, j, k, r1, c1, r2, c2, sum = 0;

// Display multiplication of two matrices


printf("\n\nMultiplication of two Matrices :\n");
printf("----------------------------------\n");

// Input rows and columns of the first matrix


printf("\nInput the rows and columns of the first matrix: ");
scanf("%d %d", &r1, &c1);

// Input rows and columns of the second matrix


printf("\nInput the rows and columns of the second matrix: ");
scanf("%d %d", &r2, &c2);

// Check if multiplication is possible


if (c1 != r2) {
printf("Multiplication of matrices is not possible.\n");
printf("Column of the first matrix and row of the second matrix must be the same.\n");
Jyotirmay SINGH PARIHAR- 23H027

} else {
// Input elements in the first matrix
printf("Input elements in the first matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &arr1[i][j]);
}
}

// Input elements in the second matrix


printf("Input elements in the second matrix:\n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &brr1[i][j]);
}
}

// Display the first matrix


printf("\nThe First matrix is:\n");
for (i = 0; i < r1; i++) {
printf("\n");
for (j = 0; j < c1; j++)
printf("%d\t", arr1[i][j]);
}

// Display the second matrix


printf("\nThe Second matrix is:\n");
for (i = 0; i < r2; i++) {
printf("\n");
for (j = 0; j < c2; j++)
Jyotirmay SINGH PARIHAR- 23H027

printf("%d\t", brr1[i][j]);
}

// Matrix multiplication
for (i = 0; i < r1; i++) { // Row of first matrix
for (j = 0; j < c2; j++) { // Column of second matrix
sum = 0;
for (k = 0; k < c1; k++)
sum = sum + arr1[i][k] * brr1[k][j];
crr1[i][j] = sum;
}
}

// Display the result of matrix multiplication


printf("\nThe multiplication of two matrices is:\n");
for (i = 0; i < r1; i++) {
printf("\n");
for (j = 0; j < c2; j++)
printf("%d\t", crr1[i][j]);
}
}
printf("\n\n");
return 0;
}

Output: Multiplication of two Matrices :


Jyotirmay SINGH PARIHAR- 23H027

----------------------------------

Input the rows and columns of first matrix : 2


2

Input the rows and columns of second matrix : 2


2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8

The First matrix is :

1 2
3 4
The Second matrix is :

5 6
7 8
The multiplication of two matrices is :

19 22
43 50

Question4: Write a program in C to find transpose of a given matrix, Input the rows and columns of
the, matrix : 3 3
Answer4: input:
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
Jyotirmay SINGH PARIHAR- 23H027

printf("Enter element a%d%d: ", i + 1, j + 1);


scanf("%d", &a[i][j]);
}

// printing the matrix a[][]


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");
}

// computing the transpose


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// printing the transpose


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;
}

Question5: Write a program in C to check whether a given matrix is an identity matrix.


Answer5: #include <stdio.h>
Jyotirmay SINGH PARIHAR- 23H027

// In a square matrix, if all the main diagonal elements are 1's and
// all the remaining elements are 0's, it is called an Identity Matrix.
int main() {
int arr1[10][10];
int r1, c1;
int i, j, yn = 1;

// Display the purpose of the program


printf("\n\n Check whether a given matrix is an identity matrix :\n");
printf("-----------------------------------------------------------\n");

// Input the number of Rows for the matrix


printf("Input number of Rows for the matrix :");
scanf("%d", &r1);

// Input the number of Columns for the matrix


printf("Input number of Columns for the matrix :");
scanf("%d", &c1);

// Input elements into the matrix


printf("Input elements in the matrix :\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &arr1[i][j]);
}
}

// Display the matrix


printf("The matrix is :\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1 ;j++) {
Jyotirmay SINGH PARIHAR- 23H027

printf("% 4d", arr1[i][j]);


}
printf("\n");
}

// Check whether the matrix is an identity matrix or not


for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
if (arr1[i][j] != 1 && arr1[j][i] != 0) {
yn = 0;
break;
}
}
}

// Display the result


if(yn == 1) {
printf(" The matrix is an identity matrix.\n\n");
} else {
printf(" The matrix is not an identity matrix.\n\n");
}

return 0;
}

Question6: Write a program in C to print individual characters of a string in reverse order.


TestData :
Input the string : w3resource.com
Expected Output :
The characters of the string in reverse are :
m o c . e c r u o s e r 3 w
answer6:
input:
#include <stdio.h>
#include <string.h>
Jyotirmay SINGH PARIHAR- 23H027

#include <stdlib.h>

void main()
{
char str[100]; /* Declares a string of size 100 */
int l,i;

printf("\n\nPrint individual characters of string in reverse order :\n");


printf("------------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are : \n");
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
}
Print individual characters of string in reverse order :
Output:
-----------------------------------------------------------
Input the string : w3resource.com
The characters of the string in reverse are :

m o c . e c r u o s e r 3 w

Question7: Write a program in C to count the total number of alphabets, digits and special characters
in a string.
TestData :
Input the string : Welcome to w3resource.com
Expected Output :
Number of Alphabets in the string is : 21
Number of Digits in the string is : 1
Number of Special characters in the string is : 4
Answer7:
Input: #include <stdio.h>
#include <string.h>
Jyotirmay SINGH PARIHAR- 23H027

#include <stdlib.h>

#define str_size 100 //Declare the maximum size of the string

void main()
{
char str[str_size];
int alp, digit, splch, i;
alp = digit = splch = i = 0;

printf("\n\nCount total number of alphabets, digits and special characters :\n");


printf("--------------------------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);

/* Checks each character of string*/

while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alp++;
}
else if(str[i]>='0' && str[i]<='9')
{
digit++;
}
else
{
splch++;
Jyotirmay SINGH PARIHAR- 23H027

i++;
}

printf("Number of Alphabets in the string is : %d\n", alp);


printf("Number of Digits in the string is : %d\n", digit);
printf("Number of Special characters in the string is : %d\n\n", splch);
}
Count total number of alphabets, digits and special
Output:
characters :
--------------------------------------------------------------
------
Input the string : Welcome to w3resource.com
Number of Alphabets in the string is : 21
Number of Digits in the string is : 1
Number of Special characters in the string is : 4

Question8: Write a C program to check whether a string is palindrome or not.


Answer8:

#include <stdio.h>
input:
#include <string.h>

int main() {
char string1[20];
int i, length;
int flag = 0;

// Prompt the user for input


printf("Enter a string: ");
scanf("%s", string1);

// Calculate the string length


length = strlen(string1);

// Compare characters from the start and end of the string


// and stop if a mismatch is found or the middle of the string is reached.
for (i = 0; i < length / 2; i++) {
if (string1[i] != string1[length - i - 1]) {
flag = 1;
break;
}
}
Jyotirmay SINGH PARIHAR- 23H027

// Output the result


if (flag) {
printf("%s is not a palindrome\n", string1);
} else {
printf("%s is a palindrome\n", string1);
}

return 0;
}

Question9: Write a program to develop your own functions for performing following operations on
strings. i. Copying one string to another.

ii. Comparing two strings


iii. Adding a string to the end of another string.

Answer9:

i input:

#include <stdio.h>
int main() {
char s1[1000];
char s2[1000];
printf("Enter any string: ");
gets(s1);
int i;
for(i=0;s1[i]!='\0';i++) {
s2[i]=s1[i];
}
s2[i]='\0';

printf("original string s1='%s'\n",s1);


printf("copied string s2='%s'",s2);
return 0;
}

Ii input: #include <stdio.h>


#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
Jyotirmay SINGH PARIHAR- 23H027

scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}

Question10: Write a program to check whether the entered number is circular prime, partially circular
prime or not a circular prime. If the 5 digit number is taken as input from the keyboard and check
every possible circular rotation of input number is prime or not.

 If all the rotation of a number is prime then say entered is circular prime.
 If half of the rotation of a number is prime not all then say entered number is partially prime.
If no rotation is prime and the only number is prime then say entered number is not a circular prime

Answer10:
input: // Program to check if a number is circular
// prime or not.
#include <iostream>
#include <cmath>
using namespace std;

// Function to check if a number is prime or not.


bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
Jyotirmay SINGH PARIHAR- 23H027

// This is checked so that we can skip


// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i = i + 6)


if (n % i == 0 || n % (i + 2) == 0)
return false;

return true;
}

// Function to check if the number is circular


// prime or not.
bool checkCircular(int N)
{
// Count digits.
int count = 0, temp = N;
while (temp) {
count++;
temp /= 10;
}

int num = N;
while (isPrime(num)) {

// Following three lines generate the next


// circular permutation of a number. We move
// last digit to first position.
int rem = num % 10;
int div = num / 10;
num = (pow(10, count - 1)) * rem + div;
Jyotirmay SINGH PARIHAR- 23H027

// If all the permutations are checked and


// we obtain original number exit from loop.
if (num == N)
return true;
}

return false;
}

// Driver Program
int main()
{
int N = 1193;
if (checkCircular(N))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy