You are on page 1of 61

EX.No.

: 1 PROGRAM USING I/O STATEMENTS AND


DATE : EXPRESSIONS

a) Write a C Program to find sum of Odd Number, sum of Even Number and absolutely Difference
between Sum of Number and sum of Even Number.
AIM
To write a C Program to perform I/O statements and expressions.
ALGORITHM
Step-1: Start
Step-2: Declare variables and initializations
Step-3: Read the Input variable.
Step-4: Using I/O statements and expressions for computational processing.
Step-5: Display the output of the calculations.
Step-6: Stop
PROGRAM
/*
* Sum the odd and even numbers, respectively, from 1 to a given upperbound.
* Also compute the absolute difference.
* (SumOddEven.c)
*/
#include <stdio.h> // Needed to use IO functions
int main() {
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int upperbound; // Sum from 1 to this upperbound
int absDiff; // The absolute difference between the two sums
int number = 1;
// Prompt user for an upperbound
printf("Enter the upper bound: ");
scanf("%d", &upperbound); // Use %d to read an int
// Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
while (number <= upperbound) {
if (number % 2 == 0) { // Even number
sumEven += number; // Add number into sumEven

1
} else { // Odd number
sumOdd += number; // Add number into sumOdd
}
++number; // increment number by 1
}
// Compute the absolute difference between the two sums
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// Print the results
printf("The sum of odd numbers is %d.\n", sumOdd);
printf("The sum of even numbers is %d.\n", sumEven);
printf("The absolute difference is %d.\n", absDiff);
getch();
}

OUTPUT
Enter the upper bound: 1000
The sum of odd numbers is 250000.
The sum of even numbers is 250500.
The absolute difference is 500.

RESULT:
Thus a C Program using i/o statements and expressions was executed and the output was obtained.

2
b) For a given product with a VAT OF 7%,find the VAT amount and the total value of the product.

AIM:

To find the VAT amount and the total value of the product.

ALGORITHM:

Step-1: Start the program.


Step-2: Read the item Details.
Step-3: Read discount and tax % details.
Step-4: Calculate amount qty x value.
Step-5: Calculate discount (amount x discount)/100.
Step-6: Calculate taxamt (subtot x tax)100
Step-7: Calculate totamt (subtot + taxamt)
Step-8: Print all Details
Step-9: Stop the Program
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
floatquantity, value, total, subtotal, discount, discountamount, tax, taxamount, amount;
clrscr();
printf(“\n enter the quantity of item sold:”);
scanf(“%f”,&quantity);
printf(“\n enter the value of item:”);
scanf(“%f”,&value);
printf(“\n enter the discount percentage:”);
scanf(“%f”,&discount);
printf(“\n enter the tax:);
scanf(“%f”,&tax);
amount=quantity*value;
discountamount=(amount*discount)/100.0;
subtotal=amount-discountamount;
taxamount=(subtotal*tax)/100.0;
total=subtotal+taxamount;
printf(“\n\n *** bill *** \n\n”);

3
printf(“quantity sold:%f”, quantity);
printf(“\nprice per item:%f”,value);
printf(“\n amount:%f”, amount);
printf(“\n discount:- %f”,discountamount);
printf(“\n discount total:%f”,subtotal);
printf(“\n tax: +%f”,taxamount);
printf(“\n total amount:%f”,total);
getch();
}

OUT PUT:
Enter the quantity of item sold: 100

Enter the value of item: 10

Enter the discount percentage: 20

Enter the tax: 14

*** BILL ***

Quantity sold: 100

Price per item: 10

Amount: 1000

Discount: -200

Discount Total: 800

Tax: +112

Total Amount: 912

RESULT:

Thus the program for a given product with a VAT OF 7% to find the VAT Amount and the total value of
the product was executed and the output was verified.

4
c) Write a C Program to find area and perimeter of Circle.
AIM:
To write a C Program to find area and perimeter of Circle.
ALGORITHM:
Step-1: Start the program.
Step-2: Read radius.
Step-3: Area P1 * Radius * Radius , PI=3.14
Step-4: Perm 2 * PI * Radius
Step-5: Print area Prem
Step-6: Stop.
PROGRAM:
/*C program to find area and perimeter of circle.*/

#include <stdio.h>

#define PI 3.14f

int main()

float rad,area, perm;

printf("Enter radius of circle: ");

canf("%f",&rad);

area=PI*rad*rad;

perm=2*PI*rad;

printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perm);

return 0;

OUTPUT:
Enter radius of circle: 2.34
Area of circle: 17.193384
Perimeter of circle: 14.695200

RESULT:
Thus the program to find area and perimeter of Circle was executed and the output was verified.

5
EX.No. : 2
PROGRAM USING DECISION-MAKING CONSTRUCTS
DATE :

a) Write a C program Employee Salary Calculating using Decision Making Constructs


