-->

NEB28:program to sort records using struct

//program to sort records using struct from a to z
#include<stdio.h>
#include<conio.h>
struct detail
{
int roll;
char name[100];
char address[100];
}var[100],temp;

void main()
{
int i,j,n;
printf("enter total records (less or than '100')\n");
scanf("%d",&n);
printf("now enter records of students\n");
for(i=0;i<n;i++)
{
printf("enter student's roll no.\n");
scanf("%d",&var[i].roll);
printf("enter student's name\n");
scanf("%s",var[i].name);
printf("enter student's address\n");
scanf("%s",var[i].address);
}
printf("now sorting from a to z on the basis of  address\n");
for(i=0;i<n;i++)
 {
     for(j=i+1;j<n;j++)
       {
            if((strcmp(var[i].address,var[j].address))>0)
                  {
                      temp=var[i];
                      var[i]=var[j];
                     var[j]=temp;
                   }
         }
}
printf("sorted data (from a to z i.e. ascending order )on the basis of address are:\n:");
  for(i=0;i<n;i++)
{
printf("student's roll=%d, name=%s,student's address=%s \n",var[i].roll,var[i].name,var[i].address);
}
getch();
}
----------------------------------------------------------------------------------------------------------
//program to sort records using struct(in descending order i.e. z to a)
#include<stdio.h>
#include<conio.h>
struct detail
{
int roll;
char name[100];
char address[100];
}var[100],temp;

void main()
{
int i,j,n;
printf("enter total records (less or than '100')\n");
scanf("%d",&n);
printf("now enter records of students\n");
for(i=0;i<n;i++)
{
printf("enter student's roll no.\n");
scanf("%d",&var[i].roll);
printf("enter student's name\n");
scanf("%s",var[i].name);
printf("enter student's address\n");
scanf("%s",var[i].address);
}
printf("now sorted data (z to a) on the basis of address are:\n:);
for(i=0;i<n;i++)
 {
     for(j=i+1;j<n;j++)
       {
            if((strcmp(var[i].address,var[j].address))<0)
                  {
                      temp=var[i];
                      var[i]=var[j];
                     var[j]=temp;
                   }
         }
}
  for(i=0;i<n;i++)
{
printf("student's roll=%d, name=%s,student's address=%s \n",var[i].roll,var[i].name,var[i].address);
}
getch();
}
--------------------------------------------------------------------------------
note:
Here I have done both, from 'a' to 'z' and 'z' to 'a'. You can do only one, either 'a' to 'z' or 'z' to 'a'. 

NEB30:Difference between struct and union

Ans:-
The differences between struct and union are given below in tabular form.
                   
struct
Union
1.It stores dis-similar data.Each gets separate space in memory.They do not share memory
1. It stores dis-similar type of data.Total memory space is equal to the member with largest size.They share memory space.
2.we can access member in any sequence .
2.We can access that whose value is recently used.
3.It uses keyword 'struct'
3.It uses keyword 'union'
4.We can initialize values of member/s and in any order
4.We can initialize that member which is first declared.
5.It consumes more memory
5.It consumes less memory.
6.Its syntax
    struct tag
{
datatype member1;
datatype member2;
datatype member3;
}variable;

6.Its syntax
    union tag
{
datatype member1;
datatype member2;
datatype member3;
}variable;

Example
struct student
{
int roll;
char sex;
float percentage;
}var;

Example
union student
{
int roll;
char sex;
float percentage;
}var;


NEB27:program to get factorial value of a number using recursion

//program to get factorial value of a number using recursion
#include <stdio.h>
#include <stdlib.h>
int recur(int n);
int main()
{
 int n;
 printf("ente a number\n");
 scanf("%d",&n);
printf("the factorial value is\n");
printf("%d",recur(n));
getch();
return 0;
}
int recur(int n)
{
 if(n<=0)
  {
          return 1;
}
else
{
    return n*recur(n-1);
}

}

NEB29:Difference between array and struct

Ans:-
Differences between array and struct are given below.                                                                                                                                                
Arraystruct
1.It stores same type of data1. It stores dis-similar type of data.
2.It uses static memory allocation . 2.I uses dynamic memory allocation.
3.It takes less time to access elements.3.It takes more time to access elements.
4.It uses index value to access elements. 4.It takes help of period operator(.) to access elements.
5.It is derived data type. 5. IT is user's defined data type.
6.We can not define array of array.          6.We can define array of structure.
7.Its syntax is                                          
data type identifier[value];








example,int a[20];
7.Its syntax is
struct tag
{datatype member1;
datatype member2;
data type member3;
}variable;
example,
struct det
{char name[100];
char add[40];
int  roll;
}variable;



program to know a matrix is identity or not

// program to know a matrix is identity or not
//to know a matrix is identity or not
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],order,i,j,first=0,second=0;
printf("enter order\n");
scanf("%d",&order);
printf("enter matrix elements\n");
for(i=0;i<order;i++)
  {
    for(j=0;j<order;j++)
      {
       scanf("%d",&a[i][j]);
      }
  }
printf("the matrix is\n");
for(i=0;i<order;i++)
  {
    for(j=0;j<order;j++)
      {
       printf("%d",a[i][j]);
      }
    printf("\n");

  }
 for(i=0;i<order;i++)
  {
    for(j=0;j<order;j++)
      {
       if(i==j)
       {
first=first+1;
}
else
{
second=second+a[i][j];
}

  }
  }

  if(first==order && second==0)
  {
  printf("it is identity matrix\n");
  }
  else
  {
  printf("it is not");
  }

  getch();
  }

program to understand strcmp(), libraray function

//program to understand strcmp(), libraray function
//strcmp() function
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char first[50],sec[50];
int k;
gets(first);
gets(sec);
k=strcmp(first,sec);
printf("k=%d",k);
if(k==0)
  {
    printf("both are same");
    }
else if(k>0)
  {
  printf("first string =%s has more value\n",first);
  }
  else if(k<0)
  {
  printf("second string =%s has more value\n",sec);
  }
  getch();
  }

program to get hcf and lcm of two numbers

//program to get hcf and lcm of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,n,n1,lcm,hcf;
printf("enter numbrs\n");
scanf("%d%d",&a,&b);
if(a>b)
{
n=a;
}
else
{
n=b;
}
for(n1=n;n1>=1;n1--)
    {
    if(a%n1==0 && b%n1==0)
     {
     hcf=n1;
     break;
     }
}
printf("hcf=%d",hcf);
printf("lcm=%d",(a*b)/hcf);
getch();
}