prowessapps.in Structure and DMA

 C Program to Store Information (Name, ID and Salary) of a Employee Using Structure.

#include<stdio.h>
struct Employee{
 int id;
 char name[50];
 float salary;
};
int main( ) {
 struct Employee s;
 printf("Enter Info. of Emp :\n");
 printf("Enter the ID : ");
 scanf("%d",&s.id);
 printf("Enter Name : ");
 scanf("%s",s.name);
 printf("Enter salary : ");
 scanf("%f",&s.salary);
 printf("Display Info. : \n");
 printf("Name : %s\n",s.name);
 printf("Id : %d\n",s.id);
 printf("Sal : %f\n", s.salary);
 return 0;
}
OUTPUT :
Enter information of Emp : 

Enter the ID : 39
Enter name : xyz
Enter Salary : 30000
Display Info. : 
Name : xyz
Id : 39
Salary : 30000.0000

 C Program to add two complex numbers by using Structure.

#include<stdio.h>
typedef struct complex{
 float r;
 float i;
}complex;
complex add(complex n1,complex n2);
int main( ) {
 complex n1,n2,t;
 printf("For 1st complex no. : \n");
 printf("Enter real & imag. part:");
 scanf("%f%f",&n1.r,&n1.i);
 printf("For 2nd complex no. : ");
 printf("Enter real & imag. part:");
 scanf("%f%f",&n2.r,&n2.i);
 t=add(n1,n2);
 printf("Sum=%.1f+%.1fi",t.r,t.i);
 return 0;
}
complex add(complex n1,complex n2) {
 complex temp;
 temp.r=n1.r+n2.r;
 temp.i=n1.i+n2.i;
 return(temp);
}
OUTPUT :
For 1st complex number :
Enter real & imaginary respectively : 3 4.5
For 2nd complex number : 
Enter real & imaginary respectively : 3.2 5
Sum = 6.2 + 9.5i

 C Program to add Distance given in cm and meter.

#include<stdio.h>
struct Distance {
 int meter;
 float cm;
} d1,d2,sum;
int main( ) {
 printf("Enter Info of Distance1:");
 printf("Enter meter: ");
 scanf("%d",&d1.meter);
 printf("Enter cm: ");
 scanf("%f",&d1.cm);
 printf("Enter Info of Distance2:");
 printf("Enter meter : ");
 scanf("%d",&d2.meter);
 printf("Enter cm : ");
 scanf("%f",&d2.cm);
 sum.meter=d1.meter+d2.meter;
 sum.cm=d1.cm+d2.cm;
 if (sum.cm>100.0) {
  sum.cm=sum.cm-100.0;
  ++sum.meter;
 }
 printf("\nSum of distances = %dm ,%.1fcm",sum.meter,sum.cm);
 return 0;
}
OUTPUT :
Enter Info of Distance1 : 
Enter meter : 7
Enter cm : 85
Enter Info of Distence2 :
Enter meter : 10
Enter cm : 65
Sum of distances = 18m, 50cm

 C Program to Store Information of 10 Employees Using Structure.

#include<stdio.h>
struct Employee{
 char name[50];
 int id;
 float salary;
};
int main( ){
 struct Employee s[10];
 int i;
 printf("Enter Info of Emps : \n");
 for(i=0;i<10;++i)  {
  s[i].id=i+1;
  printf("\nFor id number %d\n",s[i].id);
  printf("Enter name: ");
  scanf("%s",s[i].name);
  printf("Enter salary: ");
  scanf("%f",&s[i].salary);
  printf("\n");
 }
 printf("Information of Emps:\n");
 for(i=0;i<10;++i) {
  printf("Info for id number %d:\n",i+1);
  printf("Name : ");
  puts(s[i].name);
  printf("salary : %.1f",s[i].salary);
 }
 return 0;
}
OUTPUT :
Enter Info of Emps : 
For id number 1
Enter Name : xyz
Enter Salary : 5000
For id number 2
Enter Name : abc
Enter Salary : 15000
.
.
Information of Emps :
Info for id number 1
Name : xyz
Salary : 5000
Name : abc
Salary : 15000
.
.

 C Program to Store record of Employee dynamically.

