0% found this document useful (0 votes)
25 views

Program To Display The Student Marks Details Using Structure

The document contains C code to demonstrate various programs using data structures and functions: 1. A program to store and display student marks details using a structure. It defines a structure with student details, takes input for multiple students and calculates their average and grade. 2. Programs to calculate nCr and nPr values using a function. It defines a factorial function and calls it to calculate combinations and permutations. 3. A program to sort names in alphabetical order using string manipulation functions like strcmp() and strcpy(). It takes names as input and sorts them using a string comparison function.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Program To Display The Student Marks Details Using Structure

The document contains C code to demonstrate various programs using data structures and functions: 1. A program to store and display student marks details using a structure. It defines a structure with student details, takes input for multiple students and calculates their average and grade. 2. Programs to calculate nCr and nPr values using a function. It defines a factorial function and calls it to calculate combinations and permutations. 3. A program to sort names in alphabetical order using string manipulation functions like strcmp() and strcpy(). It takes names as input and sorts them using a string comparison function.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

PROGRAM TO DISPLAY THE STUDENT MARKS DETAILS USING STRUCTURE

#include<stdio.h>
#include<conio.h>
struct stu
{
int rno;
char name[25];
int marks[6];
float avg;
char grade;
}it[10];
void main()
{
int n,i,j,sum;
float a;
clrscr();
printf("Enter no. of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=0;
printf("Student %d record \n",i+1);
printf("Enter students rno\n");
scanf("%d",&it[i].rno);
printf("Enter students name\n");
scanf("%s",it[i].name);
printf("Enter 6 sub. marks\n");
for(j=0;j<6;j++)
{
scanf("%d",&it[i].marks[j]);
sum=sum+it[i].marks[j];
}
it[i].avg=sum/6.0;
a=it[i].avg;
if(a>=75)
it[i].grade='A';
else if((a>=60) && (a<75))
it[i].grade='B';
else if((a>=50) && (a <60))
it[i].grade='C';
else
it[i].grade='D';
}
printf("Student Records\n");

printf("Rno Sname Avg Grade\n");


for(i=0;i<n;i++)
printf("%d\t%s\t%4.1f\t%c\n",it[i].rno,it[i].name,it[i].avg,it[i].grade);
getch();
}

SAMPLE INPUT & OUTPUT


Enter no. of students
2
Student 1 record
Enter students rno
101
Enter students name
subha
Enter 6 sub. marks
67
78
67
56
90
98
Student 2 record
Enter students rno
102
Enter students name
lohith
Enter 6 sub. marks
89
78
56
45
68
78
Student Records
Rno Sname Avg Grade
101 subha 76.0 A
102 lohith 69.0 B

PROGRAM TO CALCULATE nCr & nPr VALUE USING FUNCTION


#include<stdio.h>
#include<conio.h>
void main()
{
long int n,r,ncr,npr;
int fact(int k); // function prototype (or) function declaration
clrscr();
printf("\n\n\t ncr and npr Calculation\n");
printf("\t--------------------------------\n");
printf("\t Enter the value of n and r \n\t");
scanf("%ld %ld",&n,&r);
ncr=(fact(n)) / (fact(r) * fact(n-r)); // calling the function fact()
npr=ncr * fact(r);
printf("\n\n\t ncr value=%ld\n",ncr);
printf("\n\t npr value=%ld\n",npr);
getch();
}
int fact(int k)
// Function definition of fact()
{
int i,fact=1;
for(i=1;i<=k;i++)
fact=fact*i;
return fact; //returning the fact value to the calling function main()
}

SAMPLE INPUT & OUTPUT

ncr and npr Calculation


-------------------------------Enter the value of n and r
53
ncr value=10
npr value=60

PROGRAM TO DISPLAY THE NAMES IN ALPHABETICAL ORDER USING STRING


FUNCTION
#include<stdio.h>
#include<conio.h>
#include<string.h> //for strcmp(),strcpy()
#define MAX 6 //Symbolic constant definition
void main()
{
char names[MAX][10],temp[20];
int i,j;
clrscr();
printf("\n\n\t Arrange the names in Alphabetical order\n");
printf("\t------------------------------\n");
printf("\t Enter the names one by one\t\n");
for(i=0;i<MAX;i++)
gets(names[i]); //input statement for reading string
for(i=0;i<MAX;i++)
for(j=i+1;j<MAX;j++)
/*strcmp() function will return 0 if two strings are equal, -1 if 1st string is smaller than 2nd and 1 if 1st
string is greater than 2nd*/
if(strcmp(names[i],names[j])>0)
{
strcpy(temp,names[i]); // This function copies one string into another string.
strcpy(names[i],names[j]);
strcpy(names[j],temp);
}
printf("\n\n\t Names in Alphabetical order\n");
printf("\n\t -----------------------------\n");
for(i=0;i<MAX;i++)
puts(names[i]); // output statement for displaying string
getch();
}

