ex.no : 8 finding the largest and smallest number · pdf fileex.no : 8 finding the largest and...
TRANSCRIPT
EX.NO : 8 FINDING THE LARGEST AND SMALLEST NUMBER
DATE : USING ARRAYS
Aim
To write a program to find the largest and smallest number using Array.
Algorithm
1. Read the number of values (n).
2. Initialize the iterative variable i=0.
3. Repeat the steps 4 & 5 until no. of values minus 1(n-1).
4. Read the elements in an array.
5. Increment the iterative variable.
6. Assign the first element of an array to the small and big variables.
7. Repeat the steps 8 to 10, until no. of values minus 1 (n-1).
8. If the array element is greater than big element,
Assign the element to the big variable.
Otherwise
goto step 10.
9. If the array element is smaller than small element,
Assign the element to the small variable.
Otherwise
goto step 10.
10. Increment the iterative variable.
11. Display the biggest element in the list.
12. Display the smallest element in the list.
Flowchart
start
Read number of elements (n)
Repeat process until n-1
Read the elements in an
array a[i]
Next i
Assign the first element to the small
and big variables
Repeat process until n-1
Check the
Array
element>big
N Check the N
Array
element<smal
Y Y
Assign the element to the big
variable
Assign the element to the small variable
Next i
Display the values of big,small variables
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10],big,small;
clrscr();
printf(“Enter the size of the list \n”);
scanf(“%d”,&n);
printf(“Enter the elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
small=a[1];
big=a[1];
for(i=1;i<n;i++)
{
if(a[i] >big)
big =a[i];
else if (a[i] <small)
small =a[i];
}
printf(“\n Biggest element in the list:%d\n”,big);
printf(“\n smallest element in the list:%d \n”,small);
getch( );
}
Sample Input and Output:
INPUT: ENTER THE SIZE OF THE LIST
5
Enter the elements
65
45
12
85
68
OUTPUT:
Biggest element in the list : 85
Smallest element in the list : 12
RESULT:
Thus the program for finding largest and smallest number using array is implemented.
EX.NO : 15
DATE :
SORTING THE NUMBERS IN AN ARRAY
Aim order.
To write a C program to sort the numbers in an array in Ascending and Descending
Algorithm
1. Read the number of values (n).
2. Initialize the iterative value (i) to 0.
3. Repeat the steps 4 to 5 until the number of values minus 1 (n-1).
4. Read the elements in an array a[i].
5. Increment the iterative value.
6. Initialize the iterative value (i) to 0.
7. Repeat the steps 8 to 12 until the number of values minus 1 (n-1).
8. Assign the iterative value j as i+1.
9. Repeat the steps 10 - 11 until iterative value (j) less than no. of values (n).
10. If a[i]>a[j] then
Swap the values using temporary variable.
11. Increment the iterative value (j)
12. Increment the iterative value (i)
13. Display the array elements in the ascending and descending order.
Flow chart
Start
Read the number of values
Repeat the process until n-1
Read the elements in an array a[i]
Next i
Repeat the loop i from 0 to n-1
Repeat the loop j from i+1 to n
a[i]>a[j]
Swap the elements
Next j
Next i
A
A
Repeat loop i from 0 to n
Print the sorted array
elements in ascending order
Next i
Repeat loop i from n-1 to 0
Print the sorted array
elements in descending
Next i
Stop
Program
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n,a[20],temp;
clrscr( );
printf(“Enter the size of the array \n”);
scanf(“%d”,&n);
printf(“Enter the elements of the array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(“The elements sorted in the Ascending order:\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
printf(“The elements sorted in the Descending order:\n”);
for(i=n-1;i>=0;i--)
{
printf(“%d\t”,a[i]);
}
getch( );
}
Sample Input and Output:
INPUT: Enter the size of the array 5
Enter the elements of the array:
20
44
3
56
12
OUTPUT: The elements sorted in the Ascending oder
3
12
20
44
56
The elements sorted in the Descending oder
56
44
20
12
3
Result
Thus the C program to sort the numbers in an array in Ascending and descending order
has been done and the output is verified.
EX.NO : 17
DATE :
REVERSING THE ELEMENTS IN AN ARRAY
Aim
To Display all the elements of an array in reverse order using functions
Description
Array
The elements are stored in a memory locations in sequential order is called Arrays.
It is a linear list which can be accessed by indexes.
It is a finite set of homogeneous elements where finite specifies there must be a specific
number of elements in the array & homogeneous specifies all the elements in the array
must be of same type
For Ex: It is possible to declare an array for the set of salaries of group of employees.
i.e., int salary[10];
where, 10 ----- referred as index or subscript.
Similarly, an array of 10 integers can be specified by
int a[10];
Operations: There are 2 operations that can be performed that access an array
1. Extraction
It is the function that tells retrieving a data element from an array. This
Function accepts 3 information
An array
An index
The element that is returned from that array Ex: printf(a[i]);
2. Storing
It is function that tells to store the data to an array.. This function requires
3 information
An array
An index
An element which is assigned to an array Ex: a[i]=data;
Lower Bound: The smallest element of an array index is referred as lower bound, which
is fixed at 0 the location of an array.
Upper Bound: The highest element of an array index is referred as upper bound, which
is depends on the program execution.
Range: It specifies the no. of elements in an array. It can be specified by the following
mentioned formulae,
Range=Upperbound-lowerbound+1
During the execution of the program these bounds may change.
It is the useful technique to declare a bound as a constant using “#define” identifier. So
that the time required modifying the size will be minimized in case of complex
programs.
reverse
Algorithm
1. Read the number of elements in an array (n). 2. Initialize the iterative value (i) as zero.
3. Repeat the steps 4 and 5 until no. of values minus 1 (n-1).
4. Read the elements of an array a[i].
5. Increment the iterative value.
6. Display the elements from the last position of an array to first position of an array.
Flowchart
Start
reverse
Read totaSl tnoop. of
elements
Repeat the process until n-1
Read the array elements a[i]
Next i
Repeat the loop i
from n-1 to 0
Display the array elements
Decrement i
Stop
Program
#include<stdio.h>
#include<conio.h>
void reverse();
int n,i,a[10];
void main()
{
reverse();
}
void reverse()
{
int n,i;
clrscr();
printf("Enter the total number of elements");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Reversed elements are:\n");
for(i=n-1;i>=0;i--)
{
printf("%d\n",a[i]);
}
getch();
}
Sample Input and Output: INPUT: Enter the total number of elements 5
Enter the Elements:
12
34
56
78
90
OUTPUT: Reversed elements are: 90
78
56
34
12
Result
Thus all the elements of an array are displayed in reverse order.
EX.NO : 18
DATE :
MATRIX ADDITION AND MULTIPLICATION
Aim
Implement Matrix addition and Multiplication using an array
Algorithm
Matrix Addition:
1. Read the total number of rows & columns for the matrix
2. Read the elements for A Matrix and store it into two dimensional arrays.
3. Read the elements for B Matrix and store it into other two dimensional array.
4. Add the elements of 2 matrixes namely A & B through the nested for Loops and store
the result into a resultant 2-dimesional array C.
5. Display the result
Matrix Multiplication:
1. Read the total number of rows & columns for the matrix
2. Read the elements for A Matrix and store it into two dimensional arrays.
3. Read the elements for B Matrix and store it into other two dimensional array.
4. Multiply the elements of 2 matrixes namely A & B through the nested for Loops and
store the result into a resultant 2-dimesional array C.
5. Display the result
Flowchart
Matrix Addition
Start
Read the rows & columns for the
matrixes A & B
Read the elements for the Matrix A
Store the elements into a 2-dimensional array for Matrix A
through the nested for loops
Read the elements for the Matrix B
Store the elements into a 2-dimensional array for Matrix B
through the nested for loops
Display the elements of 2 Matrixes A & B through the
nested for loops
Add the elements of 2 Matrixes A & B through the nested for loops and store the result in a resultant 2-dimensional
array C
Display the result through the nested for loops
Stop
Matrix Multiplication
Start
Read the rows & columns for the
matrixes A & B
Read the elements for the Matrix A
Store the elements into a 2-dimensional array for Matrix A
through the nested for loops
Read the elements for the Matrix B
Store the elements into a 2-dimensional array for Matrix B
through the nested for loops
Display the elements of 2 Matrixes A & B through the
nested for loops
Add the elements of 2 Matrixes A & B through the nested for loops and store the result in a resultant 2-dimensional
array C
Display the result through the nested for loops
Stop
Program
1. Matrix Addition
#include<stdio.h>
#include<conio.h>
int a[25][25],b[25][25],c[25][25],m,n,i,j;
void main()
{
clrscr();
printf("Enter the rows and columns of two matrixes\n");
scanf("%d %d",&m,&n);
printf("Enter the elements of A matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of B matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Elements of A matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
}
printf("\nElements of B matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",b[i][j]);
}
}
printf("\nThe addition of 2 matrixes are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
}
getch();
}
{
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
}
2. Matrix Multiplication
#include<stdio.h>
#include<conio.h>
int a[25][25],b[25][25],c[25][25],m,n,i,j,k;
void main()
{
clrscr();
printf("Enter the rows and columns of two matrixes\n");
scanf("%d %d",&m,&n);
printf("Enter the elements of A matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of B matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Elements of A matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
}
printf("\nElements of B matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",b[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\nThe Multiplication of 2 matrixes are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
}
getch();
}
printf("\t%d",c[i][j]);
}
Sample Input and Output: Matrix Addition
INPUT: Enter the rows and columns of two matrixes 3
3
Enter the elements of A matrix
1
2
3
4
5
6
7
8
9
Enter the elements of B matrix
1
2
3
4
5
6
7
8
9
OUTPUT:
Elements of A matrix are:
1 2 3
4 5 6
7 8 9
Elements of B matrix are:
1 2 3
4 5 6
7 8 9
The addition of 2 matrixe are:
2 4 6
8 10 12
14 16 18
Matrix Multiplication
INPUT: Enter the rows and columns of two matrixes 3
3
Enter the elements of A matrix
1
2
3
4
5
6
7
8 9
Enter the elements of B matrix
1
2
3
4
5
6
7
8
9
OUTPUT:
Elements of A matrix are:
1 2 3
4 5 6
7 8 9
Elements of B matrix are:
1 2 3
4 5 6
7 8 9
The Multiplication of 2 matrixes are:
30 36 42
66 81 96
102 126 150
Result
Thus Matrix addition and Multiplication using an array was implemented successfully.
EX.NO : 19
DATE :
MANIPULATION OF STRING FUNCTIONS
Aim To write a C program to implement string functions without using library functions.
COMPUTING STRING LENGTH
Algorithm:
1. Read the String (str).
2. Initialize a counter variable len as zero.
3. Increment the counter variable until the null character of string is reached.
4. Print the length of the string (len).
Flow chart:
Start
Read the String
Initialize length as zero
Repeat the process until
null character is reached
Increment the length
Display the length of the String
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
}
INPUT:
char str[50];
int len;
clrscr();
printf("Enter the string:");
scanf("%s",str);
for(len=0;str[len]!='\0';len++);
printf("Length of string is %d\n",len);
getch();
Enter the string: RVS ETGI
OUTPUT:
Length of string is 08
Result
Thus the program was successfully executed and output was verified
STRING COMPARISON
Algorithm
1. Read two Strings (str1, str2). 2. Compute the length of the two strings and store it in len1, len2 respectively.
3. If the length of two strings are equal
a. Initialize counter value (i) as 1 and (j) as 0.
b. Repeat the steps 3.c to 3.e until the length of the strings.
c. Check each character of the strings.
d. If they are same
i. Increment the counter variable j.
e. Increment the counter variable i
4. If the j value is same as the length of the String
Display both the Strings are Equal.
5. Else
Displays both the Strings are not equal.
Flow chart
Start
Read two Strings namely str1, str2
Compute the length of the two strings len1, len2
If the lengths N
are equal
Y
Compare each character of the two strings
If they
are same
Increment the counter variable j
If j value is N
equal to length
of the string
Y
Display Both the Strings
are Equal
Display Both the Strings are
Unequal
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
int i,j=0,len1,len2;
clrscr();
printf("Enter the two strings:");
scanf("%s%s",str1,str2);
for(len1=0;str1[len1]!='\0';len1++);
for(len2=0;str2[len2]!='\0';len2++);
if(len1==len2)
{
for(i=1;i<=len1;i++)
{
if(str1[i]==str2[i])
j+=1;
}
}
}
INPUT:
if(j==len2)
printf("Both strings are equal");
else
printf("Both strings are not equal");
getch();
Enter the two strings: apple apple
Both strings are equal
OUTPUT:
Enter the two strings: boy ball
Both strings are not equal.
Result
Thus the program was successfully executed and output was verified
COPYING THE STRING
Algorithm
1. Read the String (str1).
2. Initialize a counter variable (i) as zero.
3. Repeat the steps 4 and 5 until the null character of string is reached.
4. Copy each character from string1 to another string (str2).
5. Increment the counter variable.
6. Add the null character to the end of the String (str2).
7. Display the copied String (str2).
Flow chart
Start
Read the String (str1)
Initialize length as zero
Repeat the process until
null character is reached
Copy each character from string1 to String 2
Increment the length
Add the null character to the end of the String2
Display the Copied String(str2)
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
int i,j;
clrscr();
printf("Enter the first string:");
scanf("%s",str1);
printf("\nThe first string is %s",str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("\nCopy the string without using strcpy() function");
printf("\nThe Copied String is %s",str2);
getch();
}
INPUT:
Enter the first String: India
OUTPUT:
The First string is India
Copy the string without using strcpy() function
The Copied String is India
Result
Thus the program was successfully executed and output was verified
STRING CONCATENATION
Algorithm
1. Read two Strings (str1,str2).
2. Copy each character from string1 to another string (str3) until the null character of
string1 is reached.
3. Continuously copy each character from string2 to string (str3) until the null character of
string2 is reached.
4. Add the null character to the end of the String (str3).
5. Display the concatenated String (str3).
Flow chart
Start
Read two strings str1,str2
Copy each character from string (str1) to String (str3)
Continuously copy each character from string (str2) to String (str3)
Display the Concatenated String (str3)
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[10],str2[10],str3[10];
int i,j;
clrscr();
printf("Enter the two string:");
scanf("%s%s",str1,str2);
for(i=0;str1[i]!='\0';i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';j++)
{
str3[i]=str2[j];
i++;
}
}
INPUT:
str3[i]='\0';
printf("Concatenated String is.....%s\n",str3);
getch();
Enter the two string: Anna University
OUTPUT:
Concatenated string is……. AnnaUniversity
RESULT:
Thus the string manipulation functions are executed successfully without using library
functions.
EX.NO : 20
DATE :
ARRANGING NAMES IN ALPHABETICAL ORDER
Aim
To write a C program to arrange the names in alphabetical order.
Algorithm
1. Read the number of names (n) to be sorted.
2. Read the names in an array names[i].
3. Increment the iterative value.
4. Initialize the iterative value (i) to 0.
5. Repeat the steps 7 to 11 until the number of values minus 1 (n-1).
6. Assign the iterative value j as i+1.
7. Repeat the steps 9 - 10 until iterative value (j) less than no. of values (n).
8. Compare the Strings as
If names[i], names[j]>0 then
Swap the names using temporary variable by using the strcpy() function.
9. Increment the iterative value (j)
10. Increment the iterative value (i)
11. Display the names in the sorted order.
Flow chart
Start
Read the number of values (n)
Read the elements in an array
names[i]
Compare the Strings using strcmp() function
If the value is
greater than 0
Swap the names using temporary
variable using strcpy() function
Display the sorted names
stop
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char names[50],temp[20];
int n,i,j;
clrscr();
printf("\n How many names are there to sort?:");
scanf("%d",&n);
printf("\n Enter the %d names one by one\n",n);
for(i=0;i<n;i++)
scanf("%s",names[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(names[i],names[j])>0)
{
strcpy(temp,names[i]);
strcpy(names[i],names[j]);
strcpy(names[j],temp);
}
printf("\n Names in alphabetical order are:");
for(i=0;i<n;i++)
printf("\n %s",names[i]);
getch();
}
Sample Input and Output:
INPUT:
How many names are there to sort?
5
Enter the 5 names one by one
India
Asia
Nepal
Srilanka
Bhutan
OUTPUT: Names in alphabetical order are: Aravind
Banu
Priya
Sakthi
Yamuna
Result
Thus the C program to arrange the given string in alphabetical order executed
successfully.
EX.NO : 22
DATE :
FINDING THE FACTORIAL USING RECURSION
Aim:
To write a C program to calculate the factorial of the given number using recursive
functions.
Algorithm
1. Read the number (a)
2. Call the function rec() by passing the number
3. If the number is 0
Return the factorial value as 1.
4. Else
Compute the factorial value by recursively calling rec() function by
decrementing the number (x).
5. Return the factorial value (f).
6. Display the factorial value.
Flowchart
Start
Read the number a
recursive(a)
Display the Factorial Value
Stop
Recursive (a)
Y If the
value is 0
Factorial value is 1
N
Compute factorial value as
F=f*rec(a-1)
Return the factorial value
Program:
#include<stdio.h>
#include<conio.h>
int recursive(int);
void main ()
{
int a,fact;
clrscr();
printf("Enter the number:");
scanf("%u",&a);
fact=recursive(a);
printf("The factorial of %u is %u",a,fact);
getch();
}
int recursive(x)
int x;
{ int f;
if(x==0)
return(1);
else
f=x*recursive(x-1);
return(f);
}
Sample Input and Output:
INPUT:
Enter the number 5
OUTPUT:
The factorial of 5 is 120
Result
Thus the program for the factorial using recursive function is performed.
EX.NO : 23
DATE :
PREPARING MARK LIST USING STRUCTURES CONCEPT
Aim
To write a program in C to print the mark sheet of „n‟ students using structure.
Algorithm
1. Initialize the structure student with its members like name, number, marks, total,
average and grade.
2. Read the number of students (n).
3. Read the student number, name and six subject marks for n students.
4. Find the total marks and average for each student.
5. Award the grade for each student by checking the marks of all the subjects.
6. Print the student mark list displaying the student number, name, total marks, average
and grade for each student.
Flowchart
Start
Initialize the structure student with its members like name, number, marks,
total, average and grade
Read the number of students (n)
Read the student number,
name and six subject marks
Find the total marks and average
for each student
If at least one of
the marks is less
than 35
Y
Grade = Fail
N
Y
If avg>=75 Grade = Distinction
N
If avg <75
& >=60
Y Grade = first class
N
If avg <60 Y
& >=50
N
Grade = Second class
Display the Marklist for all the Students
Stop
Program
#include<string.h> #include<stdio.h>
#include<conio.h>
struct student
{
}s[10];
int sno,m1,m2,m3,m4,m5,m6,tot;
char name[10],grade[50];
float avg;
void main()
{
float avg;
int tot,n,i;
clrscr();
printf("Enter the number of students to process..");
scanf("%d",&n);
printf("Enter Student number, name and 6 subject marks ...\n");
for(i=1;i<=n;i++)
{
scanf("%d%s%d%d%d%d%d%d",&s[i].sno,&s[i].name,&s[i].m1,&s[i].m2,&s[
i].m3,&s[i].m4,&s[i].m5,&s[i].m6);
s[i].tot=s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5+s[i].m6;
s[i].avg=s[i].tot/6;
if(s[i].m1<35||s[i].m2<35||s[i].m3<35||s[i].m4<35||s[i].m5<35||s[i].m6<35)
strcpy(s[i].grade,"Fail");
else
{
}
}
if(s[i].avg>=75)
strcpy(s[i].grade,"Distinction");
else if(s[i].avg<75 && s[i].avg>=60)
strcpy(s[i].grade,"First class");
else if(s[i].avg<60 && s[i].avg>=50)
strcpy(s[i].grade,"Second class");
printf("Student Mark List \n");
printf("S.No\tSname\tTotal\tAverage\tGrade\n");
printf("-------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%d\t%s\t%d\t%f\t%s",s[i].sno,s[i].name,s[i].tot,s[i].avg,s[i].grade);
printf("\n");
}
getch();
}
Sample Input and Output
INPUT: Enter the number of students to process.. 2
Enter Student number, name and 6 subject marks...
100
aravind
96
78
85
76
99
86
101
Brinda
72
64
60
77
32
62
OUTPUT:
Student Mark List
S.No Sname Total Average Grade
------------------------------------------------------------------------------ 100 aravind 520 86.000 Distinction
101 brinda 367 61.16 Fail
Result
Thus the program to print the mark sheet of „n‟ students using structure is written and
executed.
String is palindrome or not #include<stdio.h> #include<conio.h>
#include<string.h>
void main() {
char name[10]; int i=0,j,len;
clrscr();
printf("Enter Name :"); scanf("%s" ,name); len=strlen(name); for(i=0,j=len-1;i<=j;i++,j--)
if(name[i]!=name[j])
{ printf("Not Palindrome");
getch(); exit(0);
}
printf("Palindrome"); getch();
Transpose of Matrix #include<stdio.h> #include<conio.h>
void main() {
int r,c,i,j,m[10][10]; clrscr();
printf("Enter number of rows and columns:");
scanf("%d %d",&r,&c);
for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&m[i][j]);
printf("\nThe Transpose matrix");
for(i=0;i<r;i++)
{
printf("\n"); for(j=0;j<c;j++) printf(" %d",m[j][i]);
}
getch(); }
C program to find largest digit of a number #include <stdio.h>
int main() {
int num, large = 0, rem = 0;
/* get the input from the user */
printf("Enter your input value:");
scanf("%d", &num);
/* finding the largest digit of the given input */
while (num > 0) {
rem = num % 10;
if (rem > large) {
large = rem;
}
num = num / 10;
}
/* print the largest digit of the number */
printf("Largest digit of the number is %d\n", large);
return 0;
}
Calculate and display total cost of 4 models of PC using 1 dimensional Array
Code to Find Number of Vowels:
#include<stdio.h>
void main()
{
char line[150];
int i,v,ch;
v= 0;
printf("Enter a line of string:\n");
gets(line);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E'
|| line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
}
printf("Vowels: %d",v);
}
Calculator Using Function:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
int main(){
int num1, num2, choice;
printf("[0] Exit\v[1] Add\v[2] Subtract\v[3] Multiply\v[4] Divide");
scanf("%d", &choice);
switch(choice){
case 0:
return 0;
break;
case 1:
printf("Enter 1st number:\n");
scanf("%d", &num1);
printf("Enter 2nd number:\n");
scanf("%d", &num2);
printf("%d", add(num1,num2));
break;
case 2:
printf("Enter 1st number:\n");
scanf("%d", &num1);
printf("Enter 2nd number:\n");
scanf("%d", &num2);
printf("%d ", sub(num1,num2));
break;
case 3:
printf("Enter 1st number:\n");
scanf("%d", &num1);
printf("Enter 2nd number:\n");
scanf("%d", &num2);
printf("%d", mul(num1,num2));
break;
case 4:
printf("Enter 1st number:\n");
scanf("%d", &num1);
printf("Enter 2nd number:\n");
scanf("%d", &num2);
printf("%d", div(num1,num2));
break;
default:
printf("That is not a valid choice.");
break;
}
//Addition
int add(int x,int y){
int z = x + y;
return z;
}
//Subtraction
int sub(int a,int b){
int c = a - b;
return c;
}
//Multiplication
int mul(int d,int e){
int f = d * e;
return f;
}
//Division
int div(int g,int h){
int i = g / h;
return i;
}
}
Transpose of Matrix using Function:
#include<stdio.h>
#include<conio.h>
void read_mat(int,int,int[10][10]);
void print_mat(int,int,int[10][10]);
void transpose_mat(int,int,int[10][10]);
main()
{
int a[10][10],b[10][10],i,j,m,n;
clrscr();
printf("Enter Matrix Row size and Column size:");
scanf("%d %d",&m,&n);
read_mat(m,n,a);
printf("Before Transpose:\n");
print_mat(m,n,a);
printf("After Transpose Matrix:\n");
transpose_mat(m,n,a);
getch();
}
void read_mat(int x,int y,int c[10][10])
{
int i,j;
printf("Enter Elements:");
for(i=0;i<x;i++)
for(j=0;j<y;j++)
scanf("%d",&c[i][j]);
}
void print_mat(int x,int y,int c[10][10])
{
int i,j;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
printf("%5d",c[i][j]);
printf("\n");
}
}
void transpose_mat(int x,int y,int a[10][10])
{
int i,j,b[10][10];
for(i=0;i<x;i++)
for(j=0;j<y;j++)
b[j][i]=a[i][j];
print_mat(x,y,b);
}
output:
Enter Matrix Row size and Column size:
3
3
Enter Elements:
1
2
3
4
5
6
7
8
9
Before Transpose:
1 2 3
4 5 6
7 8 9
After Transpose Matrix:
1 4 7
2 5 8
3 6 9
Maximum of three numbers using Function:
#include <stdio.h>
int maxofthree(int,int,int);
void main()
{
int a,b,c,maxdup;
int a1,b1,c1;
clrscr();
printf("Enter a,b,c");
scanf("%d%d%d",&a,&b,&c);
maxdup=maxofthree(a,b,c); ///function calling
printf("Max is : %d",maxdup);
printf("nter a1,b1,c1");
scanf("%d%d%d",&a1,&b1,&c1);
maxdup=maxofthree(a1,b1,c1); ///function calling
printf("Max1 is : %d",maxdup);
getch();
}
int maxofthree(int x,int y,int z)
{
int max;
if(x>y)
max=x;
else
max=y;
if(max>z)
;
else
max=z;
return max;
}
Pass By Reference (Swap two numbers):
void swap(int *num1, int *num2);
void main() {
int x, y;
printf("\nEnter First number : ");
scanf("%d", &x);
printf("\nEnter Second number : ");
scanf("%d", &y);
printf("\nBefore Swaping x = %d and y = %d", x, y);
swap(&x, &y); // Function Call - Pass By Reference
printf("\nAfter Swaping x = %d and y = %d", x, y);
getch();
}
void swap(int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
Pass by value ( Swap Two numbers):
#include
#include
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}
Fibonacci Series Using recursive function:
#include<stdio.h>
int Fibonacci(int);
main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
NCR using recursion:
#include<stdio.h>
int ncr(int n,int r);
int main()
{
int n,r,y;
printf("enter the value of n and r resp.\n");
scanf("%d %d",&n,&r);
y=ncr(n,r);
printf("the value of ncr is %d\n",y);
return 0;
}
int ncr(int n,int r)
{
int y,res;
if(r==0)
{
y=1;
return y;
}
else if(n==r)
{
y=1;
return y;
}
else
{
res=ncr(n-1,r-1)+ncr(n-1,r);
}
return res;
}
Pascals Triangle:
1
1 1
1 2 1
1 3 3 1
#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0 ; c <= i; c++)
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
C program to calculate employee salary using structures:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* structure to store employee salary details */
struct employee {
int empId;
char name[32];
int basic, hra, da, ma;
int pf, insurance;
float gross, net;
};
/* prints payslip for the requested employee */
void printSalary(struct employee e1) {
printf("Salary Slip of %s:\n", e1.name);
printf("Employee ID: %d\n", e1.empId);
printf("Basic Salary: %d\n", e1.basic);
printf("House Rent Allowance: %d\n", e1.hra);
printf("Dearness Allowance: %d\n", e1.da);
printf("Medical Allowance: %d\n", e1.ma);
printf("Gross Salary: %.2f Rupees\n", e1.gross);
printf("\nDeductions: \n");
printf("Provident fund: %d\n", e1.pf);
printf("Insurance: %d\n", e1.insurance);
printf("\nNet Salary: %.2f Rupees\n\n", e1.net);
return;
}
int main() {
int i, ch, num, flag, empID;
struct employee *e1;
/* get the number of employees from the user */
printf("Enter the number of employees:");
scanf("%d", &num);
/* dynamically allocate memory to store employee salary details */
e1 = (struct employee *)malloc(sizeof(struct employee) * num);
/* get the employee salary details from the customer */
printf("Enter your input for every employee:\n");
for (i = 0; i < num; i++) {
printf("Employee ID:");
scanf("%d", &(e1[i].empId));
getchar();
printf("Employee Name:");
fgets(e1[i].name, 32, stdin);
e1[i].name[strlen(e1[i].name) - 1] = '\0';
printf("Basic Salary, HRA:");
scanf("%d%d", &(e1[i].basic), &(e1[i].hra));
printf("DA, Medical Allowance:");
scanf("%d%d", &(e1[i].da), &(e1[i].ma));
printf("PF and Insurance:");
scanf("%d%d", &(e1[i].pf), &(e1[i].insurance));
printf("\n");
}
/* gross and net salary calculation */
for (i = 0; i < num; i++) {
e1[i].gross = e1[i].basic +
(e1[i].hra * e1[i].basic) / 100 +
(e1[i].da * e1[i].basic) / 100 +
(e1[i].ma * e1[i].basic) / 100;
e1[i].net = e1[i].gross - (e1[i].pf + e1[i].insurance);
}
/* printing payslip for the given employee ID */
while (1) {
printf("Enter employee ID to get payslip:");
scanf("%d", &empID);
flag = 0;
for (i = 0; i < num; i++) {
if (empID == e1[i].empId) {
printSalary(e1[i]);
flag = 1;
}
}
if (!flag) {
printf("No Record Found!!\n");
}
printf("Do You Want To Continue(1/0):");
scanf("%d", &ch);
if (!ch) {
break;
}
}
return 0;
}
Employee Details Using structure:
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,
e[i].name, e[i].bpay, e[i].allow, e[i].ded, e[i].npay) ;
}
getch() ;
}
Sorting Two Structures on the basis of any structure element and Display Information:
#include<stdio.h>
struct cricket {
char pname[20];
char tname[20];
int avg;
} player[10], temp;
void main() {
int i, j, n;
clrscr();
for (i = 0; i < 10; i++) {
printf("\nEnter Player Name : ");
scanf("%s", player[i].pname);
printf("\nEnter Team Name : ");
scanf("%s", player[i].tname);
printf("\nEnter Average : ");
scanf("%d", &player[i].avg);
printf("\n");
}
n = 10;
for (i = 1; i < n; i++)
for (j = 0; j < n - i; j++) {
if (strcmp(player[j].tname, player[j + 1].tname) > 0) {
temp = player[j];
player[j] = player[j + 1];
player[j + 1] = temp;
}
}
for (i = 0; i < n; i++) {
printf("\n%s\t%s\t%d",player[i].pname,player[i].tname,player[i].avg);
}
getch();
}
Output:
Enter Player Name : Sehwag
Enter Team Name : India
Enter Average : 78
Enter Player Name : Ponting
Enter Team Name : Australia
Enter Average : 65
Enter Player Name : Lara
Enter Team Name : WI
Enter Average : 67
Ponting Australia 65
Sehwag India 78
Lara WI 67
String Pass as an Argument:
#include <stdio.h>
#include <string.h>
void convert(char ch[]);
int main(){
char c[50];
printf("Enter string: ");
convert(c);
Display(c); // Passing string c to function.
return 0;
}
void convert(char ch[])
{
Int i;
for (i = 0; i < 50; i++)
{
ch[i] = toupper(ch[i]);
}
printf("String Output: ");
puts(ch);
}