Aim: 6.1 Write a C program to read and store the roll no and marks of 20 students using array.
Code:
#include<stdio.h>
int main()
{
int rollno[20],marks[20],i;
for(i=0;i<20;i++)
{
printf("\nEnter Roll number of Student%d : ",i+1);
scanf("%d",&rollno[i]);
printf("\nEnter Marks of Student : ",i+1);
scanf("%d",&marks[i]);
}
for(i=0;i<20;i++)
{
printf("\n Roll No : %d Marks : %d",rollno[i],marks[i]);
}
return 0;
}
Output screenshot:
Aim:6.2 Write a program to find out which number is even or odd from list of 10 numbers using an array.
Code:
#include<stdio.h>
int main()
{
int num[10],i;
printf("\nEnter 10 Numbers : \n");
for(i=0;i<10;i++)
{
scanf("\n%d",&num[i]);
}
for(i=0;i<10;i++)
{
if(num[i]%2==0)
{
printf("\n%d is Even Number",num[i]);
}
else
{
printf("\n%d is Odd Number",num[i]);
}
}
}
Output screenshot:
Aim: 6.3 Write a program to find the maximum element from 1-Dimensional array.
Code:
#include<stdio.h>
int main()
{
int a[100],i,n,max;
printf("\nEnter How many numbers you want to Enter : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Value in Array at Position%d : ",i+1);
scanf("%d",&a[i]);
if(i==0)
{
max=a[i];
}
else
{
if(max<a[i])
{
max=a[i];
}
}
}
printf("\n Maximum Value in Array = %d",max);
return 0;
}
Output screenshot:
Aim:6.4 Write a C program to calculate the average, geometric and harmonic mean of n elements in an array.
Code:
#include<stdio.h>
#include<math.h>
int main()
{
float a[50],sum=0,sum1 = 0,sum2=1,gem,har;
int i,n;
printf("\n How many number you want to Enter : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter value at position %d : ",i+1);
scanf("%f",&a[i]);
sum=sum+a[i];
sum1 = sum1 + (1.0/a[i]);
sum2= sum * a[i];
}
printf("\n Average = %f ",sum/n);
printf("\n Geometric Mean = %f ",pow(sum2,(1.0/n)));
printf("\n Harmonic Mean = %f",n*pow(sum1,-1));
}
Output screenshot:
Aim: 6.5 Write a program to sort a given array in ascending order (Bubble sort).
Code:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("Enter %d integers : \n",n);
for(c=0; c<n; c++)
scanf("%d", &array[c]);
for(c=0; c<n-1; c++)
{
for(d=0; d<n-c-1; d++)
{
if(array[d]>array[d+1])
{
swap=array[d];
array[d]=array[d+1];
array[d+1]=swap;
}
}
}
printf("Sorted list in ascending order : \n");
for(c=0; c<n; c++)
printf("%d\n",array[c]);
return 0;
}
Output screenshot:
No comments:
Post a Comment
If you have any doubts or suggestions, Please let me know