-->

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();
}

program to multiply two matrices of order mxn

// program to multiply two matrices of order 'mxn'
#include <stdio.h>
#include<conio.h>
void main()
{
    int A[3][3], B[3][3], C[3][3];
    int row, col, i, sum;

    /*
     * Reads elements in first matrix from user
     */
    printf("Enter elements in matrix A of size 2x2: \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   scanf("%d", &A[row][col]);
}
    }

    /*
     * Reads elements in second matrix from user
     */
    printf("\nEnter elements in matrix B of size 2x2: \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   scanf("%d", &B[row][col]);
}
    }

    /*
     * Multiplies both matrices A*B
     */
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   sum = 0;
   /*
    * Multiplies row of first matrix to column of second matrix
    * And stores the sum of product of elements in sum.
    */
   for(i=0; i<2; i++)
   {
sum += A[row][i] * B[i][col];
   }

   C[row][col] = sum;
}
    }

    /*
     * Prints the product of matrices
     */
    printf("\nProduct of Matrix A * B = \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   printf("%d ", C[row][col]);
}
printf("\n");
    }
getch();;
}

NEB26:program to reverse a number using function reverse()

//program to reverse a number using function reverse()
#include<stdio.h>
#include<conio.h>
void reverse();
void main()
{
prinf("the output is\n");
reverse();
getch();
}
void reverse()
{
int num;
int rem;
printf("enter a number\n");
scanf("%d",&num);
while(num!=0)
{
 rem=num%10;
    printf("%d",rem);
num=num/10;
}
------------------------
or simply we can also use strrev() function.



NEB24:explanation of string handling functions

Ans-
String--
           It means series of characters used in our program. We can also use digits with characters. They also act as string or a part of it. It is used when we input our name or address or somewhere their combination.

Syntax
char identifier[value];
or char identifier[value][value];

We can use he upper one if we want to input single string then. And if we want to input multiple values then we use lower one(called 2-dimensional array in string).

For example,
char name[100];
gets(name)
Here, the variable accepts value having maximum characters 100. We can also write ,
char name[10]="kathmandu";
IT stores the value in following way.

It stores values from cell location '0' and ends at null character called escape sequence. This is used to show that there is no more character beyond that. This null character is used with strings only. For  numbers, it is not used.
                        Similarly, if we want to store multiple string with same name then one-dimensional is not applicable. We use two dimensional string.
We declare:

char name[5][50];
It means, we are going to store any five names having optimum 50 characters.
Let's look below. If we have five names :RAM,SHYAM,Nita then it would like

so sequentially it stores data in different row. We use loop to execute it with different data.
Or, instead of inputting string, if we initialize strings then it can be written as
  char name[5][50]={"RAM","Shyam","Nitu"};

String handling functions:-There are some in-built functions,  we can say in-built small program. It is used to make our program easier and faster.
      Let's know about some.
1) strlen(string);-It is used to get length of given string.

 syntax:
identifer=strlen(string);
For example,
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
int k;
k=strlen(string);
printf("length=%d",k);
}
It returns 5. It also counts white spaces present in a string.
If we want want , we can also input string using scanf() or gets().

2)strrev():- It reverses the string given by us.

syntax:- strrev(string);
example:

#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
printf("reverseed string=%s",strrev(string));
}

We get output "elppa". Instead of putting in output line, we can put it before printf.

3)strlwr(): This function is used to convert given string into lower case.
syntax:-
strlwr(string);
Example;
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="APPLE";
printf("string in lowercase=%s",strlwr(string));
}

It gives us "apple" as an  output.

4)strupr():This string function is used to convert all characters of  string into upper case.

syntax:
strupr(string);

example:-
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
printf("string in uppercase=%s",strupr(string));
}

We get APPLE as an output after execution.

5)strcpy();- We use this function to copy one string to another. while copying, one replaces another.
Syntax:-
strcpy(string1,string2);
or strcpy(destination,source);

When we copy string, this is done from source to destination (string2 to string1). If first is empty then there would not be over-writing.

Example;
#include<stdio.h>
#include<string.h>
void main()
{
char string1[100]="APPLE";
char string2[100]="grapes";
printf("copied string =%s",strcpy(string1,string2));
}