SAMPLE INPUT & OUTPUT


Arrange the names in Alphabetical order
-------------------------------------------------Enter the names one by one
Kalai
Subha
Hari
Usha
Doss
Names in Alphabetical order
-----------------------------------Doss
Hari
Kalai
Subha
Usha

PROGRAM TO PERFORM MATRIX OPERATIONS USING ARRAYS


#include<stdio.h>
#include<conio.h>
void main()
{
int m1[5][5],m2[5][5],r[5][5],r1,r2,c1,c2,choice,i,j,k;
clrscr();
printf("\n\t MATRIX MANIPULATIONS\n");
printf("\t----------------");
printf("\n 1. Matrix Addition\n");
printf("\n 2. Matrix Subtraction\n");
printf("\n 3. Matrix Multiplication\n");
printf("\n 4. Transpose\n");
printf("\n\t Enter the choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the order of the matrix\n");
scanf("%d %d",&r1,&c1);
printf("\n Enter the Elements of the matrix m1");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
printf("\n Enter the Elements of the matrix m2");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m2[i][j]);
printf("\n Resultant Matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
r[i][j]=m1[i][j]+m2[i][j];
printf("%d\t",r[i][j]);
}//inner for
printf("\n");
}//outer for
break;
case 2:
printf("\n Enter the order of the matrix\n");
scanf("%d %d",&r1,&c1);
printf("\n Enter the Elements of the matrix m1");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)

scanf("%d",&m1[i][j]);
printf("\n Enter the Elements of the matrix m2");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m2[i][j]);
printf("\n Resultant Matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
r[i][j]=m1[i][j]-m2[i][j];
printf("%d\t",r[i][j]);
}//inner for
printf("\n");
} //outer for
break;
case 3:
printf("\n Enter the order of the matrix m1\n");
scanf("%d %d",&r1,&c1);
printf("\n Enter the Elements of the matrix m2");
scanf("%d %d",&r2,&c2);
if(c1==r2)
{
printf("\n Enter the elements of the Matrix m1");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
printf("\n Enter the Elements of the matrix B");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
printf("\n Resultant Matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
r[i][j]=0;
for(k=0;k<r2;k++)
{
r[i][j]=r[i][j]+m1[i][k]*m2[k][j];
}
printf("%d\t",r[i][j]);
}
printf("\n");
} //for
}//if

else
printf("\n Multiplication is not possible");
break;
case 4:
printf("\n Enter the order of the matrix\n");
scanf("%d %d",&r1,&c1);
printf("\n Enter the Elements of the matrix ");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
printf("\n Transpose of the matrix is\n\n");
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
{
printf("%d\t",m1[j][i]);
}
printf("\n");
}
break;
default:
printf("Invalid choice\n");
}//switch
getch();
}

SAMPLE INPUT & OUTPUT


MATRIX MANIPULATIONS
-------------------------------------1. Matrix Addition
2. Matrix Subtraction
3. Matrix Multiplication
4. Transpose

Enter the choice: 1


Enter the order of the matrix
22
Enter the Elements of the matrix m1
2211
Enter the Elements of the matrix m2
2222
Resultant Matrix
4
4
3
3

Enter the choice :2


Enter the order of the matrix
22
Enter the Elements of the matrix m1
4444
Enter the Elements of the matrix m2
2222
Resultant Matrix
2
2
2
2

Enter the choice :3


Enter the order of the matrix m1
22
Enter the Elements of the matrix m2
22
Enter the elements of the Matrix m1
2222
Enter the Elements of the matrix B
2222
Resultant Matrix
8
8
8
8

Enter the choice :4


Enter the order of the matrix
22
Enter the Elements of the matrix
2345
Transpose of the matrix is
2
3

4
5

ROOTS OF A QUADRATIC EQUATION


#include<stdio.h>
#include<conio.h>
#include<math.h> //for sqrt()
void main()
{
int choice;
float a,b,c,r1,r2,d;
clrscr();
printf("\n\n\t ROOTS OF A QUADRATIC EQUATION");
printf("\n\t -----------------------------------");
printf("\n\n\t Enter the coefficients of a, b & c\n");
scanf("%f %f %f",&a,&b,&c);
if(a!=0)
{
d=(b*b)-(4*a*c);
printf("\n Discriminant value = %5.2f\n",d);
if (d > 0)
choice = 1;
else if (d==0)
choice =2;
else
choice=3;
switch(choice)
{
case 1:
printf("\n\t The Roots are Real and Distinct\n");
r1=( -b + sqrt(d)) / (2.0 * a);
r2=( -b - sqrt(d)) / (2.0 * a);
printf("\n\t Root1 = %5.2f \t Root2 = %5.2f\n",r1,r2);
break;
case 2:
printf("\n\t The Roots are Real and Equal\n");
r1=( -b ) / (2.0 * a);
printf("\n\t Root1 = Root2 = %5.2f\n",r1);
break;
case 3:
printf("\n\t The Roots are Real and Distinct\n");
r1=( -b ) / (2.0 * a);
r2=sqrt(-d) / (2.0 * a);
printf("\n\t Root1 = %5.2f + i %5.2f\n",r1,r2);
printf("\n\t Root1 = %5.2f - i %5.2f\n",r1,r2);
break;
default:
printf("\n\t Invalid choice\n");

} // switch
}//if
else
printf("\n\t The equation is linear");
getch();
} //main()

SAMPLE INPUT & OUTPUT


ROOTS OF A QUADRATIC EQUATION
----------------------------------Enter the coefficients of a, b & c
145
Discriminant value = -4.00
The Roots are Real and Distinct
Root1 = -2.00 + i 1.00
Root1 = -2.00 - i 1.00

PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS PALNINDROME OR NOT


#include<stdio.h>
#include<conio.h>
void main()
{
int n,q,r,rev=0;
clrscr();
printf("\n\n\t PALINDROME CHECKING\n");
printf("\t------------------------\n");
printf("\n\t Enter an integer number \n");
scanf("%d",&n);
q=n;
while(n>0)
{
r=n%10;
rev=rev * 10 + r;
n=n/10;
}//while
if(q==rev)
printf("\n\n\t Given Number is Palindrome\n");
else
printf("\n\t Given number is not a Palindrome\n");
getch();
}//main()

SAMPLE INPUT & OUTPUT


PALINDROME CHECKING
-------------------------------------Enter an integer number
212
Given Number is Palindrome
Enter an integer number
243
Given number is not a Palindrome

PROGRAM TO FIND THE SUM AND FACTORIAL OF A GIVEN NUMBER


#include<stdio.h>
#include<conio.h>
void main()
{
long int n,i,sum=0,fact=1;
clrscr();
printf("\n\n\t Sum and Factorial Calculation\n");
printf("\t--------------------------------\n");
printf("\t Enter the limit \n\t");
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
fact=fact*i;
} //for
printf("\n\n\t Sum of first %ld number is %ld\n",n,sum);
printf("\n\t Factorial of %ld is %ld\n",n,fact);
getch();
}

SAMPLE INPUT & OUTPUT


Sum and Factorial Calculation
-------------------------------Enter the limit
7
Sum of first 7 numbers is 28
Factorial of 7 is 5040

PROGRAM TO GENERATE FIBONACCI SERIES USING RECURSIVE FUNCTION


#include<stdio.h>
#include<conio.h>
void fibo(int,int,int); // Function declaration
long int n=0,x=-1,y=1; // global variable declaration
void main()
{
clrscr();
printf("\n\n\t Fibonacci series n\n");
printf("\t----------------------\n");
printf("\t Enter the limit \n\t");
scanf("%ld",&n);
printf("\n\n\t Fibonacci series is\n");
fibo(n,x,y); // Calling the function fibo()
getch();
}
void fibo(int n,int f,int f1)
// Function definition of fibo()
{
if(n==0)
exit(0);
printf("%d\t",f+f1);
fibo(--n,f1,f+f1);// recursive function calling
}

SAMPLE INPUT & OUTPUT


Fibonacci series
---------------------Enter the limit
6

Fibonacci series is
1
1
2
3

PROGRAM TO DISPLAY THE SIZE OF VARIOUS BASIC DATA TYPES


#include<stdio.h> // standardized input and output
#include<conio.h> /*This header file declares several useful library functions for performing
"console input and output" from a program. included for getch()*/
void main()
{
clrscr();
printf("\n\t SIZE OF VARIOUS BASIC DATA TYPES\n");
printf("\t ---------------------------------------------------");
printf("\n\t Size of int = %d bytes ",sizeof(int));
printf("\n\t Size of unsigned int = %d bytes ",sizeof(unsigned));
printf("\n\t Size of short int = %d bytes ",sizeof(short));
printf("\n\t Size of unsigned short int = %d bytes ",sizeof(unsigned short));
printf("\n\t Size of long int = %d bytes ",sizeof(long));
printf("\n\t Size of unsigned long int = %d bytes ",sizeof(unsigned long));
printf("\n\t Size of char = %d byte ",sizeof(char));
printf("\n\t Size of unsigned char = %d byte ",sizeof(unsigned char));
printf("\n\t Size of float = %d bytes ",sizeof(float));
printf("\n\t Size of double = %d bytes ",sizeof(double));
printf("\n\t Size of long double = %d bytes ",sizeof(long double));
getch();
}

SAMPLE INPUT & OUTPUT


