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

prog in c assig

The document contains a series of C programming assignments that cover various topics such as reading and printing arrays, calculating sums and averages, matrix operations, and implementing binary search. Each assignment includes code snippets demonstrating the required functionality, along with prompts for user input. The assignments also explore string manipulation functions and pattern printing in C.

Uploaded by

sohan119singh
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)
6 views

prog in c assig

The document contains a series of C programming assignments that cover various topics such as reading and printing arrays, calculating sums and averages, matrix operations, and implementing binary search. Each assignment includes code snippets demonstrating the required functionality, along with prompts for user input. The assignments also explore string manipulation functions and pattern printing in C.

Uploaded by

sohan119singh
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/ 26

Assignment-4

Q.1-Read and print elements in an array at runtime .


Ans:
#include<stdio.h>

int main(){
int arr[100] , n ;
printf("Enter numbers of element:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(int i=1;i<=n;i++){
scanf("%d",&arr[i]);
}
printf("The elements in array are given below:\n");
for(int i=1;i<=n;i++){
printf("%d\t",arr[i]);
}
return 0 ;
}
Output:
Q.2-Read marks of 10 students and calculate sum and average using array.
Ans:
#include<stdio.h>

int main(){
int marks[10] , sum=0;
float avg;
printf("Enter your marks of 10 subject\n");
for(int i=1;i<=10;i++){
scanf("%d",&marks[i]);
sum = sum+marks[i];
}
avg= sum/10;
printf("The sum of your marks:%d\n",sum);
printf("Average of your marks:%d\n",avg);
return 0 ;
}
Output:
Q.3-Write a program in C to read an array of 10 integers and count total
number of even and odd.
Ans-
#include<stdio.h>

int main(){
int arr[10],i,count_even=0,count_odd=0;
printf("Enter 10 elements in array:\n");
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
printf("Elements in array:\n");
for(i=0;i<10;i++){
printf("%d\t",arr[i]);
}
printf("\n");
for(i=0;i<10;i++){
if(arr[i]%2==0){
count_even++;
}
if(arr[i]%2!=0){
count_odd++;
}
}
printf("Total even numbers in array:%d\n",count_even);
printf("Total odd numbers in array:%d\n",count_odd);

return 0;
}
Output:
Assignment-5
Q.1-Write a Program in C to print and sum of 2D array.
Ans-
#include<stdio.h>

int main(){
int arr[2][3],i,j,sum=0;
printf("Enter elements in 2D array:\n ");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value at [%d][%d]:",i,j);
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d\t",arr[i][j]);
sum+=arr[i][j];
}
printf("\n");
}
printf("Sum of array is:%d",sum);
return 0;
}
Output:
Q.2-Write a program in C to print transpose of matrix.
Ans-
#include<stdio.h>

int main(){
int arr[2][3],i,j,sum=0;
printf("Enter elements in 2D array:\n ");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value at [%d][%d]:",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("Elements in array:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d\t",arr[i][j]);
arr[i][j]=arr[j][i];
}
printf("\n");
}
printf("Transpose of array:\n");
for(i=0;i<3;i++){
for(j=0;j<2;j++){
printf("%d\t",arr[j][i]);
}
printf("\n");
}
return 0;
}
Output:
Assignment-6
Q.1-Write a program in C to print the sum of two matrices.
Ans-
#include<stdio.h>

int main(){
int m1[2][3],m2[2][3],sum[2][3];
int i,j;
printf("Enter elements in matrix1:\n ");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value at [%d][%d]:",i,j);
scanf("%d",&m1[i][j]);
}
}
printf("Enter elements in matrix2:\n ");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value at [%d][%d]:",i,j);
scanf("%d",&m2[i][j]);
}
}
printf("Matrix 1:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d",m1[i][j]);
}
printf("\n");
}
printf("Matrix 2:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d",m2[i][j]);
}
printf("\n");
}
printf("Sum:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
sum[i][j]=m1[i][j]+m2[i][j];
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Q.2-Write a program in C to print multiplication of two matrices.
Ans-
#include<stdio.h>

int main(){
int m1[2][3],m2[2][3],product[2][3];
int i,j;
printf("Enter elements in matrix1:\n ");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value at [%d][%d]:",i,j);
scanf("%d",&m1[i][j]);
}
}
printf("Enter elements in matrix2:\n ");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value at [%d][%d]:",i,j);
scanf("%d",&m2[i][j]);
}
}
printf("Matrix 1:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d",m1[i][j]);
}
printf("\n");
}
printf("Matrix 2:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d",m2[i][j]);
}
printf("\n");
}
printf("Product:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
product[i][j]=m1[i][j]*m2[i][j];
printf("%d\t",product[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Q.3-Write a program in C to find any data with binary search.
Ans-
#include<stdio.h>

int main(){
int arr[100],n,i,search,low,high,mid;
printf("Enter number of elements in array:\n");
scanf("%d",&n);
printf("Enter elements in array:\n");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
printf("%d\t",arr[i]);
}
printf("\n");
printf("Enter a element to find it:\n");
scanf("%d",&search);
low=0;
high=n-1;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==search){
printf("Element is found at position %d\n",mid+1);
break;
}else if(arr[mid]<search){
low=mid+1;
}else if(arr[mid]>search){
high=mid-1;
}else {
printf("Element not found\n");
}
}
return 0;

}
Output-
Assignment-7
Q.1-Write a program in C to print the following pattern:-
1).
*
**
***
****
*****
2).
*
**
***
****
*****
3).
*****
****
***
**
*
4).
1
12
123
1234
12345
Ans-
1).
#include<stdio.h>

int main(){
int i,j,n;
printf("Enter number of lines:");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
Output:

2).
#include<stdio.h>
int main(){
int i,j,n;
printf("Enter number of lines:");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if((i+j)<=n){
printf(" ");
}else{
printf("*");
}
}
printf("\n");
}
return 0;
}
Output:
3).
#include<stdio.h>

int main(){
int i,j,n;
printf("Enter number of lines:");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=n;j>=i;j--){
printf("*");
}
printf("\n");
}
return 0;
}
Output:
4).
#include<stdio.h>

int main(){
int i,j,n;
printf("Enter number of lines:");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Output:
Assignment-8
Q.1-Write a program in C to execute all gets() and puts() function.
Ans-
#include<stdio.h>

int main(){
char str[100];
printf("Enter your full name:-\n");
gets(str);
printf("Your name is:-");
puts(str);
return 0;
}
Output:
Q.2-Write a program in C to execute all the predefine function in string:-
strlen(), strcmp(), strcpy(), strcat().
Ans-
#include<stdio.h>

int main(){
char str1[100],str2[100],str3[100];
printf("Enter your first name:\n");
gets(str1);
printf("Enter your last name:\n");
gets(str2);

int length = strlen(str1);


printf("Length of str1 is %d\n",length);

int compare= strcmp(str1,str2);


if(compare==0){
printf("Strings are equal\n");
}else{
printf("Strings are not equal\n");
}

strcpy(str3,str1);
printf("Copied str1 into str3: %s\n",str3);
strcat(str1,str2);
printf("After conactination: %s",str1);

return 0;
}
Output:

You might also like