You are on page 1of 7

4- Array and structure

// program to store elements in array and print them.


#include<stdio.h>
#include<conio.h>
void main()
{
int p[10],i;
printf("enter element in array");
for(i=0;i<=9;i++)
{
scanf("%d",&p[i]);
}
printf("\n element of array are:");
for(i=0;i<=9;i++)
{
printf("\t%d",p[i]);
}
getch();
}
//Write a program to find largest number in an integer array.
#include<stdio.h>
void main()
{
int i, arr[5], max=0;
printf("Enter 5 integers of an array :");
for(i=0;i<5;i++)
{
scanf("%d", &arr[i]);
}
//to find largest
for(i=0;i<5;i++)
{
if(max<=arr[i])
max=arr[i];
}
printf("Elements from array:\n");
for(i=0;i<5;i++)
{
printf("%d\n",arr[i]);
}
printf("largest number : %d",max);
}
//Write a program to find smallest number in an integer array.
#include<stdio.h>
void main()
{
int i, arr[5], min;
printf("Enter 5 integers of an array :");
for(i=0;i<5;i++)
{
scanf("%d", &arr[i]);
}
//to find smallest
Min=arr[0];
for(i=1;i<5;i++)
{
if(min>=arr[i])
min=arr[i];
}
printf("largest number : %d",min);
getch(); }
// program to sort elements in ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
printf(" enter the element in the array");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("the sorted no are ,,,,,,.......>>>>>>");
for(i=0;i<=4;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Write a program for addition of two 3 X 3 matrices.
(Logic – 2 Marks, Syntax-2 Marks)
#include<stdio.h>
#include<conio.h>
void main()
{
int a [3][3], b[3][3],c[3][3],i,j;
printf("Enter First matrix ");
for (i=0;i< 3;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",&a[i][j] );
}
}
printf ("Enter second matrix:" );
{
scanf("%d",&a[i][j] );
}
}
printf ("Enter second matrix:" );
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf ("%d", &b[i][j]);
}
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf ("The resultant matrix is;");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf ("\t%d",c[i][j] );
}
printf ("\n" );
}
getch();
}
// C program to multiply two matrices

#include <stdio.h>
int main()
{
int A[3][3]; // Matrix 1
int B[3][3]; // Matrix 2
int C[3][3]; // Resultant matrix

int row, col, i, sum;

/* Input elements in first matrix from user */


printf("Enter elements in matrix A \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &A[row][col]);
}
}

/* Input elements in second matrix from user */


printf("\n Enter elements in matrix B \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &B[row][col]);
}
}
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
sum = 0;
for(i=0; i<3; i++)
{
sum += A[row][i] * B[i][col];
}

C[row][col] = sum;
}
}

/* Print product of the matrices */


printf("\nProduct of matrix A * B = \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
printf("%d ", C[row][col]);
}
printf("\n");
}

return 0;
}
//Write a program to find transpose of 3 x 3 matrix.
#include<stdio.h>
#include<conio.h>
void main(){
int a[3][3],t[3][3];
int i, j;
clrscr();
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter value");
scanf("%d",&a[i][j]);
}
}
printf("The elements of the matrix are:\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++) {
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++){
for(j=0;j<3;j++) {
t[j][i]=a[i][j];
}
}
printf("The transpose matrix is: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++) {
printf("%d\t",t[i][j]);
}
printf("\n");
}
getch();
}

/*Write a program to declare structure student having member variables are roll-no, name and marks.
Accept data for one student and display it.*/
(Logic - 2 Marks, Syntax – 2 Marks)
Ans: #include<stdio.h>
#include<conio.h>
struct student
{
int roll_no;
char name[20];
float marks;
}s;
void main()
{
clrscr();
printf("Enter student's roll number:");
scanf("%d",&s.roll_no);
printf("\nEnter student's name:");
scanf("%s",s.name);
printf("\nEnter student's marks:");
scanf("%f",&s.marks);
printf("\n\nStudent's details are:");
printf("\nRoll number=%d",s.roll_no);
printf("\nName=%s",s.name);
printf("\nMarks=%f",s.marks);
getch();
}
/*Write a program to declare a structure book having data members, title, author and
price. Accept data and display information for one book.
(Logic – 2 Marks, Syntax-2 Marks) */
#include<stdio.h>
#include<conio.h>
struct book
{
char tit[20];
charauth[20];
int price;
}b1;
void main()
{
printf("enter title of book");
scanf("%s",b1.tit);
printf("enter author name of book");
scanf("%s",b1.auth);
printf("enter price of book");
scanf("%d",&b1.price);
printf("detdails of book are");
printf("\n\t title:%s\n\t author:%s\n\t price:%d",b1.tit,b1.auth,b1.price);
getch();
}
Write a program to find simple interest using structure.
(Logic – 2 Marks, Syntax-2 Marks)
#include<stdio.h>
#include<conio.h>
void main()
{
struct Simple_Interest
{
float p,r,t,si;
}s;
clrscr();
printf("Enter principal amount ");
scanf("%f",&s.p);
printf("Enter rate of interest ");
scanf("%f",&s.r);
printf("Enter time ");
scanf("%f",&s.t);
s.si=(s.p*s.r*s.t)/100;
printf("Simple Interest = %f",s.si);
getch();
}
/*Write a program to declare a structure employee having name, designation and salary.
Accept and display this information for five members.*/
#include<stdio.h>
#include<conio.h>
struct employee
{
char ename[20];
char desg[20];
int salary;
}s[5];
void main()
{
int i;
clrscr();
//Accepting information
printf("Enter details of 5 employees:\n");
for(i=0;i<5;i++)
{
printf("Enter name :");
scanf("%s",s[i].ename);
printf("Enter designation :");
scanf("%s",s[i].desg);
printf("Enter salary :");
scanf("%d",&s[i].salary);
}
//displaying information
printf("The details of emplyoees are :\n");
for(i=0;i<5;i++)
{
printf("%s\t%s\t%d",s[i].ename,s[i].desg,s[i].salary);
printf("\n--------------------\n");
}
}

You might also like