#include<stdio.h>
#include<stdlib.h>
struct Employee {
 int id;
 char name[30];
};
int main( ) {
 struct Employee *ptr;
 int i,n;
 printf("Enter Number of Employee : ");
 scanf("%d",&n);
 ptr=(struct Employee*)malloc(n*sizeof(struct Employee));
  for(i=0;i < n;++i) {
   printf("Enter Name and ID respectively:\n");
   scanf("%s%d",&(ptr+i)->name, &(ptr+i)->id);
  }
  printf("Displaying Information :\n");
  for(i=0;iname,(ptr+i)->id);
 return 0;
}
OUTPUT :
Enter Number of Employee : 2
Enter Name and ID respectively :
xyz 39
Enter Name and ID respectively :
abc 45
Displaying Information : 
xyz    39
abc    40

 C Program to calculate difference between given Time.

#include<stdio.h>
struct TIME{
 int s;
 int m;
 int h;
};
void Difference(struct TIME t1, struct TIME t2, struct TIME *diff);
int main( ){
 struct TIME t1,t2,diff;
 printf("Enter Start Time [hh:mm:ss]: ");
 scanf("%d:%d:%d",&t1.h,&t1.m,&t1.s);
 printf("Enter Stop Time [hh:mm:ss]: ");
 scanf("%d:%d:%d",&t2.h,&t2.m,&t2.s);
 Difference(t1,t2,&diff);
 printf("TIME DIFFERENCE: ");
 printf("%d:%d:%d\n",diff.h,diff.m,diff.s);
 return 0;
}
void Difference(struct TIME t1, struct TIME t2, struct TIME *differ){
 if(t2.s>t1.s){
  --t1.m;
  t1.s+=60;
  }
 differ->s=t1.s-t2.s;
 if(t2.m>t1.m){
  --t1.h;
  t1.m+=60;
  }
  differ->m=t1.m-t2.m;
  differ->h=t1.h-t2.h;
}
OUTPUT :
Enter Start Time [hh:mm:ss]:
2:23:46
Enter Stop Time [hh:mm:ss]:
1:13:46
TIME DIFFERENCE: 1:10:00 


 C Program to calculate sum of elements of array using malloc function.

#include<stdio.h> 
#include<stdlib.h>
int main(){
 int n,i,*ptr,sum=0;
 printf("Enter Number of Elements : ");
 scanf("%d",&n);
 ptr=(int*)malloc(n*sizeof(int));  
 if(ptr==NULL) {
  printf("Error! memory not allocated.");
  exit(0);
 }
  printf("Enter Elements of array : ");
  for(i=0; i < n; ++i) {
   scanf("%d",ptr+i);
   sum+=*(ptr+i);
  }
 printf("Sum = %d",sum);
 free(ptr);
 return 0;
}
OUTPUT :
Enter Number of Elements : 2
Enter Elements of array : 2 4
Sum = 6

 C Program to calculate sum of all elements of array using calloc function.

#include<stdio.h>
#include<stdlib.h>
int main( ) {
 int n,i,*ptr,sum=0;
 printf("Enter Number of Elements: ");
 scanf("%d",&n);
 ptr=(int*)calloc(n,sizeof(int));
 if(ptr==NULL) {
  printf("Error! memory not allocated.");
  exit(0);
 }
 printf("Enter Elements of array : ");
 for(i=0; i < n; ++i) {
 scanf("%d",ptr+i);
 sum+=*(ptr+i);
 }
 printf("Sum = %d",sum);
 free(ptr);
 return 0;
}
OUTPUT :
Enter the Number of Elements : 2
Enter Elements of array : 2 4
Sum = 6

 C Program to demonstrate the function realloc function.

#include<stdio.h>
#include<stdlib.h>
int main( ) {
 int *ptr,i,n1,n2;
 printf("Enter size of Array : ");
 scanf("%d",&n1);
 ptr=(int*)malloc(n1*sizeof(int));
 printf("Address of previously allocated memory : \n");
 for(i=0; i < n1; ++i)
  printf("%u\t",ptr+i);
 printf("\nEnter new size of array: ");
 scanf("%d",&n2);
 ptr=realloc(ptr,n2);
 printf("New Allocated memory :\n");
 for(i=0; i < n2;++i)
  printf("%u\t",ptr+i);
 return 0;
}
OUTPUT :
Enter size of Array : 5
Address of previously allocated memory :  
1002	1006	1010	1014	1018
Enter new size of array : 2
New Allocated memory :
1002	1006


Get it on Google Play


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved