Vous êtes sur la page 1sur 27

Programming in C Lab

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

Enter the Temperature in Fahrenheit : 105

The Celsius Equivalent : 40.56

Conversion
1. Fahrenheit -> Celsius
2. Celsius -> Fahrenheit
3. Exit
Enter Your Choice : 2

Enter the Temperature in Celcius : 40.56

The Fahrenheit Equivalent : 105.01

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:

Enter the Number : 259


The Given number is Not an Armstrong Number!

Enter the Number : 153


The Given number is an Armstrong Number..

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:

1. Start the process.


2. Read the input values for a,b and c.
3. Find discriminant b2 – 4ac
3.1. If the value is less than or equal to 0, display that the roots are imaginary
3.2. Otherwise, find
root1 = (-b+sqrt(discriminant))/2a
root2 = (-b- sqrt(discriminant))/2a
4. Stop the process

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:

Input values of a,b&c


123
The roots are imaginary

Input values of a,b&c


253
Root 1: -1.00
Root 2: -1.50

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:

1. Start the process.


2. Read the list of values
3. Using linear sorting technique, sort the given list
4. Print the first number in the list as minimum number and last number as maximum
number
5. Stop the process

Program:

#include<stdio.h>

void Display(int a[],int n);

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

The Given List is


56 76 32 44

The Sorted List Is


32 44 56 76

The Smallest Number is 32


The Largest Number is 76

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:

1. Start the process.


2. Read the number of rows and columns for matrix a and b
3. Read the values for the matrices
4. Perform matrix addition using ci,j = ai,j + bi,j
5. Perform matrix subtraction using ci,j = ai,j – bi,j
6. Perform matrix multiplication using ci,j = ci,j + ai,j * bj,k
7. Perform Transpose of matrix a using ci,j = ai,j
8. Stop the process

Program:

#include<stdio.h>

void Input(int mat[][10],int r, int c);


void Add(int a[][10], int b[][10], int r, int c);
void Subtract(int a[][10], int b[][10], int r, int c);
void Multiply(int a[][10], int b[][10], int r1,int c1, int r2,int c2);
void Display(int a[][10],int r, int c);
void Transpose(int a[][10], int r, int c);

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:

Enter the Number of Rows & Columns for Matrix 1


22

Values of Matrix 1

Enter Value for Row 1 Col 1 : 1

Enter Value for Row 1 Col 2 : 2

Enter Value for Row 2 Col 1 : 3

Enter Value for Row 2 Col 2 : 4

Enter The Number of Rows & Columns for Matrix 2


22

Enter Value for Row 1 Col 1 : 1

Enter Value for Row 1 Col 2 : 2

Enter Value for Row 2 Col 1 : 3

Enter Value for Row 2 Col 2 : 4

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:

Thus the program to perform Matrix manipulation is executed successfully.

- 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:

Enter The String


malayalam

The Reversed String is


malayalam

The Given String is a Palindrome..

Output 2:

Enter The String


C program

The Reversed String is


margorp C

The Given String is not a Palindrome!!

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:

1. Start the process


2. Read the input as a line of text
3. Inspect each character until end of the line is reached
3.1. If the character is ‘a’, ’e’, ’i’, ’o’ or ’u’ then add 1 to vowels count, otherwise add
1 to consonants count
3.2. If the character is ‘ ‘ then add 1 to white spaces count and words count. Reduce 1
from consonants count
4. Add 1 to words count (to add the last word’s count)
5. Display the counts of vowels, consonants, white spaces and words
6. Stop the process

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:

Enter the Text


This is a sample text

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:

1. Start the process.


2. Read the inputs for n and r
3. Calculate the binomial coefficient using the formula:
n!
_________

r! (n-r)!
4. Display the result
5. Stop the process

Program:

#include <stdio.h>

int binom (int, int);

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));
}

int binom (int n, int r)


{
int n1,n2;
if (( r ==0 ) || (n== r))
return 1;
else
{
n1 = binom ( n - 1, r);
n2 = binom ( n - 1, r -1);
return n1 + n2;

- 18 -
Programming in C Lab
Sub Code:09BCSM201
}
}

Sample Output:

Enter Value for n : 5

Enter Value for r : 3

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

Enter your Name : XXXX

Enter Your Age : 21

Enter Your Native City : YYYY

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:

Enter Two Integers


24 32

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:

1. Start the process


2. Read the number of students
3. Open the file in append mode
4. Read the students name and marks and store it in that text file
5. Close the file
6. Open the file in read mode
7. Display the content in that file
8. Close the file
9. Stop the process

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:

Enter The Number of Students : 1


Enter NAME of student

Ram
Enter marks

34 23 23

Name Mark1 Mark2 Mark3


Moorthi 56 56 43
XXXX 45 67 87
moorthi 45 8 69
RAJA 15 69 87
Ram 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:

1. Start the process


2. Open a file in write mode
3. Read a line of text in terms of character by character and store it in the file
4. Use ftell() function to identify the number of characters stored in the file since the
position of the file pointer is at the end of file
5. Close the file and open it in read mode
6. Read the position from the user
7. Move the file pointer to the position mentioned by user using fseek() function
8. Read the character in the position of file pointer and display it
9. Stop the process

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:

Enter The Text


Hello world
No. of Characters in the file : 11

Enter the Position of Text : 7

The Character in Position 7 is w


Do You Want to Continue(1 -> Yes 0 -> No) : 1

Enter the Position of Text : 2

The Character in Position 2 is e


Do You Want to Continue(1 -> Yes 0 -> No) : 0

Result:
Thus the program to find the character using its position in file is executed
successfully.

- 27 -

Vous aimerez peut-être aussi