AIM
To write a C Program to perform decision-making constructs.
ALGORITHM
Step-1: Start
Step-2: Declare variables and initializations
Step-3: Read the Input variable.
Step-4: Codes are given to different categories and da is calculated as follows:
For code 1,10% of basic salary.
For code 2, 15% of basic salary.
For code 3, 20% of basic salary.
For code >3 da is not given.
Step-5: Display the output of the calculations .
Step-6: Stop
PROGRAM
#include <stdio.h>
#include<conio.h>
void main ()
{
float basic , da , salary ;
int code ;
char name[25];
da=0.0;
printf("Enter employee name\n");
scanf("%[^\n]",name);
printf("Enter basic salary\n");
scanf("%f",&basic);
printf("Enter code of the Employee\n");
scanf("%d",&code);
switch (code)
{

6
case 1:
da = basic * 0.10;
break;
case 2:
da = basic * 0.15;
break;
case 3:
da = basic * 0.20; break;
default :
da = 0;
}
salary = basic + da;
printf("Employee name is\n");
printf("%s\n",name);
printf ("DA is %f and Total salary is =%f\n",da, salary);
getch();
}
OUTPUT
Enter employee name
sriram
Enter basic salary
5000
Enter code of the Employee
1
Employee name is
sriram
DA is 500.000000 and Total salary is =5500.000000

RESULT
Thus a C Program using decision-making constructs was executed and the output was obtained

7
b) Write a C Program to Find the Eligibility of a Person to Vote or Not?
AIM:
To Write a C Program to find the eligibility of a Person to Vote or not?
ALGORITHM:
Step-1: Start the program.
Step-2: Read name, age.
Step-3: IF age>=18 print “Eligible”.
Step-4: ELSE print “Not Eligible”.
Step-5: ENDIF.
Step-6: Stop the Program.
PROGRAM:
#include<stdio.h> #include<conio.h>
void main()
{
int age;
char name[50]; // or *name
clrscr();
printf("\n Type the Name of the candidate: ");
gets(name);
printf("\n Enter The Age : ");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligible for Vote",name);
else
printf("\n %s is Not Eligible for Vote",name);
getch();
}
OUTPUT:
Type the Name of the candidate: ARUN
Enter The Age : 12
ARUN is Not Eligible for Vote

RESULT:
Thus the program to find the eligibility of a Person to Vote or not was executed and the output was verified.
c) Write a C Program to find the Largest Numbers Among Three Numbers.

8
AIM:
To Write a C Program to find the Largest Numbers among Three Numbers.
ALGORITHM:
Step-1: Start the program.
Step-2: Read a, b ,c.
Step-3: IF(a>b and a>c) big = a.
Step-4: ELSEIF (b>a and b>c) big =b.
Step-5: ELSE big = c.
Step-6: ENDIF.
Step-7: print big.
Step-8: Stop the Program.
PROGRAM:
#include <stdio.h> #include<conio.h>
int main()
{
int a,b,c; int big;
clrscr();
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
big=a;
else if(b>a && b>c)
big=b;
else
big=c;
printf("Largest number is = %d",big);
getch(); }
OUTPUT:
Enter three numbers: 30 40 10
Largest number is = 40

RESULT:
Thus the program to find the Largest Numbers among Three Numbers was executed and the output was
verified.

9
EX. No. : 3(a) CALCULATE THE SUM OF FIRST N NATURAL NUMBERS USING
DATE : FOR LOOP

AIM
To write a C Program to calculate the sum of first n natural numbers.
ALGORITHM
Step-1: Start
Step-2: Declare variables num, count and sum
Step-3: Read the Input.
Step-4: Using for loop statements to sum=sum + count,
Step-5: Display the output of the calculations.
Step-6: Stop
PROGRAM
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main() {
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count) {
sum += count; }
printf("Sum = %d", sum);
return 0; }

OUTPUT
Enter a positive integer: 10
Sum = 55

RESULT
Thus a C Program to calculate the sum of first n natural numbers was executed and the output was
obtained.

10
EX.No. : 3(b)
ARMSTRONG NUMBER USING WHILE LOOP
DATE :

AIM
To write a C Program to Check whether a given number is Armstrong number or not .
ALGORITHM
Step-1: Start
Step-2: Declare variables
Step-3: Read the Input number.
Step-4: Calculate sum of cubic of individual digits of the input.
Step-5: Match the result with input number.
Step-6: If match, Display the given number is Armstrong otherwise not.
Step-7: Stop
PROGRAM
#include<stdio.h> #include<conio.h> #include<math.h>
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
clrscr();
printf ("enter a number"); scanf("%d", &number);
temp = number;
while (number != 0)
{
rem = number % 10;
cube = pow(rem, 3);
sum = sum + cube;
number = number / 10;
}
if (sum == temp)
printf ("The given no is armstrong no");
else
printf ("The given no is not a armstrong no");
getch();
}

11
OUTPUT
Enter a number 370
The given no is armstrong no

RESULT:
Thus a C Program for Armstrong number checking was executed and the output was obtained.

12
EX.No. : 3(c) CALCULATE THE SUM OF FIRST N NATURAL NUMBERS USING
DATE : DO- WHILE LOOP

AIM
To write a C Program to calculate the sum of first n natural numbers using while loop.
ALGORITHM
Step-1: Start
Step-2: Declare variables num, i and sum
Step-3: Read the Input.
Step-4: Using while loop statements: while(i<=num) to sum=sum+count,
Step-5: Display the output of the calculations.
Step-6: Stop

PROGRAM
#include<stdio.h>
#include<stdlib.h>
void main ()
{
char c;
int choice,dummy;
do{
printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Hello");
break;
case 2:
printf("Javatpoint");
break;
case 3:
exit(0);
break;

13
default:
printf("please enter valid choice");
}
printf("do you want to enter more?");
scanf("%d",&dummy);
scanf("%c",&c);
}while(c=='y');
}