output-
We get here string1=grapes. Because 'grapes' is copied to firs string named "APPLE". We can also write in reverse order.

Like these all, we have many other string handling functions like, strcat(),strchr(),strdup(),strset() etc.





NEB23;program to sort 'n' number in descending order

//program to sort 'n' elements in descending order
  #include<stdio.h>
  #include<conio.h>
  void main()
  {
  clrscr();
  int arr[200];    
  int size;     
  int i,j,k;      
 printf("now,enter size of elements \n");
  scanf("%d",&size); 
printf("enter array's elements\n");                 
  for(i=0;i<size;i++)
   {                            
     printf("enter elements for location=%d\n",i);
     scanf("%d",&arr[i]);      
   }
   printf("elements' sorting process started\n");
   for(i=0;i<size;i++)
   {
       for(j=i+1;j<size;j++)
        {
           if(arr[i]<arr[j])
             {
                 k=arr[i];
                 arr[i]=arr[j];
                arr[j]=k;
             }
       }
}
printf("in descending order,elements are\n");
for(i=0;i<size;i++)
   {                            
     printf("%d\n",arr[i]);      
   }
getch();
}

NEB23;program to sort 'n' number in ascending order

//program to sort 'n' elements in ascending order
  #include<stdio.h>
  #include<conio.h>
  void main()
  {
  clrscr();
  int arr[200];    
  int size;     
  int i,j,k;      
 printf("enter size of elements \n");
  scanf("%d",&size); 
printf("enter array's elements\n");                 
  for(i=0;i<size;i++)
   {                            
     printf("enter elements for location=%d\n",i);
     scanf("%d",&arr[i]);      
   }
   printf("elements' sorting process started\n");
   for(i=0;i<size;i++)
   {
       for(j=i+1;j<size;j++)
        {
           if(arr[i]>arr[j])
             {
                 k=arr[i];
                 arr[i]=arr[j];
                arr[j]=k;
             }
       }
}
printf("in ascending order,elements are\n");
for(i=0;i<size;i++)
   {                            
     printf("%d\n",arr[i]);      
   }
getch();
}

NEB23;program to sort any 'n' names

//program to sort 'n' elements 
  #include<stdio.h>
  #include<conio.h>
  void main()
  {
  clrscr();
  int arr[200][200],temp;    
  int size;     
  int i,j;      
 printf("enter size of string \n");
  scanf("%d",&size); 
printf("enter strings\n");                 
  for(i=0;i<size;i++)
   {                            
     printf("enter string for location=%d\n",i);
     scanf("%s",&arr[i]);      
   }
   printf("elements' sorting process started\n");
   for(i=0;i<size;i++)
   {
       for(j=i+1;j<size;j++)
        {
           if((strcmp(arr[i],arr[j]))>0)
             {
                 strcpy(temp,arr[i]);
                 strcpy(arr[i],arr[j]);
                strcpy(arr[j],temp);
             }
       }
}
printf("in ascending order,elements are\n");
for(i=0;i<size;i++)
   {                            
     printf("%s\n",arr[i]);      
   }
       
 printf("in descending order....\n");

for(i=0;i<size;i++)
   {
       for(j=i+1;j<size;j++)
        {
           if((strcmp(arr[i],arr[j]))<0)
             {
                 strcpy(temp,arr[i]);
                 strcpy(arr[i],arr[j]);
                strcpy(arr[j],temp);
             }
       }
}
printf("in descending order,elements are\n");
for(i=0;i<size;i++)
   {                            
     printf("%s\n",arr[i]);      
   }   
 getch();
}

Neb22:program to get difference of two matrices of order mxn

//program to get difference of two matrices of order mxn
#include<stdio.h>
#include<conio.h>
void main()
{
int matrics_1[100][100],matrics_2[100][100];
int i,j;
int total_row,total_col;
printf("enter total number of rows(m)\n");
scanf("'%d",&total_row);
printf("'enter total columns(n)\n");;
scanf("%d",&total__col);
printf("enter elements of first matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           scanf("%d",&matrics_1[i][j]);
        }
}
printf("now enter for second matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           scanf("%d",&matrics_2[i][j]);
        }
}
 printf("the summed matrix is\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           printf("%d",matrics_1[i][j]-matrics_2[i][j]);
        }
      printf(\n");
}
getch();

hseb21:;program to get sum of two matrices of order mxn

///program to get sum of two matrices of order mxn
#include<stdio.h>
#include<conio.h>
void main()
{
int matrics_1[100][100],matrics_2[100][100];
int i,j;
int total_row,total_col;
printf("enter total number of rows(m)\n");
scanf("'%d",&total_row);
printf("'enter total columns(n)\n");;
scanf("%d",&total__col);
printf("enter elements of first matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           scanf("%d",&matrics_1[i][j]);
        }
}
printf("now enter for second matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           scanf("%d",&matrics_2[i][j]);
        }
}
 printf("the summed matrix is\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           printf("%d",matrics_1[i][j]+matrics_2[i][j]);
        }
      printf(\n");
}
getch();
}



hseb20:program to get maximum and minimum value

//program to get maximum and minimum value
#include<stdio.h>
#include<conio.h>
void main()
{
int a[200],size,i,max,min;
printf("enter value of size to be stored in array\n");
scanf("%d",&size);
for(i=0;i<size;i++)
{
   printf("enter value for location=%d",i);
   scanf("%d",&a[i]);
}
max=a[2];
min=a[1];
for(i=0;i<size;i++)
{
   if(max<a[i])
     {
       max=a[i];
     }
 else if (min>a[i])
   {
     min=a[i];
    }
}
printf("the maximum value=%d\n",max);
printf("the minimum value=%d\n",min);
getch();
}


hseb19 solution:program to do different operation using switch

//program to carry out different operation  using switch.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int choice;
int length_rect,breadth_rect;
char string[200];
printf("'we have following menu\n");
printf( "1.to get length of string\n");
printf("2. to get area of rectangle\n");
printf("3 to reverse a string\n");
printf("4 exit");
printf("enter your choice 1 or 2 or 3 or 4\n");
scanf("%d",&choice);
 switch(choice)
          {
                  case 1:
                            printf("we are going to get length of string\n");
                            printf("enter string\n");
                            gets(string);
                           printf("the length=%d",strlen(string));
                 break;
               case 2:
                            printf("we are going to get area of rectangle\n");
                            printf("enter its length and breadth\n");
                            scanf("%d%d",&length_rect,&breadth_rect);
                            printf("the area=%d",length_rect*breadth_rect);
                 break;

               case 3:
                            printf("we are going to reverse the string\n");
                            printf("enter your string\n");
                            gets(string);
                           printf("the reverse=%s",strrev(string));
                 break;
             case 4:
                            printf("we are going to exit\n");
                 break;

              default:
                            printf("sorry, not a valid input\n");
                            printf("thank u, terminating....\n");
          }
getch();
}

hseb sol17:program to get all even numbers lying between 1 and 100

//program  to print/display all even numbers lying between 1 and 100.
#include<stdio.h>
#include<conio.h>
void main()
{
int k,n;
printf("enter value of 'n'\n");
scanf("%d",&n);
for(k=1;k<=n;k++)
   {
     if(k%2==0)
      {      printf("%d,",k);
      }
  }
getch();
}

hseb16 sol:program to get given star pattern

/*program to get following pattern
      * * * * *
      * * * *
      * * *
      * *
      * 
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
  {
     for(j=1;j<=i;j++)
          {
                printf("* ");
          }
       printf("\n");
 }
getch();
}

hseb_12_sol:Program to know a number is Armstrong or not

/*program to check a number is Armstrong or not .
we are taking number with three digits*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,k,rem,sum=0;
printf("enter a three digit number to be checked\n");
scanf("%d",&i);
k=i;
while(i!=0)
{
     rem=i%10;
    sum=sum+pow(rem,3);
    i=i/10;
  }
 if(sum==k)
 {
       printf("%d is Armstrong\n",k);

 }
else
{
printf("%d is not Armstrong\n",k);;}
getch();
}