0% found this document useful (0 votes)
7 views

Assignment 9

C program
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)
7 views

Assignment 9

C program
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/ 5

Assignment :- 9

Q1. Write a program to take two 2-D arrays as input from the user and perform the
following operations:
a) Sum of the arrays

b) Product of the arrays

CODE a)
#include<stdio.h>
int main(){
int a[50][100], n1,n2,i,j,sum = 0;
printf("Enter the Rows numbers in array\n");
scanf("%d",&n1);
printf("Enter the column numbers in array\n");
scanf("%d",&n2);

for(i = 0;i<n1;i++){
for(j=0;j<n2;j++){
printf("Enter a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i = 0;i<n1;i++){
for(j=0;j<n2;j++){
sum = sum + a[i][j];
}
}
printf("The sum of elements of 2D array is %d\n",sum);
return 0;
}
OUTPUT:
CODE b)
#include<stdio.h>
int main(){
int a[50][100], n1,n2,i,j,product=1;

printf("Enter the Rows numbers in array\n");


scanf("%d",&n1);
printf("Enter the column numbers in array\n");
scanf("%d",&n2);

for(i = 0;i<n1;i++){
for(j=0;j<n2;j++){
printf("Enter a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}

for(i = 0;i<n1;i++){
for(j=0;j<n2;j++){
product = product * a[i][j];
}
}

printf("The product of elements of 2D array is %d",product);


return 0;
}
OUTPUT:
Q2. Write a program to ta string as input from the user and perform the following
operations:
a) Calculate the length of string
b) Find the count of each element in the string
c) Delete the duplicate characters in the string

CODE a)
#include<stdio.h>
#include<string.h>
int main(){
char c[100];
int i=0,count=0;

printf("Enter String\n");
scanf("%s",c);

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

printf("The length of the string is %d",count);


return 0;
}
OUTPUT:

CODE b)
#include<stdio.h>
#include<string.h>
int main(){
char c[100],org[100];
int count,l,i,j;

printf("Enter String\n");
scanf("%s",c);

l = strlen(c);
for(i=0;i<=l;i++){
org[i]=c[i];
}

for(i = 0;i<l;i++){
count = 1;
for(j = i+1;j<l;j++){
if(c[i]==c[j]){
count++;
for(int m = j;m<l;m++){
c[m]=c[m+1];
}l--;
}
} printf("The count of element %c in %s is %d\n", c[i],org,count);
}
return 0;
}
OUTPUT:

CODE c)
#include<stdio.h>
#include<string.h>

int main(){
char c[100],org[100];
int count,l,i,j;

printf("Enter String\n");
scanf("%s",c);

l = strlen(c);
for(i = 0;i<l;i++){
count = 1;
for(j = i+1;j<l;j++){
if(c[i]==c[j]){
count++;
for(int m = j;m<l;m++){
c[m]=c[m+1];
}l--;
}
}

}printf("The string after deleting the duplicate elements is %s", c);

return 0;
}
OUTPUT:

You might also like