Output:
1. Print Hello
2. Print Javatpoint
3. Exit
1
Hello
do you want to enter more?
y

1. Print Hello
2. Print Javatpoint
3. Exit
2
Javatpoint
do you want to enter more?
n

RESULT:
Thus a C Program for sum of first n natural numbers using do-while loop was executed and the output
was obtained.

14
EX.No. : 4(a)
SORT THE NUMBERS BASED ON THE WEIGHT
DATE :
AIM:
To write a C Program to perform the following: Given a set of numbers like <10, 36, 54, 89, 12, 27>, find
sum of weights based on the following conditions
5 if it is a perfect cube
4 if it is a multiple of 4 and divisible by 6
3 if it is a prime number
Sort the numbers based on the weight in the increasing order as shown below <10,its weight>, <36,its
weight> , <89,its weight>.
ALGORITHM:
Step-1: Start
Step-2: Declare variables
Step-3: Read the number of elements .
Step-4: Get the individual elements.
Step-5: Calculate the weight for each element by the conditions
Step-5.1: if it is a perfect cube (pow)
Step-5.2: if it is a multiple of 4 and divisible by 6 (modulus operator)
Step-5.3: if it is a prime number(modulus operator)
Step-6: Display the output of the weight calculations after sorting .
Step-7: Stop
PROGRAM:
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
int nArray[50],wArray[50],nelem,i,j,t;
clrscr();
printf("\nEnter the number of elements in an array : ");
scanf("%d",&nelem);
printf("\nEnter %d elements\n",nelem);
for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);

15
//Calculate the weight
for(i=0; i<nelem; i++)
{
wArray[i] = 0;
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;
if(nArray[i]%4==0 && nArray[i]%6==0)
wArray[i] = wArray[i] + 4;
if(prime(nArray[i]))
wArray[i] = wArray[i] + 3;
}
// Sorting an array
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(wArray[i] > wArray[j])
{
t = wArray[i];
wArray[i] = wArray[j];
wArray[j] = t;
}
for(i=0; i<nelem; i++)
printf("<%d,%d>\n", nArray[i],wArray[i]);
getch();
}
int prime(int num)
{
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
{
flag=0;
break;
}
return flag;
}

16
int percube(int num)
{
int i,flag=0;
for(i=2;i<=num/2;i++)
if((i*i*i)==num)
{
flag=1;
break;
}
return flag;
}
OUTPUT
Enter the number of elements in an array :5
Enter 5 elements:
8
11
216
24
34
<34,0>
<11,3>
<24,4>
<8,5>
<216,9>
Explanation:
• 8 is a perfect cube of 2, not a prime number and not a multiple of 4 & divisible of 6 so the answer is 5
• 11 is a prime number so the answer is 3
• 216 is a perfect cube and multiple of 4 & divisible by 6 so the answer is 5+4 = 9
• 24 is not a perfect cube and not a prime number and multiple of 4 & divisible by 6 so the answer is 4
• 34 not satisfied all the conditions so the answer is 0

RESULT:
Thus a C Program for Sort the numbers based on the weight was executed and the output was obtained.

17
EX.No. : 4(b)
AVERAGE HEIGHT OF PERSONS
DATE :

AIM
To write a C Program to populate an array with height of persons and find how many persons are above
the average height.
ALGORITHM
Step-1: Start
Step-2: Declare variables
Step-3: Read the total number of persons and their height.
Step-4: Calculate avg=sum/n and find number of persons their h>avg.
Step-5: Display the output of the calculations .
Step-6: Stop
PROGRAM
/* Get a Height of Different Persons and find how many of them
are are above average */
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
clrscr();
//Read Number of persons
printf("Enter the Number of Persons : ");
scanf("%d",&n);
//Read the height of n persons
printf("\nEnter the Height of each person in centimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;

18
//Counting
for(i=0;i<n;i++)
if(height[i]>avg)
count++;
//display
printf("\nAverage Height of %d persons is : %.2f\n",n,avg);
printf("\nThe number of persons above average : %d ",count);
getch();
}

OUTPUT
Enter the Number of Persons : 5
Enter the Height of each person in centimeter
150
155
162
158
154
Average Height of 5 persons is : 155.8
The number of persons above average : 2

RESULT
Thus a C Program average height of persons was executed and the output was obtained.

19
EX.No. : 4(c)
BODY MASS INDEX OF THE INDIVIDUALS
DATE :

AIM
To write a C Program to Populate a two dimensional array with height and weight of persons and compute
the Body Mass Index of the individuals..
ALGORITHM
Step-1: Start
Step-2: Declare variables
Step-3: Read the number of persons and their height and weight.
Step-4: Calculate BMI=W/H2for each person
Step-5: Display the output of the BMI for each person.
Step-6: Stop
PROGRAM
#include<stdio.h>
#include<math.h>
int main(void){
int n,i,j;
printf("How many people's BMI do you want to calculate?\n");
scanf("%d",&n);
float massheight[n][2];
float bmi[n];
for(i=0;i<n;i++){
for(j=0;j<2;j++){
switch(j){
case 0:
printf("\nPlease enter the mass of the person %d in kg: ",i+1);
scanf("%f",&massheight[i][0]);
break;
case 1:
printf("\nPlease enter the height of the person %d in meter: ",i+1);
scanf("%f",&massheight[i][1]);
break;}
}

20
}
for(i=0;i<n;i++){
bmi[i]=massheight[i][0]/pow(massheight[i][1],2.0);
printf("Person %d's BMI is %f\n",i+1,bmi[i]);
}
return 0;
}

