Temperature Conversion Aim:: Programming in C Lab Sub Code:09BCSM201
Temperature Conversion Aim:: Programming in C Lab Sub Code:09BCSM201
Sub Code:09BCSM201
Program – 1
Temperature Conversion
Aim:
To write a C program for temperature conversion from Fahrenheit to Celsius and
Celsius to Fahrenheit
Algorithm:
1. Start the process
2. Display the menu and read the choice
3. If the choice is “Fahrenheit to Celcius” then,
3.1. Read the Fahrenheit value
3.2. Compute cel = (fah – 32) / 1.8
3.3. Display the result and go to menu.
4. If the choice is “Celcius to Fahrenheit” then,
4.1. Read the celcius value
4.2. Compute fah = (cel * 1.8) + 32
4.3. Display the result and go to menu.
5. If the choice is “Exit”, stop the process.
Program:
#include<stdio.h>
main()
{
float fah,cel;
int choice;
while(1)
{
printf("\n\n Conversion");
printf("\n 1. Fahrenheit -> Celsius\n 2. Celsius -> Fahrenheit \n 3. Exit");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the Temperature in Fahrenheit : ");
-1-
Programming in C Lab
Sub Code:09BCSM201
scanf("%f",&fah);
cel = (fah-32.0)/1.8;
printf("\n The Celsius Equivalent : %.2f",cel);
break;
case 2:
printf("\n Enter the Temperature in Celsius : ");
scanf("%f",&cel);
fah = (cel * 1.8) + 32.0;
printf("\n The Fahrenheit Equivalent : %.2f",fah);
break;
case 3:
return;
default:
printf("\n Error In Input!");
}
getchar();
}
}
Sample Output:
Conversion
1. Fahrenheit -> Celsius
2. Celsius -> Fahrenheit
3. Exit
Enter Your Choice : 1
Conversion
1. Fahrenheit -> Celsius
2. Celsius -> Fahrenheit
3. Exit
Enter Your Choice : 2
Result:
Thus the program for temperature conversion is executed successfully.
-2-
Programming in C Lab
Sub Code:09BCSM201
Program – 2
Armstrong Number
Aim:
To write a C program to find whether the given number is an Armstrong number
or not
Algorithm:
1. Start the process
2. Read the number ‘n’
3. Find the Sum of cubes of each digit in that number n.
4. If the sum is equal to ‘n’ then display that “n is an Armstrong number” otherwise “n
is not an Armstrong number”
5. Stop the process
Program:
#include<stdio.h>
main()
{
int n, x,sum=0,temp ;
printf("Enter the Number : ");
scanf("%d",&n);
temp=n;
while(n>0)
{
x=n%10;
sum+=x*x*x;
n=n/10;
}
if(temp==sum)
printf("The Given number is an Armstrong Number..\n");
else
printf("The Given number is Not an Armstrong Number!\n");
}
-3-
Programming in C Lab
Sub Code:09BCSM201
Sample Output:
Result:
Thus the program to find whether the given number is Armstrong or not is
executed successfully.
-4-
Programming in C Lab
Sub Code:09BCSM201
Program – 3
Quadratic Equation
Aim:
To write a C program to find the quadratic equation for the given values
Algorithm:
Program:
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,discriminant,root1,root2;
printf("\n Input values of a,b&c \n");
scanf("%f%f%f",&a,&b,&c);
discriminant=b*b-4*a*c;
if(discriminant<0)
printf("\n The roots are Imaginary \n");
else
{
root1=(-b+sqrt(discriminant))/(2.0*a);
root2=(-b-sqrt(discriminant))/(2.0*a);
printf("\n Root1 :%.2f \n Root2 :%.2f \n",root1,root2);
}
}
-5-
Programming in C Lab
Sub Code:09BCSM201
Sample Output:
Result:
Thus the program to find the quadratic equation is executed successfully.
-6-
Programming in C Lab
Sub Code:09BCSM201
Program – 4
Maximum & Minimum Number
Aim:
To write a C program to find the maximum and minimum number by sorting the
given list
Algorithm:
Program:
#include<stdio.h>
main()
{
int a[50],n,i,j,temp;
printf("\n Enter the Number of Inputs : ");
scanf("%d",&n);
printf("\n Enter %d Inputs\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\n The Given List is\n");
Display(a,n);
for(i=1;i<n;i++)
{
for(j=i;j<=n;j++)
{
if(a[i] > a[j])
{
-7-
Programming in C Lab
Sub Code:09BCSM201
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\n The Sorted List Is\n");
Display(a,n);
printf("\n The Smallest Number is %d",a[1]);
printf("\n The Largest Number is %d\n",a[n]);
}
void Display(int a[],int n)
{
int i;
for(i=1;i<=n;i++)
printf("%4d",a[i]);
printf("\n");
}
Sample Output:
Enter the Number of Inputs: 4
Enter 4 Inputs
56 76 32 44
Result:
Thus the program to find the maximum and minimum number is executed
successfully
-8-
Programming in C Lab
Sub Code:09BCSM201
Program – 5
Matrix Manipulation
Aim:
To write a C program to perform matrix manipulation such as addition,
subtraction, multiplication and transpose of given matrices.
Algorithm:
Program:
#include<stdio.h>
main()
{
int a[10][10],b[10][10],r1,c1,r2,c2,i,j;
printf("\n Enter the Number of Rows & Columns for Matrix 1\n");
scanf("%d%d",&r1,&c1);
printf("\n Values of Matrix 1\n");
Input(a,r1,c1);
printf("\n Enter The Number of Rows & Columns for Matrix 2\n");
scanf("%d%d",&r2,&c2);
Input(b,r2,c2);
-9-
Programming in C Lab
Sub Code:09BCSM201
printf("\n Matrix 1\n");
Display(a,r1,c1);
getchar();
printf("\n Matrix 2\n");
Display(b,r2,c2);
getchar();
printf("\n Matrix Addition");
if(r1!=r2 || c1 != c2)
printf(" - Not Possible!\n");
else
Add(a,b,r1,c1);
printf("\n Matrix Subtraction");
if(r1 != r2 || c1 != c2)
printf(" - Not Possible!\n");
else
Subtract(a,b,r1,c1);
printf("\n Matrix Multiplication");
if(c1 != r2)
printf(" - Not Possible!\n");
else
Multiply(a,b,r1,c1,r2,c2);
printf("\n Transpose of Matrix 1");
Transpose(a,r1,c1);
}
void Input(int mat[][10],int r, int c)
{
int i,j;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("\n Enter Value for Row %d Col %d : ",i,j);
scanf("%d",&mat[i][j]);
}
}
}
void Display(int a[][10],int r, int c)
{
int i,j;
printf("\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%4d",a[i][j]);
}
- 10 -
Programming in C Lab
Sub Code:09BCSM201
printf("\n");
}
}
void Add(int a[][10], int b[][10],int r, int c)
{
int res[10][10],i,j;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
res[i][j] = a[i][j] + b[i][j];
}
}
Display(res,r,c);
}
void Subtract(int a[][10], int b[][10], int r, int c)
{
int res[10][10],i,j;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
res[i][j] = a[i][j] - b[i][j];
}
}
Display(res,r,c);
}
void Multiply(int a[][10],int b[][10], int r1, int c1, int r2, int c2)
{
int res[10][10],i,j,k;
for(i=1;i<=r1;i++)
{
for(j=1;j<=c2;j++)
{
res[i][j] = 0;
for(k=1;k<=c1;k++)
{
res[i][j] += a[i][k] * b[k][j];
}
}
}
Display(res,r1,c2);
}
void Transpose(int a[][10],int r, int c)
{
int res[10][10],i,j;
- 11 -
Programming in C Lab
Sub Code:09BCSM201
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
res[j][i] = a[i][j];
}
}
Display(res,c,r);
}
Sample Output:
Values of Matrix 1
Matrix 1
1 2
3 4
Matrix 2
1 2
3 4
- 12 -
Programming in C Lab
Sub Code:09BCSM201
Matrix Addition
2 4
6 8
Matrix Subtraction
0 0
0 0
Matrix Multiplication
7 10
15 22
Transpose of Matrix 1
1 3
2 4
Result:
- 13 -
Programming in C Lab
Sub Code:09BCSM201
Program – 6
Palindrome
Aim:
To write a C program to check whether the given string is palindrome or not
Algorithm:
1. Start the process
2. Read the string
3. Find the length of the string
4. Reverse the string and compare it with the actual one.
4.1. If both the strings are equal, then display “The given string is a Palindrome”
4.2. Otherwise Display “The given string is not a Palindrome”
5. Stop the process
Program:
#include<stdio.h>
#include<string.h>
main()
{
char str1[25],str2[25];
int i,j,len;
system("clear");
printf("\n Enter The String\n");
scanf("%[^\n]",str1);
len = strlen(str1);
str2[len] = '\0';
j = len-1;
for(i=0;i<len;i++)
{
str2[i] = str1[j--];
}
printf("\n The Reversed String is\n %s\n",str2);
if(strcmp(str1,str2) == 0)
printf("\n The Given String is a Palindrome..\n");
else
printf("\n The Given String is not a Palindrome!!\n");
}
- 14 -
Programming in C Lab
Sub Code:09BCSM201
Sample Output:
Output 1:
Output 2:
Result:
Thus the program to check whether the given string is palindrome or not is
executed successfully.
- 15 -
Programming in C Lab
Sub Code:09BCSM201
Program – 7
Counting on String
Aim:
To write a C program to count the number of vowels, consonants, words, white
spaces in a line of text.
Algorithm:
Program:
#include<stdio.h>
main()
{
char str[50];
int i=0,cnt=0,words=1,vowels=0,cons=0,ws=0;
printf("\n Enter the Text\n");
scanf("%[^\n]",str);
while(str[i] != '\0')
{
cnt++;
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
vowels++;
else
cons++;
if(str[i] == ' ')
- 16 -
Programming in C Lab
Sub Code:09BCSM201
{
ws++;
words++;
cons--;
}
i++;
}
printf("\n Number of Characters : %d",cnt);
printf("\n Number of Words : %d",words);
printf("\n Number of White Spaces : %d",ws);
printf("\n Number of Vowels : %d",vowels);
printf("\n Number of Consonants : %d\n",cons);
}
Sample Output:
Number of Characters : 21
Number of Words : 5
Number of White Spaces : 4
Number of Vowels : 6
Number of Consonants : 11
Result:
Thus the program to count the number of vowels, consonants, words, white
spaces is executed successfully.
- 17 -
Programming in C Lab
Sub Code:09BCSM201
Program – 8
Binomial Coefficient
Aim:
To write a C program to find the Binomial Coefficient using Recursion.
Algorithm:
r! (n-r)!
4. Display the result
5. Stop the process
Program:
#include <stdio.h>
main ()
{
int n,r;
printf ("\nEnter Value for n : ");
scanf ("%d", &n);
printf ("\nEnter Value for r : ");
scanf ("%d", &r);
printf ("Binomial Coefficient : %d\n", binom (n, r));
}
- 18 -
Programming in C Lab
Sub Code:09BCSM201
}
}
Sample Output:
Binomial Coefficient : 10
Result:
Thus the program to find the Binomial Coefficient is executed successfully.
- 19 -
Programming in C Lab
Sub Code:09BCSM201
Program – 9
Personal Information
Aim:
To write a C program to store and print the personal information using structure.
Algorithm:
1. Start the process
2. Create a structure with members as name, age and city
3. Read the personal information and store it in the structure variable
4. Display the information present in the structure variable
5. Stop the process
Program:
#include<stdio.h>
struct info
{
char name[25];
int age;
char native[20];
};
void Display(struct info);
main()
{
struct info st;
printf("\n Personal Information\n");
printf("\n Enter your Name : ");
scanf("%s",st.name);
printf("\n Enter Your Age : ");
scanf("%d",&st.age);
printf("\n Enter Your Native City : ");
scanf("%s",&st.native);
Display(st);
}
void Display(struct info st)
{
printf("\n Personal Information\n");
printf("\n Name : %s\n Age : %d\n Native : %s\n",st.name,st.age,st.native);
}
- 20 -
Programming in C Lab
Sub Code:09BCSM201
Sample Output:
Personal Information
Personal Information
Name : XXXX
Age : 21
Native : YYYY
Result:
Thus the program to store and print the personal information using structure is
executed successfully.
- 21 -
Programming in C Lab
Sub Code:09BCSM201
Program – 10
Swapping Using Pointers
Aim:
To write a C program to swap values of two variables using pointers
Algorithm:
1. Start the process
2. Design the Swap() function which receives address of two variables as input
3. Read the values into variables
4. Send the address of the variables to the Swap() function
5. Exchange the values of variables using the following steps in Swap() function
temp = *p1
*p1 = *p2
*p2 = temp
6. Display the exchanged values
7. Stop the process
Program:
#include<stdio.h>
void Swap(int *, int *);
main()
{
int v1,v2;
printf("\n Enter Two Integers\n”);
scanf("%d %d",&v1, &v2);
printf("\nBefore Swapping\n");
printf("Value 1 : %5d , Value 2 : %5d\n",v1,v2);
Swap(&v1,&v2);
printf("\nAfter Swapping\n");
printf("Value 1 : %5d , Value 2 : %5d\n",v1,v2);
}
void Swap(int *p1, int *p2){
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
- 22 -
Programming in C Lab
Sub Code:09BCSM201
Sample Output:
Before Swapping
Value 1 : 24 , Value 2 : 32
After Swapping
Value 1 : 32 , Value 2 : 24
Result:
Thus the program to swap values of two variables using pointers is executed
successfully.
- 23 -
Programming in C Lab
Sub Code:09BCSM201
Program – 11
Student Mark Sheet Preparation
Aim:
To write a C program to manage mark statement of a class in file
Algorithm:
Program:
#include<stdio.h>
struct stud
{
char name[33];
int mark[3];
};
main()
{
FILE *p;
struct stud tmp;
int n,i,j ;
p=fopen("student.txt","a");
printf("Enter The Number of Students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter NAME of student\n\n");
scanf("%s",tmp.name);
- 24 -
Programming in C Lab
Sub Code:09BCSM201
fprintf(p,"%s ",tmp.name);
printf("Enter marks\n\n");
for(j=0;j<=2;j++)
{
scanf("%d",&tmp.mark[j]);
fprintf(p,"%d ",tmp.mark[j]);
}
}
fclose(p);
p=fopen("student.txt","r");
printf("\tName Mark1 Mark2 Mark3\n");
while(fscanf(p,"%s%d%d
%d",&tmp.name,&tmp.mark[0],&tmp.mark[1],&tmp.mark[2]) != EOF)
{
printf("%-20s%5d %5d %5d\n", tmp.name, tmp.mark[0], tmp.mark[1],
tmp.mark[2]);
}
Sample Output:
Ram
Enter marks
34 23 23
Result:
Thus the program to manage mark statement of a class in file is executed
successfully.
- 25 -
Programming in C Lab
Sub Code:09BCSM201
Program – 12
fseek and ftell Functions
Aim:
To write a C Program to find the character stored in the file based on the position
given by the user
Algorithm:
Program:
#include<stdio.h>
main()
{
FILE *fp;
int n,choice;
char c;
fp = fopen("test.txt","w");
printf("\nEnter The Text\n");
while((c=getchar()) != '\n')
putc(c,fp);
printf("No. of Characters in the file : %d",ftell(fp));
fclose(fp);
fp = fopen("test.txt","r");
do
{
- 26 -
Programming in C Lab
Sub Code:09BCSM201
printf("\n\n Enter the Position of Text : ");
scanf("%d",&n);
fseek(fp,n-1,0);
c = getc(fp);
printf("\n The Character in Position %d is %c",n,c);
printf("\n Do You Want to Continue(1 -> Yes 0 -> No) : ");
scanf("%d",&choice);
}while(choice == 1);
}
Sample Output:
Result:
Thus the program to find the character using its position in file is executed
successfully.
- 27 -