Manual
Manual
Aim:
To write C programs to implement I/O statements, operators and expressions.
Program:
#include <stdio.h>
void main()
{
float kmph, miph;
printf("Enter kilometers per hour: ");
scanf("%f", &kmph);
miph = (kmph * 0.62);
printf("%0.2f miles per hour\n", miph);
}
Output:
Enter kilometers per hour: 150
93.21 miles per hour
Algorithm: 1 (b) Convert the given temperature from Centigrade to Fahrenheit
Step 1: Start
Step 2: Get the temperature value in Centigrade as input
Step 3: Calculate, fah = ((9.0 / 5.0) * centi) + 32.0
Step 4: Display the Fahrenheit value in fah
Step 5: Stop
Program:
#include <stdio.h>
void main()
{
float fah, centi;
printf("Enter temperature in Centigrade ");
scanf("%f", ¢i);
fah = ((9.0 / 5.0) * centi) + 32.0;
printf(".2%f degrees Fahrenheit\n", fah);
}
Output:
Enter temperature in Centigrade 89.34
192.81 degrees Fahrenheit
Algorithm: 1 (c) Convert the given time in minutes to Hours and Minutes
Step 1: Start
Step 2: Get the total minutes as input
Step 3: Divide total minutes by 60 and take Quotient as hours, remainder as Minutes.
Step 4: Display the result in hours and minutes format.
Step 5: Stop
Program:
#include <stdio.h>
void main()
{
int totalmin, hour,min;
printf("Enter Minutes: ");
scanf("%d", &totalmin);
hour = totalmin / 60;
min = totalmin % 60;
printf("%d Hours \t %d Minutes", hour, min);
}
Output:
Enter Minutes: 345
5 Hours 45 Minutes
Result:
Thus the above programs to implement I/O statements, operators and expressions were
done and executed successfully.
Ex No: 2 Implementation of Decision Making Constructs
Aim:
To write C programs to implement Decision Making Constructs such as if – else and
switch.
Program:
#include <stdio.h>
void main()
{
int num;
printf("Enter a Number : ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is an even Number", num);
else
printf("%d is an odd Number", num);
}
Output:
Execution: 1
Enter a Number: 45
45 is an odd Number
Execution: 2
Enter a Number: 20
20 is an even Number
Program:
#include <stdio.h>
void main()
{
int year;
printf("Enter a year : ");
scanf("%d", &year);
if ((year % 4) == 0)
printf("%d is a leap year.\n", year);
else if ((year % 400) == 0)
printf("%d is a leap year.\n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year.\n", year);
else
printf("%d is not a leap year \n", year);
}
Output:
Execution: 1
Enter a year : 2024
2024 is a leap year.
Execution: 2
Enter a year : 1400
1400 is a leap year.
Output:
Execution: 1
Input the values for X and Y coordinate : 3 -6
The coordinate point (3,-6) lies in Fourth quadrant
Execution: 2
Input the values for X and Y coordinate : 0 0
The coordinate point (0,0) lies at the origin
Algorithm: 2 (d) Find roots of a Quadratic Equation
Step 1: Start
Step 2: Get three values as input
Step 3: Calculate d=b*b-4*a*c
Step 4: Check if (d==0)
Step 4.1: Calculate x1=x2= -b/(2.0*a)
Step 4.2: Display values of x1 and x2 with Roots are Equal
Step 5: otherwise if(d>0)
Step 5.1: Calculate x1=(-b+sqrt(d))/(2*a)
Step 5.2: Calculate x2=(-b-sqrt(d))/(2*a)
Step 5.3: Display values of x1 and x2 with roots are Real and Different
Step 6: otherwise Display Root are imaginary
Step 7: Stop
Program:
#include <stdio.h>
void main()
{
int a, b, c, d;
float x1, x2;
printf("Enter three values : ");
scanf("%d %d %d", &a, &b, &c);
d=b*b-4*a*c;
if(d==0)
{
printf("Both roots are equal\n");
x1=-b/(2.0*a);
x2=x1;
printf("Root1= %f\n",x1);
printf("Root2= %f",x2);
}
else if(d>0)
{
printf("Both roots are real and different\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("Root1= %f\n",x1);
printf("Root2= %f",x2);
}
else
printf("Root are imaginary");
}
Output:
Execution: 1
Enter three values : 1 2 1
Both roots are equal
Root1= -1.000000
Root2= -1.000000
Execution: 2
Enter three values : 2 4 1
Both roots are real and different
Root1= -0.292893
Root2= -1.707107
Execution: 3
Enter three values : 4 3 2
Root are imaginary
Program:
#include <stdio.h>
void main()
{
int a, b;
int choice;
printf(" 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d", &a, &b);
printf("Enter your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Sum = %d", a+b);
break;
case 2:
printf("Difference = %d", a-b);
break;
case 3:
printf("Product = %d", a*b);
break;
case 4:
printf("Quotient = %d", a/b);
break;
default :
printf(" Enter Your Correct Choice");
break;
}
}
Output:
Execution: 1
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter the values of a & b: 24 5
Enter your Choice : 4
Quotient = 4
Execution: 2
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 23 78
Enter your Choice : 6
Enter Your Correct Choice
Program:
#include <stdio.h>
void main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is an Vowel", ch);
break;
default:
printf("%c is a Consonant", ch);
}
}
Output:
Execution: 1
Enter any character: a
a is an Vowel
Execution: 2
Enter any character: g
g is a Consonant
Result:
Thus the above programs to implement Decision Making Constructs such as if – else and
switch statements were done and executed successfully.
Ex No: 3 Implementation of Looping Constructs
Aim:
To write C programs to implement Looping constructs such as for, while, do-while loops.
Program:
#include <stdio.h>
#include <math.h>
void main()
{
int num, temp, r, sum = 0, n = 0 ;
printf(" Input an integer : ");
scanf("%d", &num);
temp = num;
while (num!=0)
{
num=num/10;
++n;
}
num=temp;
do
{
r = num % 10;
sum = sum+ pow(r, n);
num = num/10;
} while(num>0);
if(sum == temp)
printf("%d is an Armstrong number", sum);
else
printf("%d is not an Armstrong number", sum);
}
Output: Execution: 1
Enter a number: 153
153 is an Armstrong number
Execution: 2
Enter a number: 172
172 is not an Armstrong number
Algorithm: 3(b) check a given number is an palindrome number or not
Step 1: Start
Step 2: Get a number as input and assign temp=num.
Step 3: Initialize sum=0
Step 4: Repeat the steps 4.1 to 4.3 till num>0
Step 4.1: r = num % 10
Step 4.2: sum = sum* 10+r
Step 4.3: num = num/10
Step 5: Check if(sum== temp) Display Number is Palindrome
Step 6: otherwise Display Number is not Palindrome
Step 7: Stop
Program:
#include <stdio.h>
void main()
{
int num, r, sum=0,temp;
printf("Enter a number: ");
scanf("%d", &num);
temp=num;
do
{
r=num % 10;
sum=sum*10+r;
num=num/10;
} while(num>0);
if(sum==temp)
printf("%d is an Palindrome number", temp);
else
printf("%d is not an Palindrome number", temp);
}
Output: Execution: 1
Enter a number: 121
121 is an Palindrome number
Execution: 2
Enter a number: 178
178 is not an Palindrome number
Program:
#include <stdio.h>
void main()
{
int num, r, sum=0,temp;
printf("Enter a number: ");
scanf("%d", &num);
do
{
r = num % 10;
sum = sum + r;
num = num / 10;
} while(num>0);
printf("Sum of digits = %d", sum);
}
Output:
Enter a number: 2367
Sum of digits = 18
Program:
#include <stdio.h>
void main()
{
int age;
printf("Enter Age of a person\n");
scanf("%d", &age);
if(age>=18)
goto Vote;
else
goto Nonvote;
Vote:
printf("Person eligible to vote");
return;
Nonvote:
printf("Person not eligible to vote");
}
Output: Execution: 1
Enter Age of a person
12
Person not eligible to vote
Execution: 2
Enter Age of a person
25
Person eligible to vote
Program:
#include<stdio.h>
void main()
{
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
printf("%d is a Prime number", n);
else
printf("%d is not a Prime number", n);
}
Output: Execution: 1
Enter a number 13
13 is a Prime number
Execution: 2
Enter a number 22
22 is not a Prime number
Algorithm: 3(f) Program using break and continue to find sum of 10 Non-Negative Numbers.
Step 1: Start
Step 2: Inside a loop get 10 numbers as input.
Step 2.1: While getting input if(num<0) continue to get next input number
Step 2.2: otherwise Calculate sum=sum+num and increment c
Step 2.3: if(c==10) Exit the Loop using break
Step 3: Display the value of sum
Step 4: Stop
Program:
#include <stdio.h>
void main()
{
int num, sum = 0, i, j, c=0;
printf("Enter Numbers\n");
for(i = 1; i <= 100; i++)
{
printf("Number %d = ", i);
scanf("%d", &num);
if(num < 0)
{
continue;
}
sum = sum + num;
c++;
if(c==10)
{
break;
}
}
printf("Sum of 10 Non-Negative Numbers = %d", sum);
}
Output:
Enter the Numbers
Number 1 = 3
Number 2 = 4
Number 3 = -5
Number 4 = -6
Number 5 = 2
Number 6 = -6
Number 7 = 7
Number 8 = 8
Number 9 = -1
Number 10 = -45
Number 11 = 6
Number 12 = 8
Number 13 = 7
Number 14 = 4
Number 15 = -3
Number 16 = 9
Sum of 10 Non-Negative Numbers = 58
Result:
Thus the above programs to implement Looping constructs such as for, while, do-while
loops were done and executed successfully.
Ex No: 4 Implementation of Arrays
Aim:
To write C programs to implement Single dimensional, Two dimensional and Multi
dimensional Arrays.
Algorithm: 4(a) Single Dimensional Array (Sorting and Finding Minimum and Maximum
element)
Step 1: Start
Step 2: Get Array size as input
Step 3: Get Array elements as input
Step 4: Using Nested for loops check each element of the arrays.
Step 5: While checking if comparing element is greater than other the swap them and continue
the same with other elements too.
Step 6: After loop ends, Display the Sorted array
Step 7: Display the first and last element as the Minimum and Maximum element.
Step 8: Stop
Program:
#include<stdio.h>
void main()
{
int arr1[100], n, i, j, tmp;
printf("Enter array size: ");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements in Ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\nMaximum array element is %d", arr1[n-1]);
printf("\nMinimum array element is %d", arr1[0]);
}
Output:
Enter array size: 5
Input 5 elements in the array:
67
98
32
45
12
Elements in Ascending order:
12 32 45 67 98
Maximum array element is 98
Minimum array element is 12
Program:
#include<stdio.h>
void main()
{
int x[10][10],y[10][10],z[10][10],r1,c1,r2,c2,i,j,k;
printf("Enter first matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r1);
printf("Enter number of columns =");
scanf("%d",&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&x[i][j]);
}
}
printf("Enter second matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r2);
printf("Enter number of columns =");
scanf("%d",&c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&y[i][j]);
}
}
if((r1==r2)and(c1==c2))
{
printf("Sum of two matrices\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
z[i][j]=x[i][j]+y[i][j];
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",z[i][j]);
}
printf("\n");
}
}
else
{
printf("The Matrix Orders are Different");
}
}
Output:
Enter first matrix elements
Enter number of rows = 3
Enter number of columns =3
1
2
3
4
5
6
7
8
2
Enter second matrix elements
Enter number of rows = 3
Enter number of columns =3
4
5
6
4
5
6
7
6
5
Sum of two matrices
5 7 9
8 10 12
14 14 7
Program:
#include<stdio.h>
void main()
{
int x[10][10],y[10][10],z[10][10],r1,c1,r2,c2,i,j,k;
printf("Enter first matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r1);
printf("Enter number of columns =");
scanf("%d",&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d", &x[i][j]);
}
}
printf("Enter second matrix elements \n");
printf("Enter number of rows = ");
scanf("%d",&r2);
printf("Enter number of columns =");
scanf("%d",&c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d", &y[i][j]);
}
}
if(c1==r2)
{
printf("Product of two matrices\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
z[i][j]=0;
for(k=0;k<c2;k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t", z[i][j]);
}
printf("\n");
}
}
else
{
printf("The Matrix Orders are Different");
}
}
Output:
Enter first matrix elements
Enter number of rows = 3
Enter number of columns =3
1
2
3
4
5
6
7
8
9
Enter second matrix elements
Enter number of rows = 3
Enter number of columns =3
2
3
2
3
4
5
6
2
4
Product of two matrices
26 17 24
59 44 57
92 71 90
Program:
#include<stdio.h>
void main()
{
int i, j, k;
int arr[2][2][2];
printf("Enter elements for 3D Matrix\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
scanf("%d",&arr[i][j][k]);
}
}
}
printf("3D Matrix is \n\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf("%d\t",arr[i][j][k]);
}
printf("\n");
}
printf("-------------\n");
}
}
Output:
Enter elements for 3D Matrix
11
12
13
14
1
2
3
4
3D Matrix is
11 12
13 14
-------------
1 2
3 4
-------------
Result:
Thus the above programs to implement Single dimensional, Two dimensional and Multi
dimensional Arrays were done and executed successfully.
Ex No: 5 Implementation of String Functions
Aim:
To write C programs to implement String Functions.
Program:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[10],str2[10],str3[10];
int len ;
printf("Enter two Strings\n");
scanf("%s %s",str1,str2);
strcat( str1, str2);
printf("Concatenated String : %s\n", str1 );
strcpy(str3, str1);
printf("Copied String: %s\n", str3 );
len = strlen(str1);
printf("Length of the String: %d\n", len );
if(strcmp(str1,str2)==0)
printf("String 1 and String 2 are same”);
else
printf("String1 and String 2 are different”);
}
Output:
Enter two Strings
Hello
Welcome
Concatenated String: HelloWelcome
Copied String: HelloWelcome
Length of the String: 12
String1 and string 2 are different
Program:
#include<stdio.h>
#include<string.h>
void main()
{
char s[100];
printf("Enter a string: ");
scanf("%[^\n]",s);
printf("In Upper Case:\n");
puts(strupr(s));
printf("In Lower Case:\n");
puts(strlwr(s));
Output:
Enter a string: Hai WELCOME to C proGRamming
In Upper Case:
HAI WELCOME TO C PROGRAMMING
In Lower Case:
hai welcome to c programming
Algorithm: 5(c) Find number of vowels, consonants, digits and white spaces in a string
Step 1: Start
Step 2: Get a string as input
Step 3: Read the string character by character
Step 4: Check each character for it as vowel, consonant, digit and whitespace and count it using
variable.
Step 5: Display the count value.
Step 6: Stop
Program:
#include<stdio.h>
void main()
{
char str[200];
int i,vowels=0,consonants=0,digits=0,spaces=0,specialCharacters=0;
printf("Enter a string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||str[i]=='o' || str[i]=='u' || str[i]=='A'
||str[i]=='E' || str[i]=='I' || str[i]=='O' ||str[i]=='U')
{
vowels++;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
consonants++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else if (str[i]==' ')
{
spaces++;
}
else
{
specialCharacters++;
}
}
printf("\nVowels = %d", vowels);
printf("\nConsonants = %d", consonants);
printf("\nDigits = %d", digits);
printf("\nWhite spaces = %d", spaces);
printf("\nSpecial characters = %d", specialCharacters);
}
Output:
Enter a string
Hai 123 How are You?
Vowels = 7
Consonants = 5
Digits = 3
White spaces = 4
Special characters = 1
Result:
Thus the above programs to implement String Functions were done and executed
successfully.
Program:
#include<stdio.h>
void swap(int *p , int *q);
void main()
{
int n1,n2;
printf("Enter number 1 : ");
scanf("%d",&n1);
printf("Enter number 2 : ");
scanf("%d",&n2);
printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
swap(&n1,&n2);
printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
}
void swap(int *p, int *q)
{
int tmp;
tmp = *p;
*p=*q;
*q=tmp;
}
Output:
Enter number 1 : 34
Enter number 2 : 56
Before swapping: n1 = 34, n2 = 56
After swapping: n1 = 56, n2 = 34
Program:
#include<stdio.h>
float large(float a, float b, float c)
{
if(a>b && a>c)
return a;
else if( b>c)
return b;
else
return c;
}
void main()
{
float num1, num2, num3, largest;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
largest = large(num1, num2, num3);
printf("Largest number = %.2f",largest);
}
Output:
Enter three numbers: 23.4
78.6
0.2
Largest number = 78.60
Algorithm:
Step 1: Start
Step 2: Get a number as input
Step 3: Call the function Sum() by passing a value
Step 4: Inside the function using a for loop find the sum of n numbers and display it
Step 5: Stop
Output:
Enter a Number: 15
The Sum is : 120
Result:
Thus the above programs to implement user defined functions were done and executed
successfully.
Algorithm:
Program: 7(a) Display Factorial of a Number
#include<stdio.h>
int factorial(int n);
void main()
{
int n, i;
printf("Enter a number: ");
scanf("%d",&n);
printf("Factorial of %d is: %d ", n, factorial(n));
}
int factorial(int n)
{
if((n==1)||(n==0))
return 1;
else
return (n* factorial(n-1));
}
Output:
Enter a number: 5
Factorial of 5 is: 120
Algorithm:
Program: 7(b) Display Fibonacci Series
#include<stdio.h>
int fibonacci(int);
void main()
{
int n, i;
printf("Enter the number of element in series :\n");
scanf("%d",&n);
printf("Fibonacci series is: \n");
for(i=0;i<n;i++)
{
printf("%d\t ",fibonacci(i));
}
}
int fibonacci(int i)
{
if(i==0)
return 0;
else if(i==1)
return 1;
else
return (fibonacci(i-1)+fibonacci(i-2));
}
Output:
Enter the number of element in series:
10
Fibonacci series is:
0 1 1 2 3 5 8 13 21 34
Result:
Thus the above programs to implement Recursive Functions were done and executed
successfully.
Algorithm:
Program: 8(b) Transpose of a Matrix using Pointers to Arrays
#include<stdio.h>
int main()
{
int *x[10][10],r,c,i,j;
printf("Enter number of rows = ");
scanf("%d",&r);
printf("Enter number of columns =");
scanf("%d",&c);
printf("Enter the matrix elements \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&x[i][j]);
}
}
printf("Transpose of the matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",*(*(x+j)+i));
}
printf("\n");
}
}
Output:
Enter number of rows = 3
Enter number of columns =3
Enter the matrix elements
1
1
1
2
2
2
3
3
3
Transpose of the matrix
1 2 3
1 2 3
1 2 3
Algorithm:
Program: 8(c) Sorting Names using Array of Pointers
#include<stdio.h>
#include<conio.h>
#include<string.h>
void Sort(int n,char *x[]);
void main()
{
char *x[20];
int i,n=0;
printf("Enter no. of String : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the Strings %d : ",i+1);
x[i]=(char *)malloc(20*sizeof(char));
scanf("%s",x[i]);
}
Sort(n,x);
printf("\nSorted Strings\n");
for(i=0;i<n;i++)
{
printf("%s\t", x[i]);
}
}
void Sort(int n,char *x[])
{
int i,j;
char t[20];
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
strcpy(t,x[j]);
strcpy(x[j],x[i]);
strcpy(x[i],t);
}
}
Output:
Enter no. of String : 5
Enter the Strings 1 : Sara
Enter the Strings 2 : Beni
Enter the Strings 3 : Veni
Enter the Strings 4 : Anju
Enter the Strings 5 : Devi
Sorted Strings
Anju Beni Devi Sara Veni
Result:
Thus the above programs to implement Pointers were done and executed successfully.
Algorithm:
Program: 9(a) Student details using Nested Structure
#include<stdio.h>
struct student
{
char name[30];
int roll;
struct address
{
int houseno;
char street[20];
int pincode;
};
struct address addr;
};
void main()
{
struct student stud;
printf("Enter name and roll number of student:\n");
scanf("%s %d",stud.name, &stud.roll);
printf("Enter street name, house number and Pin Code:\n");
scanf("%s %d %d",stud.addr.street, &stud.addr.houseno, &stud.addr.pincode);
printf("Student detail is:\n");
printf("Name: %s\nRoll: %d\n", stud.name, stud.roll);
printf("Address:\n");
printf("%s,\n%d,\n%d.",stud.addr.street, stud.addr.houseno, stud.addr.pincode);
}
Output:
Enter name and roll number of student:
Anju 23
Enter street name, house number and Pin Code:
NewStreet 234 629003
Student detail is:
Name: Anju
Roll: 23
Address:
NewStreet,
234,
629003.
Algorithm:
Program: 9(b) Employee details using Array of Structure
#include<stdio.h>
struct employee
{
char name[20];
char designation[20];
float salary;
}s[5];
void main()
{
int i;
printf("Enter details of 5 employees:\n");
for(i=0;i<5;i++)
{
printf("Enter name :");
scanf("%s", s[i].name);
printf("Enter designation :");
scanf("%s", s[i].designation);
printf("Enter salary :");
scanf("%f", &s[i].salary);
}
printf("\nThe details of employees are :\n");
printf("NAME\tDESIGNATION\tSALARY\n\n");
for(i=0;i<5;i++)
{
printf("%s\t%s\t%.0f\n", s[i].name, s[i].designation, s[i].salary);
}
}
Output:
Enter details of 5 employees:
Enter name :A
Enter designation :Manager
Enter salary :50000
Enter name :B
Enter designation :AssManager
Enter salary :40000
Enter name :C
Enter designation :Clerk
Enter salary :25000
Enter name :D
Enter designation :Officer
Enter salary :35000
Enter name :E
Enter designation :OfficeManager
Enter salary :38000
A Manager 50000
B AssManager 40000
C Clerk 25000
D Officer 35000
E OfficeManager 38000
Algorithm:
Program: 9(c) Student Details using Structure and Pointers
#include <stdio.h>
struct student
{
char name[30];
int roll;
float perc;
};
void main()
{
struct student stu;
struct student *ptr;
ptr= &stu;
printf("Enter details of student: ");
printf("\nName :");
gets(ptr->name);
printf("Roll No :");
scanf("%d",&ptr->roll);
printf("Percentage :");
scanf("%f",&ptr->perc);
printf("\nEntered details: ");
printf("\nName:%s \nRollNo: %d \nPercentage: %.2f\n",ptr->name,ptr->roll,ptr->perc);
}
Output:
Enter details of student:
Name :Anju
Roll No :12
Percentage :87.45
Entered details:
Name:Anju
RollNo: 12
Percentage: 87.45
Algorithm:
Program: 9(d) Implement Union
#include <stdio.h>
union pack
{
char a;
int b;
float c;
};
void main()
{
union pack p;
printf("\nOccupied size by union pack: %d",sizeof(p));
p.a='A';
printf("\nValue of a:%c",p.a);
p.b=10;
printf("\nValue of b:%d",p.b);
p.c=12345.6790;
printf("\nValue of c:%f",p.c);
printf("Initializing values together:\n");
p.a='A';
p.b=10;
p.c=12345.6790;
printf("\nValue of a:%c\n b:%d\n c:%f", p.a,p.b,p.c);
}
Output:
Occupied size by union pack: 4
Value of a: A
Value of b: 10
Value of c: 12345.678711
Initializing values together:
Value of a: ╖
b: 1178658487
c: 12345.678711
Result:
Thus the above programs to implement Structure and Union were done and executed
successfully.
Algorithm:
Program: 10(a) Store Student details in File
#include<stdio.h>
void main()
{
FILE *f1;
char s[10][10];
int i,n,r[10];
printf("Enter no of students\n");
scanf("%d",&n);
printf("Enter content for file1 : \n");
f1=fopen("f1.txt","w");
for(i=1;i<=n;i++)
{
printf("Enter name and rollno\n");
scanf("%s%d",s[i],&r[i]);
fprintf(f1,"%s\n",s[i]);
fprintf(f1,"%d",r[i]);
}
fclose(f1);
f1=fopen("f1.txt","r");
printf("\nName\tRollnumber\n-----\t----------\n");
for(i=1;i<=n;i++)
{
fscanf(f1,"%s",s[i]);
fscanf(f1,"%d",r[i]);
printf("%s\t%d\n",s[i],r[i]);
}
fclose(f1);
}
Output:
Enter no of students
2
Enter content for file1:
Enter name and rollno
Anju 12
Enter name and rollno
Arya 23
Name Rollnumber
------ --------------
Anju 12
Arya 23
Algorithm:
Program: 10(b) Random Access on File
#include <stdio.h>
void main ()
{
char name [20];
int age;
FILE *fp;
fp = fopen ("test.txt","w");
fprintf (fp, "%s %d", "CProgram", 25);
printf("Current cursor position= %d\n",ftell(fp));
fseek(fp, 5,0);
printf("After seeking cursor position= %d\n",ftell(fp));
rewind (fp);
printf("After Rewind cursor position= %d\n",ftell(fp));
fclose(fp);
fp = fopen ("test.txt","r");
fscanf (fp, "%s %d",name,&age);
printf ("Name: %s\nAge: %d\n", name, age);
fclose(fp);
}
Output:
Current cursor position= 11
After seeking cursor position= 5
After Rewind cursor position= 0
Name: CProgram
Age: 25
Algorithm:
Program: 10(c) Area of Circle using Preprocessor #define
#include<stdio.h>
#include<conio.h>
#define AREA(a) (3.14 * a * a)
void main()
{
float r, x;
clrscr();
printf("Enter radius value: ");
scanf("%f",&r);
x = AREA (r);
printf ("Area of circle = %f", x);
getch();
}
Output:
Enter radius value: 7
Area of circle = 153.86
Algorithm:
Program: 10(d) Biggest among two numbers using #if, #else, #endif
#include <stdio.h>
#include<conio.h>
#define X 10
#define Y 5
void main()
{
clrscr();
#if X>Y
printf("%d is Big", X);
#else
printf("%d is Big", Y);
#endif
getch();
}
Output:
10 is Big
Result:
Thus the above programs to implement File Access and Processor Directives were done
and executed successfully.