OUTPUT
How many people's BMI do you want to calculate?
2
Please enter the mass of the person 1 in kg: 88
Please enter the height of the person 1 in meter: 1.8288
Please enter the mass of the person 2 in kg:58
Please enter the height of the person 2 in meter: 2.2
Person 1's BMI is26.31178
Person 2's BMI is11.98347

RESULT
Thus a C Program Body Mass Index of the individuals was executed and the output was obtained.

21
EX.No. : 5 (a)
REVERSE OF A GIVEN STRING
DATE :

AIM
To write a C Program to perform reverse without changing the position of special characters for the given
string.
ALGORITHM
Step-1: Start
Step-2: Declare variables .
Step-3: Read a String.
Step-4: Check each character of string for alphabets or a special character by using isAlpha() .
Step-5: Change the position of a character vice versa if it is alphabet otherwise remains same.
Step-6: Repeat step 4 until reach to the mid of the position of a string.
Step-7: Display the output of the reverse string without changing the position of special characters
.
Step-8: Stop
PROGRAM
#include <stdio.h> #include <string.h> #include <conio.h>
void swap(char *a, char *b)
{
char t;
t = *a;
*a = *b;
*b = t;
}
// Main program
void main()
{
char str[100];
// Function Prototype
void reverse(char *);
int isAlpha(char);
void swap(char *a ,char *b);
clrscr();

22
printf("Enter the Given String : ");
// scanf("%[^\n]s",str);
gets(str);
reverse(str);
printf("\nReverse String : %s",str);
getch();
}
void reverse(char str[100])
{
// Initialize left and right pointers
int r = strlen(str) - 1, l = 0;
// Traverse string from both ends until
// 'l' and 'r'
while (l < r)
{
// Ignore special characters
if (!isAlpha(str[l]))
l++;
else if(!isAlpha(str[r]))
r--;
else
{
swap(&str[l], &str[r]);
l++;
r--;
}} }
// To check x is alphabet or not if it an alphabet then return 0 else 1
int isAlpha(char x)
{
return ( (x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z') );
}

23
OUTPUT
Enter the Given String :a@gh%;j
Reverse String :j@hg%;a

RESULT
Thus a C Program for reverse of a given String was executed and the output was obtained.

24
EX.No. : 5(b)
STRING OPERATIONS
DATE :

AIM:
To write a C Program to perform string operations on a given paragraph for the following using built-in
functions:
a. Find the total number of words.
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.

ALGORITHM:
Step-1 Start
Step-2 Declare variables
Step-3. Read the text.
Step-4. Display the menu options
Step-5. Compare each character with tab char „\t‟ or space char „ „ to count no of words
Step-6. Find the first word of each sentence to capitalize by checks to see if a character is a punctuation
mark used to denote the end of a sentence. (! . ?)
Step-7. Replace the word in the text by user specific word if match.
Step-8. Display the output of the calculations .
Step-9. Repeat the step 4 till choose the option stop.
Step-10. Stop

PROGRAM:

A. Find the total number of words.

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 100 // Maximum string size
void main()
{
char str[MAX_SIZE];
char prevChar;

25
int i, words;
clrscr();
/* Input string from user */
printf("\nEnter any string: ");
gets(str);
i = 0;
words = 0;
prevChar = '\0'; // The previous character of str[0] is null
/* Runs loop infinite times */
while(1)
{
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t' || str[i]=='\0')
{
/**
* It is a word if current character is whitespace and
* previous character is non-white space.
*/
if(prevChar != ' ' && prevChar != '\n' && prevChar != '\t' && prevChar != '\0')
{
words++;
}
}
/* Make the current character as previous character */
prevChar = str[i];

if(str[i] == '\0')
break;
else
i++;
}
printf("Total number of words = %d", words);
getch();
}

26
b. Capitalize the first word of each sentence.

#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
char str[MAX]={0};
int i;
clrscr();
//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces
//capitalize first character of words
for(i=0; str[i]!='\0'; i++)
{
//check first character is lowercase alphabet
if(i==0)
{
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
if(str[i]==' ')//check space
{
//if space is found, check next character
++i;
//check next character is lowercase alphabet

if(str[i]>='a' && str[i]<='z')


{
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}

27
}
else
{
//all other uppercase characters should be in lowercase
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32; //subtract 32 to make it small/lowercase
}
}
printf("Capitalize string is: %s\n",str);
getch();
}

c. Replace a given word with another word.

#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char s[] = "All work and no play makes Jack a dull boy.";
char word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
printf("All work and no play makes Jack a dull boy.\n");
printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
scanf("%s",word);
printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
scanf("%s",rpwrd);
p=strlen(s);
for (k=0; k<p; k++)
{
if (s[k]!=' ')
{
str[i][j] = s[k];
j++;
}

28
else
{
str[i][j]='\0';
j=0; i++;
}
}
str[i][j]='\0';
w=i;
for (i=0; i<=w; i++)
{
if(strcmp(str[i],word)==0)
strcpy(str[i],rpwrd);
printf("%s ",str[i]);
}
getch();
}
OUTPUT
Enter any text:
I like C and C++ programming!
1. Find the total number of words
2. Capitalize the first word of each sentence
3. Replace a given word with another word
4. Stop
Enter your choice : 1
Total number of words = 6
Press any key to continue....
1. Find the total number of words
2. Capitalize the first word of each sentence
3. Replace a given word with another word
4. Stop
Enter your choice : 4

RESULT
Thus a C Program String operations was executed and the output was obtained.

29
EX.No. : 6
TOWERS OF HANOI USING RECURSION
DATE :

AIM
To write a C Program to Solve towers of Hanoi using recursion.
ALGORITHM
Step-1: Start
Step-2: Declare variables
Step-3: Read the Input for number of discs.
Step-4: Check the condition for each transfer of discs using recursion.
Step-5: Display the output of the each move .
Step-6: Stop
PROGRAM
/*
Rules of Tower of Hanoi:
-Only a single disc is allowed to be transferred at a time.
-Each transfer or move should consist of taking the upper disk from one of the stacks and then placing it
on the top of another stack i.e. only a topmost disk on the stack can be moved.
-Larger disk cannot be placed over smaller disk; placing of disk should be in increasing order.
*/
#include <stdio.h>
#include <conio.h>
void towerofhanoi(int n, char from, char to, char aux)
{
if (n == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", from, to);
return;
}
towerofhanoi(n-1, from, aux, to);
printf("\n Move disk %d from peg %c to peg %c", n, from, to);
towerofhanoi(n-1, aux, to, from);
}
int main()

30
{
int n;
clrscr();
printf("Enter the number of disks : ");
scanf("%d",&n); // Number of disks
towerofhanoi(n, 'A', 'C', 'B'); // A, B and C are names of peg
getch();
return 0;
}

OUTPUT
Enter the number of disks : 3
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

RESULT
Thus a C Program Towers of Hanoi using Recursion was executed and the output was obtained.

31
EX.No. : 7
SORTING USING PASS BY REFERENCE
DATE :

AIM
To write a C Program to Sort the list of numbers using pass by reference.
ALGORITHM
Step-1. Start
Step-2. Declare variables and create an array
Step-3. Read the Input for number of elements and each element.
Step-4. Develop a function to sort the array by passing reference
Step-5. Compare the elements in each pass till all the elements are sorted.
Step-6. Display the output of the sorted elements .
Step-7. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void sortarray(int* arr,int num)
{

32
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

OUTPUT
Enter the Number of Elements in an array : 5
Enter the Array elements
33
67
21
45
11
After Sorting....
11
21
33
45
67

RESULT
Thus a C Program Sorting using pass by reference was executed and the output was obtained.

33
EX.No. : 8 GENERATE SALARY SLIP OF EMPLOYEES USING STRUCTURES
DATE : AND POINTERS

AIM:
To write a C Program to Generate salary slip of employees using structures and pointers.
ALGORITHM:
Step-1. Start
Step-2. Declare variables
Step-3. Read the number of employees .
Step-4. Read allowances, deductions and basic for each employee.
Step-5. Calculate net pay= (basic+ allowances)-deductions
Step-6. Display the output of the Pay slip calculations for each employee.
Step-7. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
nt 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) ;

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

OUTPUT
Enter the number of employees : 2
Enter the employee number : 101
Enter the name : Arun
Enter the basic pay, allowances & deductions : 5000 1000 250
Enter the employee number : 102
Enter the name : Babu
Enter the basic pay, allowances & deductions : 7000 1500 750
Emp.No. Name Bpay Allow Ded Npay
101 Arun 5000 1000 250 5750
102 Babu 7000 1500 750 7750

RESULT
Thus a C Program Salary slip of employees was executed and the output was obtained.

35
EX.No. : 9
INTERNAL MARKS OF STUDENTS
DATE :

AIM
To write a C Program to Compute internal marks of students for five different subjects using structures
and functions.
ALGORITHM
Step-1. Start
Step-2. Declare variables
Step-3. Read the number of students .
Step-4. Read the student mark details
Step-5. Calculate internal mark by i=total of three test marks / 3 for each subject per student.
Step-6. Display the output of the calculations for all the students .
Step-7. Stop
PROGRAM
#include<stdio.h> #include<conio.h>
struct stud{
char name[20];
long int rollno;
int marks[5,3];
int i[5]; }
students[10];
void calcinternal(int);
int main(){
int a,b,j,n;
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=0;a<n;++a){
clrscr();
printf("\n\nEnter the details of %d student : ", a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);

36
scanf("%ld", &students[a].rollno);
total=0;
for(b=0;b<=4;++b){
for(j=0;j<=2;++j){
printf("\n\nEnter the test %d mark of subject-%d : ",j+1, b+1);
scanf("%d", &students[a].marks[b,j]);
} }}
calcinternal(n);
for(a=0;a<n;++a){
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n------------------------------------------------------------------------");
for(b=0;b<5;b++){
printf("\n\n\t Subject %d internal \t\t :\t %d", b+1, students[a].i[b]);
}
printf("\n\n------------------------------------------------------------------------\n");
getch();
}
return(0);
}
void calcinternal(int n)
{
int a,b,j,total;
for(a=1;a<=n;++a){
for(b=0;b<5;b++){
total=0;
for(j=0;j<=2;++j){
total += students[a].marks[b,j];
}
students[a].i[b]=total/3;
}
}
}

