Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Saturday, January 31, 2015

PROGRAM FOR SIZE OF UNION

PROGRAM:

#include<stdio.h> union book

{

int price;

char bname[20];

};

main()

{

union book b;


printf("Enter the Book Details:\n");
printf("Enter the Book Name:\n"); scanf("%s",&b.bname); printf("Enter the Book Price:\n"); scanf("%d",&b.price); printf("BOOK DETAILS:\n"); printf("%s\t%d\n",b.bname,b.price); printf("Enter the Book Name:\n"); scanf("%s",b.bname);

printf("Book Name=%s\n",b.bname);
}

OUTPUT:

Enter the Book Details: Enter the Book Name: English

Enter the Book Price: 150

BOOK DETAILS:
รป                150

Enter the Book Name:

English
Book Name=English

Friday, January 30, 2015

ARRAY OF STRUCTURES

PROGRAM:

#include<stdio.h> struct student

{

int rno,m1,m2,m3; float avg;

char name[20],dept[10];

};


void find_student(int a,struct student s[],int n)
{
int i;

printf("The Student Detail of %d\n",a); for(i=0;i<n;i++)

{
if(s[i].rno==a)

{



printf("%s\t%s\t%d\t%d\t%d\t%f\n",s[i].name,s[i].dept,s[i].m1,s[i].m

2,s[i].m3,s[i].avg); break;

}

} } main()

{

int i,n,rno; struct student s[10];

printf("Enter total no. of Students\n"); scanf("%d",&n);

for(i=0;i<n;i++)
{

printf("Enter the Student %d Details:\n",(i+1)); printf("Enter the roll no:\n"); scanf("%d",&s[i].rno);

printf("Enter the Name:\n"); scanf("%s",&s[i].name); printf("Enter the Dept:\n"); scanf("%s",&s[i].dept); printf("Enter the 3 marks:\n");

scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);

s[i].avg=(s[i].m1+s[i].m2+s[i].m3)/3;

}

printf("Enter the rollno to find:\n"); scanf("%d",&rno); find_student(rno,s,n);
}

 OUTPUT:

Enter total no. of Students 2

Enter the Student 1 Details: Enter the roll no:

12

Enter the Name: Kumar

Enter the Dept: cse

Enter the 3 marks: 45 67 88



Enter the Student 2 Details: Enter the roll no:

13 Enter the Name:

Prabhu

Enter the Dept: cse

Enter the 3 marks: 77 89 67



Enter the rollno to find: 13

The Student Detail of 13
Prabhu cse   77         89       67       77.000000





Thursday, January 29, 2015

PROGRAM USING STRUCTURES AND UNIONS

PROGRAM:

#include<stdio.h> struct student

{

int rno,m1,m2,m3; float avg;

char name[20],dept[10];
};

main()
{

struct student s;

printf("Enter the Student Details:\n"); printf("Enter the Stuent roll no:\n");

scanf("%d",&s.rno);

printf("Enter the Stuent Name:\n"); scanf("%s",&s.name); printf("Enter the Stuent Dept:\n"); scanf("%s",&s.dept);

printf("Enter the 3 marks:\n"); scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
s.avg=(s.m1+s.m2+s.m3)/3;
printf("The Student Average is :%f\n",s.avg);

}


OUTPUT:

Enter the Student Details: Enter the Stuent roll no: 12

Enter the Stuent Name: Kumar

Enter the Stuent Dept: CSE

Enter the Stuent marks: 40 18 90


The Student Average is :49.000000

Wednesday, January 28, 2015

SUM OF DIGITS


PROGRAM:

#include<stdio.h> int sum(int n,int s)

{

if(n<10) return n;

else

return (n%10)+sum(n/10,s);

}

main()
{

int n,s=0;


printf("\nEnter a Number\n");
scanf("%d",&n);
s=sum(n,s);

printf("\nThe sum of digits %d is %d\n",n,sum(n,s));
}



OUTPUT:

Enter a Number 46612

The sum of digits 46612 is 19

Tuesday, January 27, 2015

PROGRAM USING RECURSIVE FUNCTION

PROGRAM:

#include<stdio.h> int factorial(int n)
{

if(n==0 || n==1) return 1;

else

return n*factorial(n-1);

}

main()

{

int n;

printf("\nEnter a Number\n"); scanf("%d",&n);

printf("\nThe factorial of %d is %d\n",n,factorial(n)

}

OUTPUT:

Enter a Number 6

The factorial of 6 is 720

Monday, January 26, 2015

FUNCTIONS WITH ARGUMENTS & RETURN TYPE

#include<stdio.h> int small(int a[],int n)

{

int s,i; s=a[0];


for(i=0;i<n;i++)
{
if(a[i]<s)

s=a[i];
}

return s;
}

main()
{

int i,a[10],n,s;

printf("Enter total no. of elements\n"); scanf("%d",&n);

printf("Enter Array Elements one by one\n"); for(i=0;i<n;i++)

scanf("%d",&a[i]); printf("Array Elemets:\n"); for(i=0;i<n;i++)

printf("%d\t",a[i]);

printf("\n"); s=small(a,n);

printf("The Smallest element of given array is %d",s); }

OUTPUT:

Enter total no. of elements 5

Enter Array Elements one by one 1 98 2 66 0

Array Elemets:

1        98       2        66       0

The Smallest element of given array is 0

Sunday, January 25, 2015

FUNCTIONS WITH ARGUMENTS & WITHOUT RETURN TYPE

PROGRAM:

#include<stdio.h>


void sorting(int a[],int n)
{

int i,j,t; for(i=0;i<n-1;i++)

{

for(j=i+1;j<n;j++)
{

if(a[i]>a[j])
{

t=a[i];
a[i]=a[j];

a[j]=t;
}
}

}

printf("Array Elemets before sorting\n"); for(i=0;i<n;i++)

printf("%d\t",a[i]);



main()

{



int i,a[10],n;

printf("Enter total no. of elements\n"); scanf("%d",&n);

printf("Enter Array Elements one by one\n"); for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Array Elemets before sorting\n"); for(i=0;i<n;i++)

printf("%d\t",a[i]);

printf("\n");


sorting(a,n);
}

OUTPUT:

Enter total no. of elements 6

Enter Array Elements one by one 21 2


45





30





11





Array Elemets before sorting

21
2
9
45
30
11
Array Elemets before sorting

2
9
11
21
30
45