Student Record using Structure - C Program

Program:
    #include<stdio.h>
    int n,i,j;
    struct student
    {
        char name[20];
        int sub1,sub2,sub3,total;
    };
    struct student st[100],temp;
    void readlist()
    {
        for(i=0;i<n;i++)
        {
            printf("\nEnter the name of student:");
                scanf("%s",st[i].name);
            printf("Enter the marks in 3 subjects:");
                scanf("%d%d%d",&st[i].sub1,&st[i].sub2,&st[i].sub3);
            st[i].total=st[i].sub1+st[i].sub2+st[i].sub3;
        }
    }
    void sortlist()
    {
    for(i=0;i<n;i++)
        {
         for(j=0;j<n-i-1;j++)
            {
             if(st[j].total<st[j+1].total)
                {
                 temp=st[j];
                 st[j]=st[j+1];
                 st[j+1]=temp;
                }
            }
        }
    }
    void display()
    {
        for(i=0;i<n;i++)
        {
            printf("\nRank=%d",i+1);
            printf("\nName:%s",st[i].name);
            printf("\nTotal mark=%d\n",st[i].total);
        }
     }
    void main()
    {
        struct student st[100],temp;
        printf("Enter the number of students:");
        scanf("%d",&n);
        readlist();
        sortlist();
        printf("\n\t<<Sorted student Record>>");
        display();
    }

Output
nn@linuxmint ~ $ gcc c15.c
nn@linuxmint ~ $ ./a.out
Enter the number of students:3

Enter the name of student:varun
Enter the marks in 3 subjects:35
30
40

Enter the name of student:athira
Enter the marks in 3 subjects:39
45
40

Enter the name of student:afsal
Enter the marks in 3 subjects:40
45
49

    <<Sorted student Record>>
Rank=1
Name:afsal
Total mark=134

Rank=2
Name:athira
Total mark=124

Rank=3
Name:varun
Total mark=105
nn@linuxmint ~ $

Evaluation of eX Series - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void ex(int y,int n)
{
    int i;
    float term=1,sum=1;
    for(i=1;i<=n;i++)
    {
        term=((term)*y)/(i);
        sum=sum+term;
    }
    printf("Sum of the ex series=%f\n",sum);
 }
void main()
{
     int n,y;
     float x;//clrscr();
     printf("Enter the no of terms:");
     scanf("%d",&n);
     printf("Enter the value:");
     scanf("%f",&x);
     ex(x,n);
    // getch();
}

Output:
nn@linuxmint ~ $ gcc c13.c
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
Enter the value:30
Sum of the ex series=238829632.000000
nn@linuxmint ~ $

Evaluation of CosX Series - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void cosx(float x,int n)
{
    int i;
    float term=1,sum=1;
    for(i=1;i<=n;i++)
    {
        term=((-term)*(x*x))/((2*i)*(2*i-1));
        sum=sum+term;
    }
    printf("Sum of the cos series=%f\n",sum);
}
void main()
{
    int n,y;
    float x;//clrscr();
    printf("Enter the no of terms:");
    scanf("%d",&n);
    printf("enter the value in degree:");
    scanf("%f",&x);
    y=x;
    x=x*(3.14/180);
    cosx(x,n);
//      getch();
}

Output:
nn@linuxmint ~ $ gcc c12.c
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
enter the value in degree:0
Sum of the cos series=1.000000
nn@linuxmint ~ $ ./a.out
Enter the no of terms:0
enter the value in degree:0
Sum of the cos series=1.000000
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
enter the value in degree:60
Sum of the cos series=0.500460
nn@linuxmint ~ $

Evaluation of SinX Series - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void sinx(float x,int n)
{
int i;
float term=x,sum=x;
for(i=1;i<=n;i++)
{
term=((-term)*(x*x))/((2*i)*(2*i+1));
sum=sum+term;
}
printf("Sum of the sin series=%f\n",sum);
}

void main()
{
int n,y;
float x;//clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("enter the value in degree:");
scanf("%f",&x);
y=x;
x=x*(3.14/180);
sinx(x,n);
//getch();
}

Output:
nn@linuxmint ~ $ gcc c11.c
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:90
Sum of the sin series=1.000000
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:0
Sum of the sin series=0.000000
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:30
Sum of the sin series=0.499770
nn@linuxmint ~ $

Check Palindrome or not - C Program

Program:
#include<stdio.h>
#include<string.h>
//#include<conio.h>
int ispalindrome(char str[20])
{
     int left,right,flag;
     if(strlen(str)==0)
        return 1;
     else
     {
        left=0;
        right=strlen(str)-1;
        flag=1;
        while(left<right && flag)
        {
         if (str[left]!= str[right])
            flag=0;
         else
            {
             left++;
             right--;
            }
        }
       return flag;
     }
}
void main()
{
     char str[200];
     printf("Enter the string: ");
     scanf("%s",str);
     if(ispalindrome(str))
        printf("String is palindrome.\n");
     else
     printf("String is not palindrome.\n");
//     getch();
}

