STRUCTURES
STRUCTURES
#include <stdio.h>
struct student
{
char usn[50];
char name[50];
int marks;
} s[10];
int main()
{
int i, n, countav = 0, countbv = 0, sum = 0;
float average;
printf("Displaying information:\n");
for (i = 0; i < n; i++)
{
printf("USN: %s\n", s[i].usn);
printf("Name: %s\n", s[i].name);
printf("Marks: %d\n", s[i].marks);
printf("\n");
}
return 0;
}
POINTERS
#include <stdio.h>
#include <math.h>
int main()
{
float a[10], *ptr, mean, std, sum=0,sumstd=0;
int i,n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum: %.3f\t\n",sum);
printf("Mean: %.3f\t\n",mean);
printf("Standard Deviation: %.3f\t",std);
return 0;
}
MATRIX MULTIPLICATION
#include <stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
printf("Enter the number of rows and columns for first
matrix\n");
scanf("%d %d",&m,&n);
printf("Enter the number of rows and columns for second
matrix\n");
scanf("%d %d",&p,&q);
if(n==p)
{
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The matrix after multiplication is: \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
printf("The multiplication is not possible");
}
}
STRING MANIPULATION
#include<stdio.h>
#include<string.h>
int main()
{
char s1[10], s2[10];
int len, res;
len = length(s1);
printf("The length of the string s1 is %d\n", len);
len = length(s2);
printf("The length of the string s2 is %d\n", len);
concat(s1, s2);
printf("The concatenated string is %s\n", s1); // Added \n
at the end
return 0;
}
SINE SERIES
#include<stdio.h>
#include<stdlib.h>
int main()
{
float degree,nume,deno,value,terms,x;
int n,i;
printf("Enter the number of terms:\n");
scanf("%d",&n);
printf("Enter the degree:\n");
scanf("%f",°ree);
x=degree*(3.14156/180);
value=0.0;
nume=x;
deno=1;
terms=nume/deno;
for(i=1;i<=n;i++)
{
value=value+terms;
nume=-nume*x*x;
deno=deno*(2*i)*(2*i+1);
terms=nume/deno;
}
printf("Calculated value of sin(%f) is %f\n",degree,value);
printf("Corrected value of sin(%f) is %.3f\n",degree,x);
return 0;