You are on page 1of 13

2021

Name: Bharath.L SRN: PES1UG20ME020 Section: G


Date: 09/06/2021 Week Number: 06

1 1) Write a C program to generate Pascal triangle using two dimensional array

Input:
Enter the n value:
4
Output:
1
11
121
1331

Program:
//CLIENT
#include <stdio.h> #include
"header1.h"
int main()
{
int a[50][50],n;
printf("Enter the n value: \n");
scanf("%d",&n);
pascal_tri(a,n);
return 0;
}
----------------------------------------------------------------------------------------------------------------
//SERVER
#include <stdio.h>
#include "header1.h"
void pascal_tri(int n;int a[][n], int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
if(j==1 || j==i)
{
a[i][j]=1;
}
else

1 Department of Computer Science &Engg, PESU


2021

{
a[i][j]=a[i-1][j]+a[i-1][j-1];

}
}
}
disp_tri(a,n);
}
void disp_tri(int n;int a[][n], int n)
{
int i,j,k;
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
{
printf("");
}
for(j=1;j<=i;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
}
----------------------------------------------------------------------------------------------------------------
//HEADER1
void pascal_tri(int n;int a[][n], int n); void
disp_tri(int n;int a[][n], int n);

Output Screen

shot:

2 Department of Computer Science &Engg, PESU


2021

2 Write a C program to read elements in a matrix and check whether the given matrix is
symmetric matrix or not.

Input:

Enter the value of m


3
Enter the value of n
3
Enter elements in matrix of size 3x3:
1
0
0
0
1
0
0
0
1
Output:

The given matrix is Symmetric matrix:

100

010

001

3 Department of Computer Science &Engg, PESU


2021

Program:
#include <stdio.h>
int main()
{
int m,n;
int r,c,issymmetric;
printf("Enter the value of m: \n");
scanf("%d",&m);
printf("Enter the value of n: \n");
scanf("%d",&n); int A[m]
[n];
int B [m][n];
printf("Enter elements in matrix of size 3x3:\n");
for(r=0;r<m;r++)

4 Department of Computer Science &Engg, PESU


2021

{
for(c=0;c<n;c++)
{
scanf("%d",&A[r][c]);
}
}
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
{
B[r][c]=A[c][r];
}
}
issymmetric=1;
for(r=0;r<n && issymmetric;r++)
{
for(c=0;c<n;c++)
{
if(A[r][c] != B[r][c])
{
issymmetric=0;
break;
}
}
}
if(issymmetric==1)
{
printf("The given matrix is symmetric \n");
for(r=0;r<m;r++)
{
printf("\n");
for(c=0;c<n;c++)
{
printf("%d ",A[r][c]);
}
}
}
else
{
printf("The given matrix is not symmetric \n");
} return
} 0;

5 Department of Computer Science &Engg, PESU


2021

Output Screenshot:

6 Department of Computer Science &Engg, PESU


2021

3 Write a C program to compare 2 dates and print appropriate message using structures

Input1:
Enter Date1 in the format dd/mm/yyyy
12/2/2000
Enter Date2 in the format dd/mm/yyyy
12/2/2000
Date1=12/2/2000
Date2=12/2/2000 Output1:

Date1 is equal to Date2


Input2:
Enter Date1 in the format dd/mm/yyyy
12/3/2000
Enter Date2 in the format dd/mm/yyyy
12/3/2001
Date1=12/3/2000
Date2=12/3/2001 Output2:
Date1 is smaller than Date2

Input3:
Enter Date1 in the format dd/mm/yyyy
12/4/1999
Enter Date2 in the format dd/mm/yyyy
12/2/1999
Date1=12/4/1999
Date2=12/2/1999 Output3:
Date1 is greater than Date2

7 Department of Computer Science &Engg, PESU


2021

Program:
//CLIENT
#include <Stdio.h>
#include "header3.h"
int main()
{
date_info d1;
date_info d2;
printf("Enter Date1 in the format dd/mm/yyyy:\n");
date_read(&d1);
printf("Enter Date2 in the format dd/mm/yyyy:\n");
date_read(&d2); printf("Date1=");
date_disp(&d1); printf("\nDate2=");
date_disp(&d2);

8 Department of Computer Science &Engg, PESU


2021

int res=date_cmp(&d1,&d2);
if(res==0)
{
printf("\nDate1 is equal to Date2\n");
}
else if(res<0)
{
printf("\nDate1 is smaller than Date2\n");
}
else if(res>0)
{
printf("\nDate1 is greater than Date2\n");
}
return 0;
}
----------------------------------------------------------------------------------------------------------------
//SERVER
#include <stdio.h> #include
"header3.h"
void date_read(date_info *d)
{
scanf("%d/%d/%d",&d->dd,&d->mm,&d->yy);
}
int date_cmp(date_info *d1,date_info *d2)
{
int res;
if(d1->dd==d2->dd&&d1->mm==d2->mm&&d1->yy==d2->yy)
res=0;
else if(d1->yy>d2->yy|| d1->yy==d2-
>yy&&d1->mm>d2->mm||d1->yy&&d1-
>mm==d2->mm||d1->dd>d2-
>dd) res=1; else
res=-1;
return res;
}
void date_disp(date_info *d)
{
printf("%d/%d/%d",d->dd,d->mm,d->yy);
}
----------------------------------------------------------------------------------------------------------------
//HEADER3
typedef struct date
{

9 Department of Computer Science &Engg, PESU


2021

int dd;
int mm;
int yy;

}
date_info;
void date_disp(date_info *d);
int date_cmp(date_info *d1,date_info *d2); void
date_read(date_info *d);

Output Screenshot:

(1)

(2)

(3)

10 Department of Computer Science &Engg, PESU


2021

4 Write a C Program to Add and subtract two Complex Numbers by Passing Structure to a
Function Input:
For 1st complex number
Enter the real and imaginary parts: 5
4
For 2nd complex number
Enter the real and imaginary parts: 3
2
Output:
Sum = 8.0 + 6.0i
Sub = 2.0 - 2.0i

Program:
\\CLIENT
#include <stdio.h>
#include "header4.h"
int main()
{
complex n1,n2,sum,sub; printf("For
1st complex number...\n"); printf("Enter the real
and imaginary part: "); scanf("%f n
%f",&n1.real,&n1.imag); printf("For 2st
complex number...\n"); printf("Enter the real
and imaginary part: "); scanf("%f n
%f",&n2.real,&n2.imag); sum=add(n1,n2);

11 Department of Computer Science &Engg, PESU


2021

sub=subtract(n1,n2); printf("Sum= %.2lf+%.2lfi\


n",sum.real,sum.imag); printf("Sub= %.2lf-%.2lfi\
n",sum.real,sum.imag); return 0;
}
-------------------------------------------------------------------------------------------------------
\\SERVER
#include <stdio.h> #include
"header4.h" complex add(complex
n1,complex n2)
{
complex temp;
temp.real=n1.real+n2.real;
temp.imag=n1.imag+n2.imag;
return (temp);
} complex subtract(complex n1,complex
n2)
{
complex temp;
temp.real=n1.real-n2.real;
temp.imag=n1.imag-n2.imag;
return (temp);
}
-------------------------------------------------------------------------------------------------------
\\HEADER4
typedef struct complex
{
float real;
float imag;
}complex;

12 Department of Computer Science &Engg, PESU


2021

complex add(complex n1,complex n2);


complex subtract(complex n1,complex n2);

Output Screenshot:

13 Department of Computer Science &Engg, PESU

You might also like