Output:
nn@linuxmint ~ $ gcc c10.c
nn@linuxmint ~ $ ./a.out
Enter the string: malayalam
String is palindrome.
nn@linuxmint ~ $

Matrix Multiplication - C Program

Program:
#include<stdio.h>
//#include<conio.h>
int a[10][10],b[10][10],c[10][10],i,j,k;

void read(int r1,int c1,int a[10][10])
{   
       for(i=0;i<r1;++i)
       for(j=0;j<c1;++j)
       scanf("%d",&a[i][j]);
}

void mul(int r1,int c2,int c1)
{
     for(i=0;i<r1;++i)
      for(j=0;j<c2;++j)
    {
            c[i][j]=0;
            for(k=0;k<c1;++k)
             c[i][j]=c[i][j]+a[i][k]*b[k][j];
    }
}

void printmat(int r1,int c2)
{
     for(i=0;i<r1;++i)
    {         
          for(j=0;j<c2;++j)
          printf("%d\t",c[i][j]);
        printf("\n");
         }
}

void main()
{
     int r1,c1,r2,c2;//clrscr();
    printf("Enter order of matrix 1:");
    scanf("%d %d",&r1,&c1);
    printf("Enter order of matrix 2:");
    scanf("%d %d",&r2,&c2);
    if(c1!=r2)
     printf("Matrices cannot be multiplied");
    else
    {
        printf("Enter Elements for matrix 1:\n");
         read(r1,c1,a);
        printf("Enter Elements for matrix 2:\n");
          read(r2,c2,b);
         mul(r1,c2,c1);
        printf("Resultant matrix :\n");
          printmat(r1,c2);
        }
//    getch();
}

Output:
nn@linuxmint ~ $ gcc c9.c
nn@linuxmint ~ $ ./a.out
Enter order of matrix 1:2
2
Enter order of matrix 2:2
2
Enter Elements for matrix 1:
1
1
1
1
Enter Elements for matrix 2:
2
2
2
2
Resultant matrix :
4    4   
4    4   
nn@linuxmint ~ $

Matrix Addition - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
    int a[100][100],b[100][100],c[100][100],i,j,m,p,q,n;
    printf("Enter the order for first matrix:");
    scanf("%d %d",&m,&n);
    printf("Enter the elements for first matrix:");
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
        scanf("%d",&a[i][j]);
    printf("Enter the order for second matrix:");
    scanf("%d %d",&p,&q);
    printf("Enter the elements for second matrix:");
    for(i=0;i<p;i++)
    for(j=0;j<q;j++)
    scanf("%d",&b[i][j]);
   
    if(m==p && p==q)
    {
        for(i=0;i<p;i++)
        for(j=0;j<q;j++)
            c[i][j]=a[i][j]+b[i][j];
        printf("Resultant matrix:\n");
        for(i=0;i<p;i++)
        {
            for(j=0;j<q;j++)
                printf("%d ",c[i][j]);
                printf("\n");
        }
    }
    else
        printf("Operation not possible.\n");
//getch();
}

Output:
nn@linuxmint ~ $ gcc c8.c
nn@linuxmint ~ $ ./a.out
Enter the order for first matrix:2
2
Enter the elements for first matrix:1
1
1
1
Enter the order for second matrix:2
2
Enter the elements for second matrix:2
2
2
2
Resultant matrix:
3 3
3 3
nn@linuxmint ~ $

Find GCD(greatest common divisor)& LCM (least common multiple) of Two Numbers - C Program

Program:
#include<stdio.h>
//#include<conio.h>
//void
main()
{
    int r,a,b,l,m,n;
    printf("Enter the numbers:\n");
    scanf("\n%d%d",&m,&n);
    a=m;
    b=n;
    while(r!=0)
    {
        r=m%n;
        m=n;
        n=r;
    }
    printf("G C D: %d\n",m);
    l=a*b/m;
    printf("L C M: %d\n",l);
//       getch();
}

Output:
nn@linuxmint ~ $ gcc c7.c
nn@linuxmint ~ $ ./a.out
Enter the numbers:
18
12
G C D: 6
L C M: 36
nn@linuxmint ~ $

Bubblesort - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
    int a[100],i,j,n,t;
    printf("Enter the number of elements: ");
    scanf("%d",&n);
    printf("\nInput the elements\n");
    for(i=1;i<=n;i++)
    {
          scanf("%d",&a[i]);
    }
    for(i=1;i<n;i++)
    {
          for(j=1;j<=n-i;j++)
        {
             if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
         }
        }
    printf("After sorting: \n");
    for(i=1;i<=n;i++)
    {
          printf("%d ",a[i]);
    }
    printf("\n");
//      getch();
}

Output:
nn@linuxmint ~ $ gcc c6.c
nn@linuxmint ~ $ ./a.out
Enter the number of elements: 5

Input the elements
5
4
3
2
1
After sorting:
1 2 3 4 5
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...