Tuesday, 26 December 2017

Structure Programs In C

To print information of a student using structure

#include <stdio.h>
#include<conio.h>
struct student
{
    char name[50];
    int roll;
    float marks;
} s1;

void main()
{
    clrscr();
    printf("Enter information:\n");

    printf("Enter name: ");
    scanf("%s", s1.name);

    printf("Enter roll number: ");
    scanf("%d", &s1.roll);

    printf("Enter marks: ");
    scanf("%f", &s1.marks);


    printf("Student Information:\n");
  printf("Name: %s\n",s1.name);
    printf("Roll number: %d\n",s1.roll);
  printf("Marks: %f\n", s1.marks);
getch();
}

OUTPUT:

Enter information:                                                                                                             
Enter name: khusi                                                                                                              
Enter roll number: 12                                                                                                          
Enter marks: 87                                                                                                                
Student Information:                                                                                                           
Name: khusi        
Roll number: 12                                                                                                                
Marks: 87.000000

To print information of a Employee using structure
#include <stdio.h>
#include<conio.h>
struct Employee
{
    char name[50];
    int id;
    float salary;
} e1;

void main()
{
    clrscr();
    printf("Enter  Employee information:\n");

    printf("Enter NAME: ");
    scanf("%s", e1.name);

    printf("Enter ID: ");
    scanf("%d", &e1.id);

    printf("Enter SALARY: ");
    scanf("%f", &e1.salary);


    printf("Employee Information:\n");

    printf("Name: %s\n",e1.name);
    printf("Id: %d\n",e1.id);

    printf("Salary: %f\n", e1.salary);

   getch();
}

OUTPUT

Enter  Employee information:                                                                                                   
Enter NAME: surya                                                                                                              
Enter ID: 123                                                                                                                  
Enter SALARY: 35000                                                                                                            
Employee Information:                                                                                                          
Name: surya
Id: 123                                                                                                                        
Salary: 35000.000000 

To store Information of 5 Students using structure

#include <stdio.h>
#include<conio.h>
struct student
{
    char name[20];
    int roll;
    float marks;
} s1[5];

void main()
{
    int i;
  clrscr();
    printf("Enter information of five students:\n");
    for(i=0; i<5; ++i)
    {
        s1[i].roll = i+1;

        printf("Student roll number %d,\n",s1[i].roll);

        printf("Enter name: ");
        scanf("%s",s1[i].name);

        printf("Enter marks: ");
        scanf("%f",&s1[i].marks);

        printf("\n");
    }

    printf("Displaying  Student Information:\n\n");
   for(i=0; i<5; ++i)
    {
        printf("\nRoll number: %d\n",i+1);
         printf("Name: %s",s1[i].name);
        printf("Marks: %f",s1[i].marks);
          }
   getch();
}

OUTPUT
Enter information of students:                                                                                                 
Student roll number1,                                                                                                          
Enter name: rosan                                                                                                              
Enter marks: 34
//Enter information up to 5 students 

To Add Two Distances (in inch-feet) Using Structure
#include <stdio.h>
#include<conio.h>
struct Distance
{
    int feet;
    float inch;
} d1, d2, sod; //sod means sum of distances

void main()
{
    clrscr();
    printf("Enter information for 1st distance\n");
    printf("Enter feet: ");
    scanf("%d", &d1.feet);
    printf("Enter inch: ");
    scanf("%f", &d1.inch);

    printf("Enter information for 2nd distance\n");
    printf("Enter feet: ");
    scanf("%d", &d2.feet);
    printf("Enter inch: ");
    scanf("%f", &d2.inch);


    sod.feet = d1.feet+d2.feet;
    sod.inch = d1.inch+d2.inch;

  
    if (sod.inch>12.0)
    {
        sod.inch = sod.inch-12.0;
        ++sod.feet;
    }

    printf("\nSum of distances = %d\'-%f\"",sod.feet, sod.inch);
   getch();
}

OUTPUT:

Enter information for 1st distance                                                                                             
Enter feet: 10                                                                                                                 
Enter inch: 12                                                                                                                 
                                                                                                                               
Enter information for 2nd distance                                                                                             
Enter feet: 12
Enter inch: 24                                                                                                                 
                                                                                                                               
Sum of distances = 23'-24.000000"

To store information of books using structure

#include <stdio.h>
#include<conio.h>
struct book
{
    char Author_Name[20];
    char Book_Name[20];
    int pageNo;
   
} p1[5];

void main()
{
    int i;
   clrscr();
    printf("Enter information of Books:\n");

    for(i=0; i<5; ++i)
    {
       printf("Enter AuthorName:\n");
        scanf("%s",p1[i].Author_Name);
       
 printf("Enter BookName:\n");
        scanf("%s",p1[i].Book_Name);

        printf("Enter Page:\n");
        scanf("%d",&p1[i].pageNo);

      
    }

    printf("Displaying  Book Information:\n");

    for(i=0; i<5; ++i)
    {
        printf("AuthorName: %s\n",p1[i].Book_Name);
      
         printf("Name: %s",p1[i].Book_Name);
        printf("Marks: %d",p1[i].pageNo);
      
    }
   getch();
}

OUTPUT

Enter information of Books:                                                                                                    
Enter AuthorName:                                                                                                              
Harish                                                                                                                         
Enter BookName:                                                                                                                
childstory                                                                                                                     
Enter Page:   
Enter Page:                                                                                                                    
456
Enter AuthorName:


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...