SIZE OF VARIOUS BASIC DATA TYPES
--------------------------------------------------Size of int = 2 bytes
Size of unsigned int = 2 bytes
Size of short int = 2 bytes
Size of unsigned short int = 2 bytes
Size of long int = 4 bytes
Size of unsigned long int = 4 bytes
Size of char = 1 byte
Size of unsigned char = 1 byte
Size of float = 4 bytes
Size of double = 8 bytes
Size of long double = 10 bytes

PROGRAM TO EVALUATE THE EXPRESSION USING OPERATOR PRECEDENCE


AND ASSOCIATIVITY RULE
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,x,y,z,r;
clrscr();
printf("\n\t Evaluation of expressions\n");
printf("\n\t --------------------------\n");
printf("\n\t Enter the value for a,b and c \n");
scanf("\n\t %d %d %d", &a,&b,&c);
x = a - b / 3 + c * 2 - 1;
y = a - b / ( 3 + c ) * ( 2 - 1 );
z = a - ( b / ( 3 + c ) * 2 ) - 1;
r = ( ( a - b ) / c ) % ( x * ( y + z ) );
c -= ( ( a > 5 ) && ( a <= 20 ) ) ? --a : ( a * b );
a = b++ - ( c * 2 );
printf("\n\t Value of x = %d ", x);
printf("\n\t Value of y = %d ", y);
printf("\n\t Value of z = %d ", z);
printf("\n\t Value of r = %d ", r);
printf("\n\t Value of c = %d ", c);
printf("\n\t Value of a = %d ", a);
getch();
}

SAMPLE INPUT & OUTPUT


Evaluation of expressions
-------------------------------Enter the value for a,b and c
352
Value of x = 5
Value of y = 2
Value of z = 0
Value of r = -1
Value of c = -13
Value of a = 31

PROGRAM TO DISPLAY THE SIZE OF STRUCTURE AND UNION DATA TYPES


#include<stdio.h>
#include<conio.h>
union result
{
long int rno;
long int mark;
};
struct student
{
long int rno;
char name[15];
};
void main()
{
struct student ss={1001,"RISHI"}; //structure variable declaration with member initialization
union result ures;//union variable declaration
ures.rno=41901001; //initialization of union members
ures.mark=89;
clrscr();
printf("\n\n\n\t Structure Member details \n");
printf("\n\t--------------------------");
printf("\n\t Reg No : %ld , Name : %s",ss.rno,ss.name);
printf("\n\n\n\n\t Union Member details \n");
printf("\n\t-------------------------");
printf("\n\t Reg no : %ld , Mark : %ld",ures.rno,ures.mark);
printf("\n\n\n\n\n\t Size of Structure & Union data type\n");
printf("\n\t----------------------------------------");
printf("\n\n\n\t size of union data type is : %d bytes\n",sizeof(ures));
printf("\n\t size of struct data type is : %d bytes\n",sizeof(ss));
getch();
}

SAMPLE INPUT & OUTPUT


Structure Member details
-------------------------------Reg No : 1001 , Name : RISHI
Union Member details
----------------------------Reg no : 89 , Mark : 89
Size of Structure & Union data type
-------------------------------------------size of union data type is : 4 bytes
size of struct data type is : 19 bytes

SORTING ELEMENTS IN ASCENDING ORDER


#include<stdio.h>
#include<conio.h>
main()
{
int marks[25],i,j,temp,n;
clrscr();
printf("Enter no. of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter mark %d\n",i+1);
scanf("%d",&marks[i]);
}
/*Sorting process*/
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(marks[i]>marks[j])
{
temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
}
}
}
/*Printing sorted elements of marks array*/
printf("Marks sorted in ascending\n");
for(i=0;i<n;i++)
printf("%d\n",marks[i]);
getch();
}

SAMPLE INPUT & OUTPUT

Enter no. of elements


5
Enter mark 1
78
Enter mark 2
45
Enter mark 3
90
Enter mark 4
56
Enter mark 5
67
Marks sorted in ascending
45
56
67
78
90

PROGRAM TO FIND THE BIGGEST OF THREE NUMBERS


#include<stdio.h>

#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter three numbers=\n");
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
{
printf("%d is the biggest number\n",a);
}
else if(b>c)
{
printf("%d is the biggest number\n",b);
}
else
{
printf("%d is the biggest number\n",c);
}
getch();
}

SAMPLE INPUT & OUTPUT


Enter three numbers =
10
15
5
15 is the biggest number

PROGRAM TO CONVERT CELSIUS INTO FARENHEIT


#include<stdio.h>

#include<conio.h>
void main()
{ float f,c;
clrscr();
printf("\nEnter the temperature in celcius");
scanf("%f",&c);
f=c*1.8+32;
printf("\nThe fahrenheit value is %f",f);
getch();
}

SAMPLE INPUT & OUTPUT


Enter the temperature in celcius 35
The fahrenheit value is 95.000000

You might also like