37
OUTPUT
How many students : 1
Enter the details of 1 student :
Enter student 1 Name : H.Xerio
Enter student 1 Roll Number : 536435
Enter the test 1mark of subject-1 : 46
Enter the test 2 mark of subject-1 : 56
Enter the test 3 mark of subject-1 : 76
Enter the test 1 mark of subject-2 : 85
Enter the test 2mark of subject-2 : 75
Enter the test 3mark of subject-2 : 75
Enter the test 1mark of subject-3 : 66
Enter the test 2 mark of subject-3 : 86
Enter the test 3 mark of subject-3 : 70
Enter the test 1 mark of subject-4 : 25
Enter the test 2mark of subject-4 : 35
Enter the test 3mark of subject-4 : 61
Enter the test 1 mark of subject-5 : 45
Enter the test 2mark of subject-5 : 75
Enter the test 3mark of subject-5 : 60
Mark Sheet
Name of Student : H.Xerio Roll No : 536435
------------------------------------------------------------------------
subject 1 internal : 59
subject 2 internal : 78
subject 3 internal : 74
subject 4 internal : 40
subject 5 internal : 60
------------------------------------------------------------------------

RESULT
Thus a C Program for Internal marks of students was executed and the output was obtained.

38
EX.No. : 10
TELEPHONE DIRECTORY
DATE :

