c structure programs

      Comments Off on c structure programs

c structure programs

c structure programs

c structure programs is collection of structure examples in c language. Basically when you wish to handle multiple data together then you need to use structure in c language. In C++ and other languages we have concepts of class and object where we can add multiple data members and member functions, In c language we can add structure when we want to deal with multiple data at once.

c structure programs example 1

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in/
// C Structure Program
#include <stdio.h>
#include <conio.h>

struct val			//structure define
{
	char name[20];
	float b,d;
};

void main()
{
	struct val c;	//structure variable
	clrscr();

	printf("Enter the Name : ");
	gets(c.name);

	printf("Enter Float 1 :");
	scanf("%f",&c.b);

	printf("Enter Float 2 :");
	scanf("%f",&c.d);

	printf("\n the Answer is : %-10s %.2f %.2f",c.name,c.b,c.d);

	getch();
}

 

c structure programs example 2

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in/
// C Structure Program

#include <stdio.h>
#include <conio.h>
#include <string.h>

struct student			//structure define
{
	int stud_no;
	char stud_name[20];
	float int_mark,ext_mark;
};

void main()
{
	struct student std1={100,"Rambhai",30.50,60.25},std2,std3;
	clrscr();

	std3.stud_no=std1.stud_no;

	strcpy(std3.stud_name, std1.stud_name);

	printf("\n Only Student No & Name are printed to structure student : ");
	printf("\n %6d %20s",std3.stud_no,std3.stud_name);

	std2=std1;

	printf("\n\nStudent 1 & student 2 are same :");
	printf("\n%5d %20s %8.2f %8.2f",std2.stud_no,std2.stud_name,std2.int_mark,std2.ext_mark);

	printf("\n\nThe size of the name is : %d",strlen(std3.stud_name));

	if(strcmp(std2.stud_name,std1.stud_name)==0)
		printf("\nBoth Name are same ");

	getch();
}

 

c structure programs example 3

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in/
// C Structure Program
#include <stdio.h>
#include <conio.h>

struct marks			//structure define
{
	int mark1,mark2,total;
	float per;
};

void main()
{
	clrscr();
	int i,n;
	struct marks b[20];

	printf("How many structure of variable require ? ");
	scanf("%d",&n);

	for(i=0;i<n;i++)
	{
		printf("Enter Mark 1 of Student[%d] :",i);
		scanf("%d",&b[i].mark1);

		printf("Enter Mark 2 of Student[%d] :",i);
		scanf("%d",&b[i].mark2);

		b[i].total=b[i].mark1+b[i].mark2;
		b[i].per=b[i].total/2.0;
	}

	printf("Student         Mark1     Mark2    Total");
	for(i=0;i<n;i++)
	{
		printf("\nStudent[%d] ->  %3d  %3d  = %3d",i,b[i].mark1,b[i].mark2,b[i].total);
		printf("\nPercentage is : %.2f",b[i].per);
	}
	getch();
}

 

c structure programs example 4

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in/
// C Structure Programs

#include <stdio.h>
#include <conio.h>

struct book
{
	char name[20];
	int amount;
	float discount,total;
};

void main()
{
	clrscr();
	struct book b[2],c;
	int i;
	struct book display(struct book);

	for(i=0;i<2;i++)
	{
		printf("Enter the Book Name :");
		fflush(stdin);
		gets(b[i].name);

		printf("Enter the Book Amount :");
		scanf("%d",&b[i].amount);
	}

	printf("\n\tName\t Amt     Dis   :   Total");
	for(i=0;i<2;i++)
	{
		c=display(b[i]);
		printf("\n %10s %8d %10.2f %10.2f",c.name,c.amount,c.discount,c.total);
	}
	getch();

}

struct book display(struct book z)
{
	z.discount = z.amount * 0.1;
	z.total = z.amount - z.discount;
	return(z);
}

 

c structure programs example 5

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in/
// C Structure Program

#include <stdio.h>
#include <conio.h>
struct rate
{
	char name[20];
	int basic;
	float da,hra;
};

void main()
{
	clrscr();
	struct rate b={"Rambhai",7000,40.5,10.5};
	float d_incre,h_incre,value;
	struct rate update1(struct rate,float,float);
	void display(struct rate);
	float mul(struct rate);

	printf("The actual values are as follows ");
	printf("%15s %6d %8.2f %8.2f \n",b.name,b.basic,b.da,b.hra);

	printf("Enter encyment value of DA :");
	scanf("%f",&d_incre);

	printf("Enter encyment value of HRA :");
	scanf("%f",&h_incre);

	b=update1(b,d_incre,h_incre);
	display(b);
	value = mul(b);
	printf("total salary = %.2f",value);

}

struct rate update1(struct rate cal, float p, float q)
{
	cal.da += p;
	cal.hra += q;
	return(cal);
}

void display(struct rate cal)
{
	printf("update values are as follows ");
	printf("\n Name  HRA DA \n");
	printf("%15s %8.2f %8.2f",cal.name,cal.da,cal.hra);
}

float mul(struct rate cal)
{
	return(cal.basic + (cal.basic*cal.da)/100.00 + (cal.basic * cal.hra)/100.00);
}

 

c structure programs example 6

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in/
// C Structure Program

#include <stdio.h>
#include <conio.h>
#include <string.h>

struct list
{
	int rollno;
	char name[20];
};

void main()
{
	struct list arr[30];	//structure delcare
	void struct_sort(struct list[],int);	//struct variable declare
	int i,n;
	clrscr();

	printf("How many students are : ");
	scanf("%d",&n);

	for(i=0;i<n;i++)
	{
		flushall();
		printf("Enter Roll No : ");
		scanf("%d",&arr[i].rollno);

		flushall();
		printf("\nEnter Name : ");
		gets(arr[i].name);

	}

	struct_sort(arr,n);

	for(i=0;i<n;i++)
	{
		printf("%6d %10s \n",arr[i].rollno,arr[i].name);
	}

	getch();

}

void struct_sort(struct list a[],int m)
{
	struct list temp;
	int i,j;

	for(i=0;i<m;i++)
	{
		for(j=i+1;j<m;j++)
		{
			if(strcmp(a[i].name,a[j].name)>0)
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}

		}

	}

}

 

c structure programs example : Download

[sociallocker]Download[/sociallocker]