AIM
To write a C Program to add, delete ,display ,Search and exit options for telephone details of an individual
into a telephone directory using random access file.
ALGORITHM
Step-1. Start.
Step-2. Declare variables, File pointer and phonebook structures.
Step-3. Create menu options.
Step-4. Read the option .
Step-5. Develop procedures for each option.
Step-6. Call the procedure (Add, delete ,display ,Search and exit)for user chosen option.
Step-7. Display the message for operations performed.
Step-8. Stop
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Phonebook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
void AddEntry(phone * );
void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
int counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
int main (void)

39
{
phone *phonebook;
phonebook = (phone*) malloc(sizeof(phone)*100);
int iSelection = 0;
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
return 1;
}
else {}
do
{
printf("\n\t\t\tPhonebook Menu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(3)\tDisplay Phonebook Entries");
printf("\n\t(4)\tSearch for Phone Number");
printf("\n\t(5)\tExit Phonebook");
printf("\n\nWhat would you like to do? ");
scanf("%d", &iSelection);
if (iSelection == 1)
{
AddEntry(phonebook);
}
if (iSelection == 2)
{
DeleteEntry(phonebook);
}
if (iSelection == 3)
{
PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);

40
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
return 0;
}
} while (iSelection <= 4);
}
void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");
if ( pWrite == NULL )
{
perror("The following error occurred ");
exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName, phonebook[counter-1].LastName,
phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}
void DeleteEntry (phone * phonebook)
{
int x = 0;

41
int i = 0;
char deleteFirstName[20]; //
char deleteLastName[20];
printf("\nFirst name: ");
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
{
for ( i = x; i < counter - 1; i++ )
{
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
}
printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}
void PrintEntry (phone * phonebook)
{
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r");
if ( pRead == NULL)
{
perror("The following error occurred: ");

42
exit(EXIT_FAILURE);
}
else
{
for( x = 0; x < counter; x++)
{
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number: %s\n", phonebook[x].PhoneNumber);
}
}
fclose(pRead);
}
void SearchForNumber (phone * phonebook)
{
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
{
printf("\n%s %s's phone number is %s\n", phonebook[x].FirstName, phonebook[x].LastName,
phonebook[x].PhoneNumber);
}
}
}
}

43
OUTPUT
. Phonebook Menu
(1) Add Friend
(2) Delete Friend"
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? 1
First Name: Ram
Last Name: Mohan
Phone Number (XXX-XXX-XXXX): 717-675-0909
Friend successfully added to Phonebook
Phonebook Menu
(1) Add Friend
(2) Delete Friend"
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? 5
You have chosen to exit the Phonebook.

RESULT
Thus a C Program was executed and the output was obtained.

44
EX.No. : 11
Banking Application
DATE :

AIM
To write a C Program to Count the number of account holders whose balance is less than the minimum
balance using sequential access file.
ALGORITHM
Step-1. Start
Step-2. Declare variables and file pointer.
Step-3. Display the menu options.
Step-4. Read the Input for transaction processing.
Step-5. Check the validation for the input data.
Step-6. Display the output of the calculations .
Step-7. Repeat step 3 until choose to stop.
Step-8. Stop
PROGRAM
/* Count the number of account holders whose balance is less than the minimum balance using sequential
access file.
*/
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10];
char name[20];
char balance[15];
};
struct Bank_Account acc;
void main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt;
char choice;

45
int type,flag=0;
float bal;
do
{
clrscr();
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum Balance\n");
printf("5. Stop\n");
printf("Enter your choice : ");
choice=getchar();
switch(choice)
{
case '1' :
fflush(stdin);
fp=fopen("acc.dat","a");
printf("\nEnter the Account Number : ");
gets(acc.no);
printf("\nEnter the Account Holder Name : ");
gets(acc.name);
printf("\nEnter the Initial Amount to deposit : ");
gets(acc.balance);
fseek(fp,0,2);
fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case '2' :
fp=fopen("acc.dat","r");
if(fp==NULL)
printf("\nFile is Empty");
else
{
printf("\nA/c Number\tA/c Holder Name Balance\n");

46
while(fread(&acc,sizeof(acc),1,fp)==1)
printf("%-10s\t\t%-20s\t%s\n",acc.no,acc.name,acc.balance);
fclose(fp);
}
break;
case '3' :
fflush(stdin);
flag=0;
fp=fopen("acc.dat","r+");
printf("\nEnter the Account Number : ");
gets(ano);
for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;pos1=ftell(fp))
{
if(strcmp(acc.no,ano)==0)
{
printf("\nEnter the Type 1 for deposit & 2 for withdraw : ");
scanf("%d",&type);
printf("\nYour Current Balance is : %s",acc.balance);
printf("\nEnter the Amount to transact : ");
fflush(stdin);
gets(amt);
if(type==1)
bal = atof(acc.balance) + atof(amt);
else
{
bal = atof(acc.balance) - atof(amt);
if(bal<0)
{
printf("\nRs.%s Not available in your A/c\n",amt);
flag=2;
break;
}
}
flag++;
break;

47
}
}
if(flag==1)
{
pos2=ftell(fp);
pos = pos2-pos1;
fseek(fp,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(acc.balance,amt);
fwrite(&acc,sizeof(acc),1,fp);
}
else if(flag==0)
printf("\nA/c Number Not exits... Check it again");
fclose(fp);
break;
case '4' :
fp=fopen("acc.dat","r");
flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
bal = atof(acc.balance);
if(bal<MINBAL)
flag++;
}
printf("\nThe Number of Account Holder whose Balance less than the Minimum Balance : %d",flag);
fclose(fp);
break;
case '5' :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue....");
getch();
} while (choice!='5');
}

48
OUTPUT
1. Add a New Account Holder
2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop
Enter your choice : 1
Enter the Account Number : 547898760
Enter the Account Holder Name : Rajan
Enter the Initial Amount to deposit : 2000
Press any key to continue....
1. Add a New Account Holder
2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop
Enter your choice : 4
The Number of Account Holder whose Balance less than the Minimum Balance : 0

RESULT
Thus a C Program for Banking Application was executed and the output was obtained.
.

49
EX.No. : 12
Railway reservation system
DATE :

AIM
Create a Railway reservation system in C with the following modules
• Booking
• Availability checking
• Cancellation
• Prepare chart
.
ALGORITHM
Step-1. Start
Step-2. Declare variables
Step-3. Display the menu options
Step-4. Read the option.
Step-5. Develop the code for each option.
Step-6. Display the output of the selected option based on existence .
Step-7. Stop
PROGRAM
#include<stdio.h> #include<conio.h>
int first=5,second=5,thired=5;
struct node
{
int ticketno;
int phoneno;
char name[100];
char address[100];
}s[15];
int i=0;
void booking()
{
printf("enter your details");
printf("\nname:");
scanf("%s",s[i].name);

50
printf("\nphonenumber:");
scanf("%d",&s[i].phoneno);
printf("\naddress:");
scanf("%s",s[i].address);
printf("\nticketnumber only 1-10:");
scanf("%d",&s[i].ticketno);
i++;
}
void availability()
{
int c;
printf("availability cheking");
printf("\n1.first class\n2.second class\n3.thired class\n");
printf("enter the option");
scanf("%d",&c);
switch(c)
{
case 1:if(first>0)
{
printf("seat available\n");
first--;
}
else
{
printf("seat not available");
}
break;
case 2: if(second>0)
{
printf("seat available\n");
second--;
}
else
{
printf("seat not available");

51
}
break;
case 3:if(thired>0)
{
printf("seat available\n");
thired--;
}
else
{
printf("seat not available");
}
break;
default:
break;
}
}
void cancel()
{
int c;
printf("cancel\n");
printf("which class you want to cancel");
printf("\n1.first class\n2.second class\n3.thired class\n");
printf("enter the option");
scanf("%d",c);
switch(c)
{
case 1:
first++;
break;
case 2:
second++;
break;
case 3:
thired++;
break;

52
default:
break;
}
printf("ticket is canceled");
}
void chart()
{
int c;
for(c=0;c<I;c++)
{
printf(“\n Ticket No\t Name\n”);
printf(“%d\t%s\n”,s[c].ticketno,s[c].name)
}
}
main()
{
int n;
clrscr();
printf("welcome to railway ticket reservation\n");
while(1) {
printf("1.booking\n2.availability cheking\n3.cancel\n4.Chart \n5. Exit\nenter your option:");
scanf("%d",&n);
switch(n) {
case 1: booking(); break;
case 2: availability(); break;
case 3: cancel(); break;
case 4: chart(); break;
case 5:
printf(“\n Thank you visit again!”);
getch();
exit(0);
default:
break;
}}
getch(); }

53
OUTPUT
welcome to railway ticket reservation
1.booking
2.availability cheking
3.cancel
4.Chart
5. Exit
enter your option: 2
availability cheking
1.first class
2.second class
3.thired class
enter the option 1
seat available
1.booking
2.availability cheking
3.cancel
4.Chart
5. Exit
enter your option: 5
Thank you visit again!

RESULT
Thus a C Program for Railway reservation system was executed and the output was obtained.

54
Additional C Programs for exercise

1.Program to find the row sum and column sum of a given matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10];
int i,j; int m,n;
int sumrow,sumcol;
clrscr();
printf("\nTO FIND THE ROW SUM AND COLUMN SUM OF A GIVEN MATRIX:");
printf("\n-- ---- --- --- --- --- ------ --- -- - ----- ------:");
printf("\nEnter the order of matrix:");
scanf("%d%d",&m,&n);
printf("\nEnter elements of a matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&mat[i][j]);
}
printf("\n\nOUTPUT:");
printf("\n---------------------------------------");
for(i=0;i<m;i++)
{
sumrow=0;
for(j=0;j<n;j++)
sumrow=sumrow+mat[i][j];
printf("\nTHE SUM OF %d ROW IS %d",i+1,sumrow);
}
printf("\n---------------------------------------");
for(j=0;j<n;j++)

55
{
sumcol=0;
for(i=0;i<m;i++)
sumcol=sumcol+mat[i][j];
printf("\nTHE SUM OF %d COLUMN IS %d",j+1,sumcol);
}
printf("\n---------------------------------------");
getch();
}

2. Program to read a string and print the first two characters of each word in the string.

#include<stdio.h>
#include<conio.h>
void main( )
{
char s[100];
int i,l;
clrscr( );
printf(“Enter a string”);
gets(s);
l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]!=’ ‘ && s[i]=’ ‘)
{
printf(“%c %c”,s[i],s[i+1])
i=i+2;
while(s[i]!=’ ‘)
i++;
}}
getch( );

56
}
3. Program to print current system date.

#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main()
{
struct date d;
getdate(&d);
printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);
getch();
return 0;
}.
4. Program to calculate Standard Deviation.

#include <stdio.h>
#include <math.h>
float standard_deviation(float data[], int n);
int main()
{
int n, i;
float data[100];
printf("Enter number of datas( should be less than 100): ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0; i<n; ++i)
scanf("%f",&data[i]);
printf("\n");
printf("Standard Deviation = %.2f", standard_deviation(data,n));
return 0;
}
float standard_deviation(float data[], int n)
{
float mean=0.0, sum_deviation=0.0;

57
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);
return sqrt(sum_deviation/n);
}

5. Program to calculate the Power of a Number using Recursion.

#include <stdio.h>
int power(int n1,int n2);
int main()
{
int base, exp;
printf("Enter base number: ");
scanf("%d",&base);
printf("Enter power number(positive integer): ");
scanf("%d",&exp);
printf("%d^%d = %d", base, exp, power(base, exp));
return 0;
}
int power(int base,int exp)
{
if ( exp!=1 )
return (base*power(base,exp-1));
}

58
6. Program to find the ASCII value of a Character.
#include <stdio.h>
int main(){
char c;
printf("Enter a character: ");
scanf("%c",&c); /* Takes a character from user */
printf("ASCII value of %c = %d",c,c);
return 0;
}

7. Program to find biggest of four no by using ternary numbers.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,d,big;
clrscr( );
printf(“enter value a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
printf(“enter the value of c”);
scanf(“%d”,&c);
printf(“enter the value of d”);
scanf(“%d”,&d);
big=(a>b)?(a>c)?(a>d)?a:d:(c>d)?c:d:(b>c)?(b>d)?b:d:(c>d)?c:d;
printf(“Biggest of the given 4 numbers is %d”,big);
getch();
}

59
8. Program for temperature conversion from Celsius to Fahrenheit and vice versa.

#include<stdio.h>
main() {
float cel, fah ,c ,f;
clrscr();
printf(“\nEnter the fahrenheit value:”);
scanf(“%f”,&f);
cel=(5.0/9.0)*(f-32);
printf(“Celsius=%d”,cel);
printf(“\nEnter the Celsius value:”);
scanf(“%f”,&c);
fah=(9.0/5.0)*c+32;
printf(“Fahrenheit=%d”,fah);
getch(); }
9. Program to find the reverse and the given Number is Palindrome or not.

#include<stdio.h>
main() {
unsigned long int a, num, r_ num=0,rem;
printf(“\nEnter the number”);
scanf(“%ld”,&num);
a=num;
while(num!=0)
{
rem=num%10;
r_ num=r_ num*10+rem;
num=num/10;
}
printf(“\nThe reverse number of the %ld is %ld”,a,r_ num);
if(a==r_ num)
printf(“\nThe given number is a palindrome”);
else
printf(“\nThe given number is not a palindrome”);

60
}
10. Program to find nCr of a given number using recursive function.

#include<stdio.h>
void main()
{
long fact(int n),ncr;
int n,r;
clrscr();
printf("\nPRG TO FIND nCr USING RECURSION\n");
printf("\n-----------------------------------------------\n");
printf("\nEnter the value of n: ");
scanf("%d",&n);
printf("\nEnter the value of r: ");
scanf("%d",&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("\nThe value of %dC%d is %ld",n,r,ncr);
getch();
}

61

You might also like