Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programming in C Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 164
At a glance
Powered by AI
The document discusses basic C programming concepts like data types, input/output operations, file handling etc. It provides examples to demonstrate the use of various statements and functions.

The data types discussed are integer, float, character, string etc. Formatting specifiers like %d, %f etc are used to print values of different data types.

In C, the fopen(), fread(), fwrite() functions are used to open, read and write files respectively. Binary files are used to store user-defined data structures.

JIS COLLEGE OF ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


BASIC COMPUTATION AND PRINCIPLES OF COMPUTER
PROGRAMMING (CS 291) LAB MANUAL

Introduction
/*1. Find out the output of the following program.*/
#include <stdio.h>
main()
{
int a, b;
printf("\n Enter two four digit numbers : ");
scanf("%2d %4d", &a, &b);
printf("\n The two numbers are : %d and %d", a, b);
return 0;
}
/*2. Write a program to demonstrate the use of printf statement to print values of variables of
different datatypes.*/
#include <stdio.h>
main()
{
// Declare and initialize variables
int num = 7;
float amt = 123.45;
char code = 'A';
double pi = 3.1415926536;
long int population_of_india = 10000000000;
char msg[] = "Hi";
// Print the values of variables
printf("\n NUM = %d \t AMT = %f \t CODE = %c \n PI = %e \t POPULATION OF INDIA = %ld \n MESSAGE =
%s", num, amt, code, pi, population_of_india, msg);
return 0;
}
/*3. Write a program to demonstrate the use of printf and scanf statements to read and print values
of variables of different data types.*/
#include <stdio.h>
main()
{
int num;
float amt;
char code;
double pi;
long int population_of_india;
char msg[10];
printf("\n Enter the value of num : ");
scanf("%d", &num);
printf("\n Enter the value of amt : ");
scanf("%f", &amt);
printf("\n Enter the value of pi : ");

scanf("%e", &pi);
printf("\n Enter the population of india : ");
scanf("%ld", &population_of_india);
printf("\n Enter the value of code : ");
scanf("%c", &code);
printf("\n Enter the message : ");
scanf("%s", msg);
printf("\n NUM = %d \n AMT = %f \n PI = %e \n POPULATION OF INDIA = %ld \n CODE = %c \n MESSAGE
= %s", num, amt, code, pi, population_of_india, msg);
return 0;
}
/*4. Write a program to calculate the area of a triangle using Heros formula.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
float a, b, c, area, S;
printf("\n Enter the lengths of the three sides of the triangle : ");
scanf("%f %f %f", &a, &b, &c);
S = ( a + b + c)/2; // sqrt is a mathematical function defined in math.h header file
area = sqrt(S*(S-a)*(S-b)*(S-c));
printf("\n Area = %f", area);
return 0;
}
/*5. Write a program to calculate the distance between two points.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int x1, x2, y1, y2;
float distance;
printf("\n Enter the x and y coordinates of the first point : ");
scanf("%d %d", &x1, &y1);
printf("\n Enter the x and y coordinates of the second point :");
scanf("%d %d", &x2, &y2); // sqrt and pow are mathematical functions defined in math.h header file
distance = sqrt(pow((x2-x1), 2)+pow((y2-y1), 2));
printf("\n Distance = %f", distance);
return 0;
}

/*6. Write a program to perform addition, subtraction, division, integer division, multiplication, and
modulo division on two integer numbers.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;
int add_res=0, sub_res=0, mul_res=0, idiv_res=0, modiv_res=0; float fdiv_res=0.0;
clrscr();
printf("\n Enter the first number : ");
scanf("%d", &num1);
printf("\n Enter the second number : ");
scanf("%d", &num2);
add_res= num1 + num2;
sub_res= num1 - num2;
mul_res = num1 * num2;
idiv_res = num1/num2;
modiv_res = num1%num2;
fdiv_res = (float)num1/num2;
printf("\n %d + %d = %d", num1, num2, add_res);
printf("\n %d - %d = %d", num1, num2, sub_res);
printf("\n %d %d = %d", num1, num2, mul_res);
printf("\n %d / %d = %d (Integer Division)", num1, num2, idiv_res);
printf("\n %d %% %d = %d (Moduluo Division)", num1, num2, modiv_res);
printf("\n %d / %d = %.2f (Normal Division)", num1, num2, fdiv_res);
return 0;
}
/*7. Write a program to perform addition, subtraction, division, and multiplication on two fl oating
point numbers.*/
#include <stdio.h>
#include <conio.h>
int main()
{
float num1, num2;
clrscr();
printf("\n Enter the first number: ");
scanf("%f", &num1);
printf("\n Enter the second number: ");
scanf("%f", &num2);
printf("\n %f + %f = %f", num1, num2, num1 + num2);
printf("\n %f - %f = %f", num1, num2, num1 - num2);
printf("\n %f X %f = %f", num1, num2, num1 * num2);
printf("\n %f / %f = %f ", num1, num2, num1 / num2);
return 0;
}

/*8. Write a program to subtract two long integers.*/


#include <stdio.h>
#include <conio.h>
int main()
{
long int num1= 1234567, num2, diff=0;
clrscr();
printf("\n Enter the number: ");
scanf("%ld", &num2);
diff = num1 - num2;
printf("\n Difference = %ld", diff);
return 0;
}
/*9. Write a program to show the use of relational operators.*/
#include <stdio.h>
main()
{
int x=10, y=20;
printf("\n %d < %d = %d", x, y, x<y);
printf("\n %d == %d = %d", x, y, x==y);
printf("\n %d != %d = %d", x, y, x!=y);
printf("\n %d > %d = %d", x, y, x>y);
printf("\n %d >= %d = %d", x, y, x>=y);
printf("\n %d <= %d = %d", x, y, x<=y);
return 0;
}
/*10. Write a program to illustrate the use of unary prefix increment and decrement operators.*/
#include <stdio.h>
main()
{
int num = 3;
// Using unary prefix increment operator
printf("\n The value of num = %d", num);
printf("\n The value of ++num = %d", ++num);
printf("\n The new value of num = %d", num);
// Using unary prefix decrement operator
printf("\n\n The value of num = %d", num);
printf("\n The value of --num = %d", --num);
printf("\n The new value of num = %d", num);
return 0;
}

/*11. Write a program to illustrate the use of unary postfix increment and decrement operators.*/
#include <stdio.h>
main()
{
int num = 3;
// Using unary postfix increment operator
printf("\n The value of num = %d", num);
printf("\n The value of num++ = %d", num++);
printf("\n The new value of num = %d", num);
// Using unary postfix decrement operator
printf("\n\n The value of num = %d", num);
printf("\n The value of num = %d", num--);
printf("\n The new value of num = %d", num);
return 0;
}
/*12. Write a program two fi nd the largest of two numbers using ternary operator.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, large;
clrscr();
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
large = num1>num2?num1:num2;
printf("\n The largest number is: %d", large);
return 0;
}
/*13. Write a program two fi nd the largest of three numbers using ternary operator.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, num3, large;
clrscr();
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
printf("\n Enter the third number: ");
scanf("%d", &num3);
large = num1>num2?(num1>num3?num1:num3):(num2>num3?num2:num3);
printf("\n The largest number is: %d", large);

return 0;
}
/*14. Write a program to demonstrate the use of assignment operators.*/
#include <stdio.h>
main()
{
int num1 = 3, num2 = 5;
printf("\n Initial value of num1 = %d and num2 = %d", num1, num2);
num1 += num2 * 4 - 7;
printf("\n After the evaluation of the expression num1 = %d and num2 = %d", num1, num2);
return 0;
}
/*15. Write a program to calculate the area of a circle.*/
#include <stdio.h>
#include <conio.h>
int main()
{
float radius;
double area, circumference;
clrscr();
printf("\n Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circumference = 2 * 3.14 * radius;
printf(" Area = %.2le", area);
printf("\n CIRCUMFERENCE = %.2e", circumference);
return 0;
}
/*16. Write a program to print the ASCII value of a character.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c", &ch);
printf("\n The ascii value of %c is: %d",ch,ch);
return 0;
}

/*17. Write a program to read a character in upper case and then print it in lower case.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
printf("\n Enter any character in uppercase: ");
scanf("%c", &ch);
printf("\n The character in lower case is: %c", ch+32);
return 0;
}

/*18. Write a program to print the digit at ones place of a number.*/


#include <stdio.h>
#include <conio.h>
main()
{
int num, digit_at_ones_place;
clrscr();
printf("\n Enter any number: ");
scanf("%d", &num);
digit_at_ones_place = num % 10;
printf("\n The digit at ones place of %d is %d", num, digit_at_ones_place);
return 0;
}
/*19. Write a program to swap two numbers using a temporary variable.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, temp;
clrscr();
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n Enter the second number: ");
scanf("%d",&num2);
temp = num1;
num1 = num2;
num2=temp;
printf("\n The first number is %d", num1);
printf("\n The second number is %d", num2);
return 0;
}

/*20. Write a program to swap two numbers without using a temporary variable.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;
clrscr();
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n Enter the second number: ");
scanf("%d",&num2);
num1 = num1 + num2;
num2= num1 - num2;
num1 = num1 - num2;
printf("\n The first number is %d", num1);
printf("\n The second number is %d", num2);
return 0;
}
/*21. Write a program to calculate average of two numbers. Also print their deviation.*/
#include <stdio.h>
#include <conio.h>
main()
{
int num1, num2;
float avg, dev1, dev2;
printf("\n Enter the two numbers: ");
scanf("%d %d", &num1, &num2);
avg = (num1 + num2) / 2;
dev1 = num1 - avg;
dev2 = num2 - avg;
printf("\n AVERAGE = %.2f", avg);
printf("\n Deviation of first number = %.2f", dev1);
printf("\n Deviation of second number = %.2f", dev2);
return 0;
}
/*22. Write a program to convert degrees Fahrenheit into degrees celsius.*/
#include <stdio.h>
#include <conio.h>
main()
{
float fahrenheit;
float celsius;
printf("\n Enter the temperature in fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (0.56) * (fahrenheit - 32);

printf("\n Temperature in degrees celsius = %f", celsius);


return 0;
}
/*23. Write a program that displays the size of every data type.*/
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
printf("\n The size of short integer is: %d", sizeof(short int));
printf("\n The size of unsigned integer is: %d", sizeof(unsigned int));
printf("\n The size of signed integer is: %d", sizeof(signed int));
printf("\n The size of integer is: %d",
sizeof(int));
printf("\n The size of long integer is: %d", sizeof(long int));
printf("\n The size of character is: %d", sizeof(char));
printf("\n The size of unsigned character is: %d", sizeof(unsigned char));
printf("\n The size of signed character is: %d", sizeof(signed char));
printf("\n The size of floating point number is: %d", sizeof(float));
printf("\n The size of double number is: %d", sizeof(double));
return 0;
}
/*24. Write a program to calculate the total amount of money in the piggybank, given the coins of Rs
10, Rs 5, Rs 2, and Re 1.*/
#include <stdio.h>
#include <conio.h>
main()
{
int num_of_10_coins, num_of_5_coins, num_of_2_coins, num_of_1_coins;
float total_amt = 0.0;
clrscr();
printf("\n Enter the number of Rs10 coins in the piggybank: ");
scanf("%d", &num_of_10_coins);
printf("\n Enter the number of Rs5 coins in the piggybank: ");
scanf("%d", &num_of_5_coins);
printf("\n Enter the number of Rs2 coins in the piggybank: ");
scanf("%d", &num_of_2_coins);
printf("\n Enter the number of Re1 coins in the piggybank: ");
scanf("%d", &num_of_1_coins);
total_amt = num_of_10_coins * 10 + num_of_5_coins * 5 + num_of_2_coins * 2 + num_of_1_coins;
printf("\n Total amount in the piggybank = %f", total_amt);
getch();
return 0;
}

/*25. Write a program to calculate the bill amount for an item given its quantity sold, value, discount,
and tax.*/
#include <stdio.h>
#include <conio.h>
main()
{
float total_amt, amt, sub_total,
discount_amt, tax_amt, qty, val,
discount, tax;
printf("\n Enter the quantity of item sold: ");
scanf("%f", &qty);
printf("\n Enter the value of item: ");
scanf("%f", &val);
printf("\n Enter the discount percentage: ");
scanf("%f", &discount);
printf("\n Enter the tax: ");
scanf("%f", &tax);
amt = qty * val;
discount_amt = (amt * discount)/100.0;
sub_total = amt - discount_amt;
tax_amt = (sub_total * tax) /100.0;
total_amt = sub_total + tax_amt;
printf("\n\n\n ****** BILL ******");
printf("\n Quantity Sold: %f", qty);
printf("\n Price per item: %f", val);
printf("\n -------------");
printf("\n Amount: %f", amt);
printf("\n Discount: - %f", discount_amt);
printf("\n Discounted Total: %f", sub_total);
printf("\n Tax: + %f", tax_amt);
printf("\n -------------");
printf("\n Total Amount %f", total_amt);
return 0;
}
/*26. Write a program to convert a floating point number into the corresponding integer.*/
#include <stdio.h>
#include <conio.h>
int main()
{
float f_num;
int i_num;
clrscr();
printf("\n Enter any floating point number: ");
scanf("%f", &f_num);
i_num = (int)f_num;
printf("\n The integer variant of %f is = %d", f_num, i_num);

return 0;
}
/*27. Write a program to convert an integer into the corresponding floating point number.*/
#include <stdio.h>
#include <conio.h>
int main()
{
float f_num;
int i_num;
clrscr();
printf("\n Enter any integer: ");
scanf("%d", &i_num);
f_num = (float)i_num;
printf("\n The floating point variant of %d is = %f", i_num, f_num);
return 0;
}
/*28. Write a program to calculate a students result based on two examinations, one sports event,
and three activities conducted. The weightage of activities = 30%, sports = 20%, and examination =
50%.*/
#include <stdio.h>
#include <conio.h>
#define ACTIVITIES_WEIGHTAGE 30
#define SPORTS_WEIGHTAGE 20
#define EXAMS_WEIGHTAGE 50
#define EXAMS_TOTAL 200
#define ACTIVITIES_TOTAL 60
#define SPORTS_TOTAL 50
main()
{
int exam_score1, activities_score1, sports_score;
int exam_score2, activities_score2, activities_score3;
float exam_total, activities_total;
float total_percent, exam_percent,
sports_percent, activities_percent;
clrscr();
printf("\n Enter the score obtained in two examination (out of 100): ");
scanf("%d %d", &exam_score1, &exam_score2);
printf("\n Enter the score obtained in sports events (out of 50): ");
scanf("%d", &sports_score);
printf("\n Enter the score obtained in three activities (out of 20): ");
scanf("%d %d %d", &activities_score1, &activities_score2, &activities_score3);
exam_total = exam_score1 + exam_score2;
activities_total = (activities_score1 + activities_score2 + activities_score3);
exam_percent = (float)exam_total * EXAMS_WEIGHTAGE / EXAMS_TOTAL;
sports_percent = (float)sports_score * SPORTS_WEIGHTAGE / SPORTS_TOTAL;
activities_percent = (float)activities_total * ACTIVITIES_WEIGHTAGE /ACTIVITIES_TOTAL;

total_percent = exam_percent + sports_percent + activities_percent;


printf("\n\n ***********************RESULT **************************");
printf("\n Total percent in examintaion : %f", exam_percent);
printf("\n Total percent in activities : %f", activities_percent);
printf("\n Total percent in sports : %f", sports_percent);
printf("\n -----------------------------------------------------------");
printf("\n Total percentage : %f", total_percent);
return 0;
}

Decision Control and Looping Statements


/*1. Write a program to determine whether a person is eligible to vote.*/
#include <stdio.h>
#include <conio.h>
main()
{
int age;
printf("\n Enter the age: ");
scanf("%d", "&age");
if(age >= 18)
printf("\n You are eligible to vote");
getch();
return 0;
}
/*2. Write a program to determine the character entered by the user.*/
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
main()
{
char ch;
printf("\n Press any key: ");
scanf("%c", &ch);
if(isalpha(ch)>0)
printf("\n The user has entered a character");
if(isdigit(ch)>0)
printf("\n The user has entered a digit");
if(isprint(ch)>0)
printf("\n The user has entered a printable character");
if(ispunct(ch)>0)
printf("\n The user has entered a punctuation mark");
if(isspace(ch)>0)
printf("\n The user has entered a white space character");
getch();
return 0;
}
/*3. Write a program to fi nd the largest of two numbers.*/
#include <stdio.h>
main()
{
int a, b, large;
printf("\n Enter the values of a and b: ");
scanf("%d %d", &a, &b);
if(a>b)
large = a;

else
large = b;
printf("\n LARGE = %d", large);
return 0;
}
/*4. Write a program to fi nd whether the given number is even or odd.*/
#include <stdio.h>
#include <conio.h>
main()
{
int num;
clrscr();
printf("\n Enter any number: ");
scanf("%d",&num);
if(num%2 == 0)
printf("\n %d is an even number", num);
else
printf("\n %d is an odd number", num);
return 0;
}
/*5. Write a program to enter any character. If the entered character is in lower case then convert it
into uppercase and if it is a lower case character then convert it into upper case.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c", &ch);
if(ch >='A' && ch<='Z')
printf("\n The entered character was in upper case. In lower case it is: %c", (ch+32));
else
printf("\n The entered character was in lower case. In upper case it is: %c", (ch-32));
return 0;
}
/*6. Write a program to enter a character and then determine whether it is a vowel or not.*/
#include <stdio.h>
#include <conio.h>
main()
{
char ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c", &ch);

if(ch ='a' ||ch =='e' ||ch=='i' ||ch=='o' ||ch=='u' || ch=='A' ||ch=='E' ||ch=='I' ||ch=='O' ||ch=='U' )
printf("\n %c is a VOWEL", ch);
else
printf("\n %c is not a vowel");
getch();
return 0;
}
/*7. Write a program to fi nd whether a given year is a leap year or not.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int year;
clrscr();
printf("\n Enter any year: ");
scanf("%d",&year);
if((year%4 == 0) && ((year%100 !=0) || (year%400 == 0)))
printf("\n Leap Year");
else
printf("\n Not A Leap Year");
return 0;
}
/*8. Write a program to demonstrate the use of nested if structure.*/
#include <stdio.h>
{
int x, y;
printf("\n Enter two numbers: ");
scanf("%d %d", &x, &y);
if(x == y)
printf("\n The two numbers are equal");
else if(x > y)
printf("\n %d is greater than %d", x, y);
else
printf("\n %d is less than %d", x, y);
return 0;
}
/*9. Write a program to test whether a number entered is positive, negative or equal to zero.*/
#include <stdio.h>
main()
{
int num;
printf("\n Enter any number: ");
scanf("%d", &num);
if(num==0)
printf("\n The value is equal to zero");

else if(num>0)
printf("\n The number is positive");
else
printf("\n The number is negative");
return 0;
}
/*10. A company decides to give bonus to all its employees on Diwali. A 5% bonus on salary is given to
the male workers and 10% bonus on salary to the female workers. Write a program to enter the salary
and sex of the employee. If the salary of the employee is less than Rs 10,000 then the employee gets
an extra 2% bonus on salary. Calculate the bonus that has to be given to the employee and display the
salary that the employee will get.*/
#include <stdio.h>
#include <conio.h>
main()
{
char ch;
float sal, bonus, amt_to_be_paid;
printf("\n Enter the sex of the employee (m or f): ");
scanf("%c", &ch);
printf("\n Enter the salary of the employee: ");
scanf("%f", &sal);
if(ch == 'm')
bonus = 0.05 * sal;
else
bonus = 0.10 * sal;
if (sal < 10000)
bonus += 0.20 * sal;
amt_to_be_paid = sal + bonus;
printf("\n Salary = %f", sal);
printf("\n Bonus = %f", bonus);
printf("\n ***************************");
printf("\n Amount to be paid: %f", amt_to_be_paid);
getch();
return 0;
}
/*11. Write a program to display the examination result.*/
#include <stdio.h>
main()
{
int marks;
printf("\n Enter the marks obtained: ");
scanf("%d", &marks);
if ( marks >= 75)
printf("\n DISTINCTION");
else if ( marks >= 60 && marks <75)
printf("\n FIRST DIVISION");

else if ( marks >= 50 && marks < 60)


printf("\n SECOND DIVISION");
else if ( marks >= 40 && marks < 50)
printf("\n THIRD DIVISION");
else
printf("\n FAIL");
return 0;
}
/*12. Write a program to calculate tax, given the following
conditions:
A) if income is less than 1,50, 000 then no tax
B) if taxable income is in the range 1,50,001300,000 then charge 10% tax
C) if taxable income is in the range 3,00,001500,000 then charge 20% tax
D) if taxable income is above 5,00,001 then charge 30% tax*/
#include <stdio.h>
#include <conio.h>
#define MIN1 150001
#define MAX1 300000
#define RATE1 0.10
#define MIN2 300001
#define MAX2 500000
#define RATE2 0.20
#define MIN3 500001
#define RATE3 0.30
main()
{
double income, taxable_income;
clrscr();
printf("\n Enter the income: ");
scanf("%lf", &income);
taxable_income = income - 150000;
if(taxable_income <= 0)
printf("\n NO TAX");
else if(taxable_income >= MIN1 && taxable_income < MAX1)
tax = (taxable_income - MIN1) * RATE1;
else if(taxable_income >= MIN2 && taxable_income < MAX2)
tax = (taxable_income - MIN2) * RATE2;
else
tax = (taxable_income - MIN3) * RATE3;
printf("\n TAX = %lf", tax);
getch();
return 0;
}

/*13. Write a program to find the greatest three numbers.*/


#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, num3, big=0;
clrscr();
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
printf("\n Enter the third number: ");
scanf("%d", &num3);
if(num1>num2)
{
if(num1>num3)
printf("\n %d is greater than %d and %d", num1, num2, num3);
else
printf("\n %d is greater than %d and %d", num3, num1, num2);
}
else if(num2>num3)
printf("\n %d is greater than %d and %d", num2, num1, num3);
else
printf("\n %d is greater than %d and %d", num3, num1, num2);
return 0;
}
/*14. Write a program to input three numbers and then find the largest of them using && operator.*/
#include <stdio.h>
#include <conio.h>
main()
{
int num1, num2, num3;
clrscr();
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
printf("\n Enter the third number: ");
scanf("%d", &num3);
if(num1>num2 && num1>num3)
printf("\n %d is the largest number", num1);
if(num2>num1 && num2>num3)
printf("\n %d is the largest number", num2);
else
printf("\n %d is the largest number", num3);
getch();

return 0;
}
/*15. Write a program to enter the marks of a student in four subjects. Then calculate the total,
aggregate, and display the grades obtained by the student.*/
#include <stdio.h>
#include <conio.h>
{
int marks1, marks2, marks3, marks4, total = 0;
float avg =0.0;
clrscr();
printf("\n Enter the marks in Mathematics: ");
scanf("%d", &marks1);
printf("\n Enter the marks in Science: ");
scanf("%d", &marks2);
printf("\n Enter the marks in Social Science: ");
scanf("%d", &marks3);
printf("\n Enter the marks in Computer Science: ");
scanf("%d", &marks4);
total = marks1 + marks2 + marks3 + marks4;
avg = total/4;
printf("\n Total = %d", total);
printf("\n Aggregate = %.2f", avg);
if(avg >= 75)
printf("\n DISTINCTION");
else if(avg>=60 && avg<75)
printf("\n FIRST DIVISION");
else if(avg>=50 && avg<60)
printf("\n SECOND DIVISION");
else if(avg>=40 && avg<50)
printf("\n THIRD DIVISION");
else
printf("\n FAIL");
return 0;
}
/*16. Write a program to calculate the roots of a quadratic equation.*/
#include <stdio.h>
#include <math.h>
void main()
{
int a, b, c;
float D, deno, root1, root2;
clrscr();
printf("\n Enter the values of a, b, and c :");
scanf("%d %d %d", &a, &b, &c);
D = (b * b) - (4 * a * c);
deno = 2 * a;

if(D > 0)
{
printf("\n REAL ROOTS");
root1 = (-b + sqrt(D)) / deno;
root2 = (-b - sqrt(D)) / deno;
printf("\n ROOT1 = %f \t ROOT 2 = %f",
root1, root2);
}
else if(D == 0)
{
printf("\n EQUAL ROOTS");
root1 = -b/deno;
printf("\n ROOT1 = %f \t ROOT 2 = %f",
root1, root2);
}
else
printf("\n IMAGINARY ROOTS");
getch();
}
/*17. Write a program to demonstrate the use of switch statement without a break.*/
#include <stdio.h>
main()
{
int option = 1;
switch(option)
{
case 1: printf("\n In case 1");
case 2: printf("\n In case 2");
default: printf("\n In case default");
}
return 0;
}
/*18. Write a program to determine whether an entered character is a vowel or not.*/
#include <stdio.h>
int main()
{
char ch;
printf("\n Enter any character: ");
scanf("%c", &ch);
switch(ch)
{
case 'A':
case 'a':
printf("\n % c is VOWEL", ch);
break;
case 'E':

case 'e':
printf("\n % c is VOWEL", ch);
break;
case 'I':
case 'i':
printf("\n % c is VOWEL", ch);
break;
case 'O':
case 'o':
printf("\n % c is VOWEL", ch);
break;
case 'U':
case 'u':
printf("\n % c is VOWEL", ch);
break;
default: printf("%c is not a vowel", ch);
}
return 0;
}
/*19. Write a program to enter a number from 17 and display the corresponding day of the week
using switch case statement.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int day;
clrscr();
printf("\n Enter any number from 1 to 7: ");
scanf("%d",&day);
switch(day)
{
case 1: printf("\n SUNDAY");
break;
case 2: printf("\n MONDAY");
break;
case 3: printf("\n TUESDAY");
break;
case 4: printf("\n WEDNESDAY");
break;
case 5: printf("\n THURSDAY");
break;
case 6: printf("\n FRIDAY");
break;
case 7: printf("\n SATURDAY");
break;
default: printf("\n Wrong Number");
}

return 0;
}
/*20. Write a program that accepts a number from 1 to 10. Print whether the number is even or odd
using a switch case construct.*/
#include <stdio.h>
main()
{
int num;
printf("\n Enter any number (1 to 10): ");
scanf("%d", &num);
switch(num)
{
case 1:
case 3:
case 5:
case 7:
case 9:
printf("\n ODD");
break;
case 2:
case 4:
case 6:
case 8:
case 10:
printf("\n EVEN");
default :
printf("\n INVALID INPUT");
break;
}
return 0;
}
/*20. Write a program that accepts a number from 1 to 10. Print whether the number is even or odd
using a switch case construct.*/
#include <stdio.h>
main()
{
int num, rem;
printf("\n Enter any number (1 to 10): ");
scanf("%d", &num);
rem = num%2;
switch(rem)
{
case 0:
printf("\n EVEN");
break;

case 1:
printf("\n ODD");
break;
}
return 0;
}
/*21. Write a program to calculate the sum of first 10 numbers.*/
#include <stdio.h>
int main()
{
int i = 0, sum = 0;
while(i<=10)
{
sum = sum + i;
i = i + 1; // condition updated
}
printf("\n SUM = %d", sum);
return 0;
}
/*22. Write a program to print 20 horizontal asterisks(*).*/
#include <stdio.h>
main()
{
int i=1;
while (i<=20)
{
printf("*");
i++;
}
return 0;
}
/*23. Write a program to calculate the sum of numbers from m to n.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int n, m, sum =0;
clrscr();
printf("\n Enter the value of m: ");
scanf("%d", &m);
printf("\n Enter the value of n: ");
scanf("%d", &n);
while(m<=n)
{
sum = sum + m;

m = m + 1;
}
printf("\n SUM = %d", sum);
return 0;
}
/*24. Write a program to display the largest of 6 numbers using ternary operator.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i=0, large = -1, num;
clrscr();
while(i<=5)
{
printf("\n Enter a number: ");
scanf("%d",&num);
large = num>large?num:large;
i++;
}
printf("\n The largest of six numbers entered is: %d", large);
return 0;
}
/*25. Write a program to read the numbers until 1 is encountered. Also count the negative, positive,
and zeros entered by the user.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int negatives=0, positives=0, zeros=0;
clrscr();
printf("\n Enter -1 to exit.");
printf("\n\n Enter any number: ");
scanf("%d",&num);
while(num != -1)
{
if(num>0)
positives++;
else if(num<0)
negatives++;
else
zeros++;
printf("\n\n Enter any number: ");
scanf("%d",&num);
}
printf("\n Count of positive numbers entered = %d", positives);

printf("\n Count of negative numbers entered = %d", negatives);


printf("\n Count of zeros entered = %d", zeros);
getch();
return 0;
}
/*26. Write a program to calculate the average of numbers entered by the user.*/
#include <stdio.h>
int main()
{
int num, sum = 0, count = 0;
float avg;
printf("\n Enter any number. Enter -1 to STOP: ");
scanf("%d", &num);
while(num != -1)
{
count++;
sum = sum + num;
printf("\n Enter any number. Enter -1 to STOP: ");
scanf("%d", &num);
}
avg = (float)sum/count;
printf("\n SUM = %d", sum);
printf("\n AVERGAE = %f", avg);
return 0;
}
/*27. Write a program to calculate the average of first n numbers.*/
#include <stdio.h>
int main()
{
int n, i = 0, sum =0;
float avg = 0.0;
printf("\n Enter the value of n: ");
scanf("%d", &n);
do
{
sum = sum + i;
i = i + 1;
}
while(i<=n);
avg = sum/n;
printf("\n The sum of first n numbers = %d", sum);
printf("\n The average of first %d numbers = %f", n, avg);
return 0;
}

/*28. Write a program using do-while loop to display the square and cube of fi rst n natural
numbers.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
printf("\n ------------------------------------------------------");
i=1;
do
{
printf("\n | \t %d \t | \t %d \t | \t %d \t |", i, (i*i), (i*i*i));
i++;
} while(i<n);
printf("\n ------------------------------------------------------");
return 0;
}
/*29. Write a program to list all the leap years from 1900 to 2100.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int m=1900, n=2100, i;
clrscr();
do
{
if(i%4 == 0)
printf("\n %d is a leap year",m);
else
printf("\n %d is not a leap year", m);
m = m+1;
}while(m<=n);
return 0;
}

/*30. Write a program to read a character until a * is encountered. Also count the number of upper
case, lower case, and numbers entered by the users.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
int lowers = 0, uppers = 0, numbers = 0;
clrscr();
printf("\n Enter any character: ");
scanf("%c", &ch);
do
{
if(ch >='A' && ch<='Z')
uppers++;
if(ch >='a' && ch<='z')
lowers++;
if(ch >='0' && ch<='9')
numbers++;
fflush(stdin);
/* The function is used to clear the
standard input file. */
printf("\n Enter another character. Enter * to exit.");
scanf("%c", &ch);
} while(ch != '*');
printf("\n Total count of lower case characters entered = %d", lowers);
printf("\n Total count of upper case characters entered = %d", uppers);
printf("\n Total count of numbers entered = %d", numbers);
return 0;
}
/*31. Write a program to read the numbers until -1 is encountered. Also calculate the sum and mean
of all positive numbers entered and the sum and mean of all negative numbers entered separately.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int sum_negatives=0, sum_positives=0;
int positives = 0, negatives = 0;
float mean_positives = 0.0, mean_negatives = 0.0;
clrscr();
printf("\n Enter -1 to exit.");
printf("\n\n Enter any number: ");
scanf("%d",&num);
do
{

if(num>0)
{
sum_positives += num;
positives++;
}
else if(num<0)
{
sum_negatives += num;
negatives++;
}
printf("\n\n Enter any number: ");
scanf("%d",&num);
} while(num != -1);
mean_positives = sum_positives/positives;
mean_negatives = sum_negatives/negatives;
printf("\n Sum of all positive numbers entered = %d", sum_positives);
printf("\n Mean of all positive numbers entered = %f", mean_positives);
printf("\n Sum of all negative numbers entered = %d", sum_negatives);
printf("\n Mean of all negative numbers entered = %f", mean_negatives);
return 0;
}
/*32. Write a program to print the following pattern.
Pass 1- 1 2 3 4 5
Pass 2- 1 2 3 4 5
Pass 3- 1 2 3 4 5
Pass 4- 1 2 3 4 5
Pass 5- 1 2 3 4 5*/
#include <stdio.h>
main()
{
int i, j;
for(i=1;i<=5;i++)
{
printf("\n Pass %d- ",i);
for(j=1;j<=5;j++)
printf(" %d", j);
}
return 0;
}

/*33. Write a program to print the following pattern.


**********
**********
**********
**********
**********
*/
#include <stdio.h>
main()
{
int i, j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=5;j++)
printf("*");
}
return 0;
}
/*34. Write a program to print the following pattern.
*
**
***
****
*****
*/
#include <stdio.h>
main()
{
int i, j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
printf("*");
}
return 0;
}

/*35. Write a program to print the following pattern.


1
12
123
1234
12345*/
#include <stdio.h>
main()
{
int i, j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
printf("%d", j);
}
return 0;
}
/*36. Write a program to print the following pattern.
1
22
333
4444
55555*/
#include <stdio.h>
main()
{
int i, j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
printf("%d", i);
}
return 0;
}

/*37. Write a program to print the following pattern.


0
12
345
6789*/
#include <stdio.h>
main()
{
int i, j, count=0;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
printf("%d", count++);
}
return 0;
}
/*38. Write a program to print the following pattern.
A
AB
ABC
ABCD
ABCDE
ABCDEF*/
#include <stdio.h>
main()
{
char i, j;
for(i=65;i<=70;i++)
{
printf("\n");
for(j=65;j<=i;j++)
printf("%c", j);
}
return 0;
}

/*39. Write a program to print the following pattern.


1
12
123
1234
1 2 3 4 5*/
#include <stdio.h>
#define N 5
main()
{
int i, j, k;
for(i=1;i<=N;i++)
{
for(k=N;k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d", j);
printf("\n");
}
return 0;
}
/*40. Write a program to print the following pattern.
1
121
12321
1234321
123454321
*/
#include <stdio.h>
#define N 5
main()
{
int i, j, k, l;
for(i=1;i<=N;i++)
{
for(k=N;k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d", j);
for(l=j-2;l>0;l--)
printf("%d", l);
printf("\n");
}
return 0;
}

/*41. Write a program to print the following pattern.


1
22
333
4444
5 5 5 5 5*/
#include <stdio.h>
#define N 5
main()
{
int i, j, k, count=5;
for(i=1;i<=N;i++)
{
for(k=1;k<=count;k++)
printf(" ");
for(j=1;j<=i;j++)
printf("%2d", i);
printf("\n");
c--;
}
return 0;
}
/*42. Write a program to print the multiplication table of n, where n is entered by the user.*/
#include <stdio.h>
int main()
{
int n, i;
printf("\n Enter any number: ");
scanf("%d", &n);
printf("\n Multiplication table of %d", n);
printf("\n ************************");
for(i=0;i<=20;i++)
printf("\n %d X %d = %d", n, i, (n * i));
return 0;
}
/*43. Write a program to print numbers from m to n.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int n, m;
printf("\n Enter the value of m: ");
scanf("%d", &m);
printf("\n Enter the value of n: ");
scanf("%d", &n);

for(i=m;i<=n;i++)
printf("\t %d", m);
return 0;
}
/*44. Write a program using for loop to print all the numbers from m to n, thereby classifying them as
even or odd.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, m, n;
clrscr();
printf("\n Enter the value of m: ");
scanf("%d", &m);
printf("\n Enter the value n: ");
scanf("%d", &n);
for(i=m;i<=n;i++)
{
if(i%2 == 0)
printf("\n %d is even",i);
else
printf("\n %d is odd", i);
}
return 0;
}
/*45. Write a program using for loop to calculate the average of first n natural numbers.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i, sum =0;
float avg = 0.0;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
sum = sum + i;
avg = sum/n;
printf("\n The sum of first n natural numbers = %d", sum);
printf("\n The average of first n natural numbers = %f", avg);
return 0;
}

/*46. Write a program using for loop to calculate factorial of a number.*/


#include <stdio.h>
#include <conio.h>
int main()
{
int fact = 1, num;
clrscr();
printf("\n Enter the number: ");
scanf("%d",&num);
if(num == 0)
fact = 1;
else
{
for(int i=1; i<=num;i++)
fact = fact * i;
}
printf("\n Factorial of %d is: %d ", num, fact);
return 0;
}
/*47. Write a program to classify a given number as prime or composite.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int flag = 0, i, num;
clrscr();
printf("\n Enter any number: ");
scanf("%d", &num);
for(i=2; i<num/2;i++)
{
if(num%i == 0)
{
flag =1;
break;
}
}
if(flag == 1)
printf("\n %d is a composite number", num);
else
printf("\n %d is a prime number", num);
return 0;
}

/*48. Write a program using do-while loop to read the numbers until 1 is encountered. Also count
the number of prime numbers and composite numbers entered by the user.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num, i;
int primes=0, composites=0, flag=0;
clrscr();
printf("\n Enter -1 to exit.");
printf("\n\n Enter any number:");
scanf("%d",&num);
do
{
for(i=2;i<=num%2;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
primes++;
else
composites++;
flag=0;
printf("\n\n Enter any number: ");
scanf("%d",&num);
}
while(num != -1);
printf("\n Count of prime numbers entered = %d", primes);
printf("\n Count of composite numbers entered = %d", composites);
return 0;
}
/*49. Write a program to calculate pow(x,n) i.e to calculate xn.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int i, num, n;
long int result =1;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);

printf("\n Till which power to calculate: ");


scanf("%d", &n);
for(i=1;i<=n;i++)
result = result * num;
printf("\n pow(%d, %d) = %ld", num, n, result);
return 0;
}
/*50. Write a program to print the reverse of a number.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num, temp;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
printf("\n The reversed number is: ");
while(num != 0)
{
temp = num%10;
printf("%d",temp);
num = num/10;
}
return 0;
}
/*51. Write a program to enter a number and then calculate the sum of its digits.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num, temp, sumofdigits = 0;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
while(num != 0)
{
temp = num%10;
sumofdigits += temp;
num = num/10;
}
printf("\n The sum of digits = %d", sumofdigits);
return 0;
}

/*52. Write a program to enter a decimal number. Calculate and display the binary equivalent of this
number.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num, remainder, binary_num = 0,
i = 0;
clrscr();
printf("\n Enter the decimal number: ");
scanf("%d", &decimal_num);
while(decimal_num != 0)
{
remainder = decimal_num%2;
binary_num += remainder*pow(10,i);
decimal_num = decimal_num/2;
i++;
}
printf("\n The binary equivalent = %d", binary_num);
return 0;
}
/*53. Write a program to enter a decimal number. Calculate and display the octal equivalent of this
number.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num, remainder, octal_num = 0,
i = 0;
clrscr();
printf("\n Enter the decimal number: ");
scanf("%d", &decimal_num);
while(decimal_num != 0)
{
remainder = decimal_num%8;
octal_num += remainder*pow(10,i);
decimal_num = decimal_num/8;
i++;
}
printf("\n The octal equivalent = %d", octal_num);
return 0;
}

/*54. Write a program to enter a decimal number. Calculate and display the hexadecimal equivalent
of this number.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num, hex_num = 0, i = 0, remainder;
clrscr();
printf("\n Enter the decimal number: ");
scanf("%d", &decimal_num);
while(decimal_num != 0)
{
remainder = decimal_num%16;
hex_num += remainder*pow(10,i);
decimal_num = decimal_num/16;
i++;
}
printf("\n The hexa decimal equivalent = %d", hex_num);
return 0;
}
/*55. Write a program to enter a binary number. Calculate and display the decimal equivalent of this
number.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num = 0, remainder, binary_num, i = 0;
clrscr();
printf("\n Enter the binary number: ");
scanf("%d", &binary_num);
while(binary_num != 0)
{
remainder = binary_num%10;
decimal_num += remainder*pow(2,i);
binary_num = binary_num/10;
i++;
}
printf("\n The decimal equivalent of = %d", decimal_num);
return 0;
}

/*56. Write a program to enter an octal number. Calculate and display the decimal equivalent of this
number.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num= 0, remainder, octal_num, i = 0;
clrscr();
printf("\n Enter the octal number: ");
scanf("%d", &octal_num);
while(octal_num != 0)
{
remainder = octal_num%10;
decimal_num += remainder*pow(8,i);
octal_num = octal_num/10;
i++;
}
printf("\n The decimal equivalent = %d", decimal_num);
return 0;
}
/*57. Write a program to enter a hexadecimal number. Calculate and display the decimal equivalent
of this number.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num = 0, remainder, hex_num, i = 0;
clrscr();
printf("\n Enter the hexadecimal number: ");
scanf("%d", &hex_num);
while(hex_num != 0)
{
remainder = hex_num%10;
decimal_num += remainder*pow(16,i);
hex_num = hex_num/10;
i++;
}
printf("\n The decimal equivalent = %d", decimal_num);
return 0;
}
/*58. Write a program to calculate GCD of two numbers.*/
#include <stdio.h>
#include <conio.h>

int main()
{
int num1, num2, temp;
int dividend, divisor, remainder;
clrscr();
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
if(num1>num2)
{
dividend = num1;
divisor = num2;
}
else
{
dividend = num2;
divisor = num1;
}
while(divisor)
{
remainder = dividend%divisor;
dividend = divisor;
divisor = remainder;
}
printf("\n GCD of %d and %d is = %d", num1, num2, dividend);
return 0;
}
/*59. Write a program to sum the series 1+1/2+1/3. . .+1/N.*/
#include <stdio.h>
#include <conio.h>
main()
{
int n;
float sum=0.0, a, i;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1.0;i<=n;i++)
{
a=1/i;
sum = sum +a;
}
printf("\n The sum of series 1/1 + 1/2 + .... + 1/%d = %f",n,sum);
return 0;
}

/*60. Write a program to sum the series (Please refer to the book for the series.) */

#include <stdio.h>
#include <math.h>
#include <conio.h>
main()
{
int n;
float sum=0.0, a, i;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1.0;i<=n;i++)
{
a=1/pow(i,2);
sum = sum +a;
}
printf("\n The sum of series 1/12 + 1/ 22 + ... 1/n2 = %f",sum);
return 0;
}
/*61. Write a program to sum the series.. (Please refer to the book for the series)*/
#include <stdio.h>
#include <conio.h>
main()
{
int n;
float sum=0.0, a, i;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1.0;i<=n;i++)
{ a= i/(i+1);
sum = sum +a;
}
printf("\n The sum of series 1/2 + 2/3 + .... = %f",n,n+1,sum);
return 0;
}
/*62. Write a program to sum the series.. (Please refer to the book for the series)*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int n, NUM;
float i,sum=0.0;

clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1.0;i<=n;i++)
{
NUM = pow(i,i);
sum += (float)NUM/i;
}
printf("\n 1/1 + 4/2 + 27/3 + .... = %f", sum);
return 0;
}
/*63. Write a program to calculate sum of cubes of first n numbers.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int i, n;
int term, sum = 0;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
term = pow(i,3);
sum += term;
}
printf("\n 13 + 23 + 33 + .... = %d", sum);
return 0;
}
/*64. Write a program to calculate sum of squares of first n even numbers.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int i, n;
int term, sum = 0;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
if(i%2 == 0)
{
term = pow(i,2);

sum += term;
}
}
printf("\n 2^2 + 4^2 + 6^2 + .... = %d", sum);
return 0;
}
/*65. Write a program to fi nd whether the given number is an armstrong number or not.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int num, sum=0, r, n;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
n=num;
while(n>0)
{
r=n%10;
sum += pow(r,3);
n=n/10;
}
if(sum==num)
printf("\n %d is an armstrong number", num);
else
printf("\n %d is not an armstrong number", num);
return 0;
}
/*66. Write a program to print the multiplication table.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j;
clrscr();
for(i=1;i<=2;i++)
{
printf("\n\n\n\t\t Multiplication table of %d", i);
printf("\n ******************************");
for(j=1;j<=20;j++)
printf("\t %d X %d = %d",i,j, (i*j));
}
getch();
return 0;
}

/*67. Write a program using for loop to calculate the value of an investment, given the initial value of
investment and the annual interest. Calculate the value of investment over a period of time.*/
#include <stdio.h>
main()
{
double initVal, futureVal, ROI;
int yrs, i;
printf("\n Enter the investment value: ");
scanf("%lf", &initVal);
printf("\n Enter the rate of interest: ");
scanf("%lf", &ROI);
printf("\n Enter the number of years for which investment has to be done: ");
scanf("%d", &yrs);
futureVal=initVal;
printf("\n YEAR \t\t VALUE");
printf("\n _______________________");
for(i=1;i<=yrs;i++)
{
futureVal = futureVal * (1 + ROI/100.0);
printf("\n %d \t %lf", i, futureVal);
}
return 0;
}
/*68. Write a program to generate calendar of a month given the start day of the week and the
number of days in that month.*/
#include <stdio.h>
main()
{
int i, j, startDay, num_of_days;
printf("\n Enter the starting day of the week (1 to 7): ");
scanf("%d", &startDay);
printf("\n ENter the number of days in that month: ");
scanf("%d", &num_of_days);
printf(" Sun Mon Ture Wed Thurs Fri Sat\n");
printf("\n ________________________________");
for(i=0;i<startDay-1;i++)
printf(" ");
for(j=1;j<=num_of_days;j++)
{
if(i>6)
{
printf("\n");
i=1;
}
else
i++;

printf("%2d ", j);


}
return 0;
}
/*69. Write a program to calculate square root of a number.*/
#include <stdio.h>
#include <math.h>
main()
{
int num;
do
{
printf("\n Enter any number. Enter 999 to stop: ");
scanf("%d", &num);
if(num == 999)
break; // quit the loop
if (num < 0)
{
printf("\n Square root of negative numbers is not defined");
continue; // skip the following statements
}
printf("\n The square root of %d is" %lf, sqrt(num));
}
while(1);
return 0;
}

Functions
/*1. Write a program to add two integers using functions.*/
#include <stdio.h> // FUNCTION DECLARATION
int sum(int a, int b);
int main()
{
int num1, num2, total = 0;
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
total = sum(num1, num2); // FUNCTION CALL
printf("\n Total = %d", total);
return 0;
}
// FUNCTION DEFINITION
int sum (int a, int b) // FUNCTION HEADER
{ // FUNCTION BODY
int result;
result = a + b;
return result;
}
/*2. Write a function to swap the value of two variables.*/
#include <stdio.h>
void swap_call_by_val(int, int);
void swap_call_by_ref(int *, int *);
int main()
{
int a=1, b=2, c=3, d=4;
printf("\n In main(), a = %d and b = %d", a, b);
swap_call_by_val(a, b);
printf("\n In main(), a = %d and b = %d", a, b);
printf("\n\n In main(), c = %d and d = %d", c, d);
swap_call_by_ref(&c, &d);
// address of the variables is passed
printf("\n In main(), c = %d and d = %d", c, d);
return 0;
}
void swap_call_by_val(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf("\n In function (Call By Value Method) a = %d and b = %d", a, b);
}

void swap_call_by_ref(int *c, int *d)


{
int temp;
temp = *c;
// *operator used to refer to the value
*c = *d;
*d = temp;
printf("\n In function (Call By Reference Method) c = %d and d = %d", *c, *d);
}
/*3. Write a program to find biggest of three integers using functions.*/
#include <stdio.h>
int greater(int a, int b, int c);
int main()
{
int num1, num2, num3, large;
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
printf("\n Enter the third number: ");
scanf("%d", &num3);
large = greater(num1, num2, num3);
printf("\n Largest number = %d", large);
return 0;
}
int greater(int a, int b, int c)
{
if(a>b && a>c)
return a;
if(b>a && b>c)
return b;
else
return c;
}
/*4. Write a program to calculate area of a circle using function.*/
#include <stdio.h>
float cal_area(float r);
int main()
{
float area, radius;
printf("\n Enter the radius of the circle: ");
scanf("%f", &radius);
area = cal_area(radius);
printf("\n Area of the circle with radius %f = %f", radius, area);
return 0;

}
float cal_area(float radius)
{
return (3.14 * radius * radius);
}
/*5. Write a program, using functions, to fi nd whether a number is even or odd.*/
#include <stdio.h>
int evenodd(int);
int main()
{
int num, flag;
printf("\n Enter the number: ");
scanf("%d", &num);
flag = evenodd(num);
if (flag == 1)
printf("\n %d is EVEN", num);
else
printf("\n %d is ODD", num);
return 0;
}
int evenodd(int a)
{
if(a%2 == 0)
return 1;
else
return 0;
}
/*6. Write a program to convert time to minutes.*/
#include <stdio.h>
#include <conio.h>
int convert_time_in_mins(int hrs, int minutes);
main()
{
int hrs, minutes, total_mins;
printf("\n Enter hours and minutes: ");
scanf("%d %d", &hrs, &minutes);
total_mins = convert_time_in_mins(hrs, minutes);
printf("\n Total minutes = %d", total_mins);
getch();
return 0;
}
int convert_time_in_mins(int hrs, int minutes)
{
int mins;
mins = hrs*60 + minutes;
return mins;

}
/*7. Write a program to calculate P(n/r).*/
#include <stdio.h>
#include <conio.h>
main()
{
int n, r;
float result;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
printf("\n Enter the value of r: ");
scanf("%d", &r);
result = (float)Fact(n)/Fact(r);
printf("\n P(n/r): P(%d)/(%d) = %f", n, r, result);
getch();
return 0;
}
int Fact(int num)
{
int f=1, i;
for(i=num;i>=1;i--)
f = f*i;
return f;
}
/*8. Write a program to calculate C(n/r).*/
#include <stdio.h>
#include <conio.h>
main()
{
int n, r;
float result;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
printf("\n Enter the value of r: ");
scanf("%d", &r);
result = (float)Fact(n)/(Fact(r)*Fact(n-r));
printf("\n C(n/r) : C(%d/%d) = %.2f", n, r, result);
getch();
return 0;
}
int Fact(int num)
{
int f=1, i;
for(i=num;i>=1;i--)

f = f*i;
return f;
}
/*9. Write a program to sum the series1/1! + 1/2! + 1/3! + .. + 1/n!*/
#include <stdio.h>
#include <conio.h>
main()
{
int n, f, i;
float result=0.0;
clrscr();
printf("\n Enter the value of n: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
f=Fact(i);
result += 1/(float)f;
}
printf("\n The sum of the series 1/1! + 1/2! + 1/3!... = %f", result);
getch();
return 0;
}
int Fact(int num)
{
int f=1, i;
for(i=num;i>=1;i--)
f = f*i;
return f;
}
/*10. Write a program to sum the series1/1! + 4/2! + 27/3! + .... */
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int n, i, NUM, DENO;
float sum=0.0;
clrscr();
printf("\n ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
NUM = pow(i,i);
DENO = Fact(i);
sum += (float)NUM/DENO;
}

printf("\n 1/1! + 4/2! + 27/3! + ....= %f", sum);


getch();
return 0;
}
int Fact(int n)
{
int f=1, i;
for(i=n;i>=1;i--)
f=f*i;
return f;
}
/*11. Write a program to calculate GCD using recursive functions.*/
#include <stdio.h>
int GCD(int, int); // FUNCTION DECLARATION
main()
{
int num1, num2, res;
printf("\n Enter the two numbers: ");
scanf("%d %d", &num1, &num2);
res = GCD(num1, num2); // FUNCTION CALL
printf("\n GCD of %d and %d = %d", num1, num2, res);
return 0;
}
int GCD(int x, int y) // FUNCTION DEFINITION
{
int rem;
rem = x%y;
if(rem==0)
return y;
else
return (GCD(y, rem)); // FUNCTION CALL
}
/*12. Write a program to calculate exp(x,y) using recursive functions.*/
#include <stdio.h>
int exp_rec(int, int);
main()
{
int num1, num2, res;
printf("\n Enter the two numbers: ");
scanf("%d %d", &num1, &num2);
res = exp_rec(num1, num2);
return 0;
}
int exp_rec(int x, int y)
{
if( y==0)

return 1;
else
return ( x * exp_rec( x, y-1));
}
/*13. Write a program to print the Fibonacci series using recursion.*/
#include <stdio.h>
int Fibonacci(int);
main()
{
int n;
printf("\n Enter the number of terms in the series: ");
scanf("%d", &n);
for(int i=0;i<n;i++)
printf("\n Fibonacci (%d) = %d", i, Fibonacci(i));
return 0;
}
int Fibonacci(int num)
{
if(num <= 2)
return 1;
return (Fibonacci (num - 1) + Fibonacci(num - 2));
}

Arrays
/*1. Write a program to read and display n numbers using an array.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i=0, n, arr[20];
clrscr();
printf("\n Enter the number of elements:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("Arr[%d] = %d\t", i, arr[i]);
return 0;
}
/*2. Write a program to read and display n random numbers using an array.*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 10
main()
{
int arr[MAX], i,RandNo;
for(i=0;i< MAX;i++)
{
/* Scale the random number in the range 0 to MAX-1 */
RandNo = rand() % MAX;
// rand() is a pre-defined function
arr[i] = RandNo;
}
printf("\n The contents of the array are: \n");
for(i=0;i<MAX;i++)
printf("\t %d", arr[i]);
getch();
return 0;
}
/*3. Write a program to find the mean of n numbers using arrays.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], sum =0;
float mean = 0.0;

clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Arr[%d] = ", i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
sum += arr[i];
mean = sum/n;
printf("\n The sum of the array elements = %d", sum);
printf("\n The mean of the array elements = %f", mean);
return 0;
}
/*4. Write a program to fi nd the largest of n numbers using arrays.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], large = -1111;
/* Note we have assigned large with a very small value so that any value in the array is assured to be
greater than this value */
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
if(arr[i]>large)
large = arr[i];
}
printf("\n The largest number is: %d", large);
return 0;
}
/*5. Write a program to print the position of the smallest of n numbers using arrays.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], small =1234, pos = -1;
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{

if(arr[i]<small)
{
small = arr[i];
pos = i;
}
}
printf("\n The smallest of element is : %d", small);
printf("\n The position of the smallest number in the array is: %d", pos);
return 0;
}
/*6. Write a program to interchange the largest and the smallest number in the array.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], temp;
int small = 9999, small_pos =0;
int large = -9999, large_pos = 0;
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the value of element %d: ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if(arr[i]<small)
{
small = arr[i];
small_pos = i;
}
if(arr[i]>large)
{
large = arr[i];
large_pos = i;
}
}
printf("\n The smallest of these numbers is : %d", small);
printf("\n The position of the smallest number in the array is: %d",small_pos);
printf("\n The largest of these numbers is: %d", large);
printf("\n The position of the largest number in the array is: %d",large_pos);
temp = arr[large_pos];
arr[large_pos] =arr[small_pos];
arr[small_pos] = temp;
printf("\n The new array is: ");
for(i=0;i<n;i++)
printf(" \n arr[%d] = %d ", i, arr[i]);
return 0;

}
/*7. Write a program to fi nd the second biggest number using an array of n numbers.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], large = -1111, second_large = 1234;
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the number: ");
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if(arr[i]>large)
large = arr[i];
}
for(i=0;i<n;i++)
{
if(arr[i] != large)
{
if(arr[i]>second_large)
second_large = arr[i];
}
}
printf("\n The numbers you entered are: ");
for(i=0;i<n;i++)
printf("%d ", arr[i]);
printf("\n The largest of these numbers is: %d",large);
printf("\n The second largest of these numbers is: %d",second_large);
return 0;
}
/*8. Write a program to enter n number of digits. Form a number using these digits.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int number=0, digit[10], numofdigits,i;
clrscr();
printf("\n Enter the number of digits: ");
scanf("%d", &numofdigits);
for(i=0;i<numofdigits;i++)
{
printf("\n Enter the %d th digit: ", i);
scanf("%d", &digit[i]);

}
i=0;
while(i<numofdigits)
{
number = number + digit[i] * pow(10,i);
i++;
}
printf("\n The number is: %d", number);
return 0;
}
/*9. Write a program to fi nd whether the array of integers contain a duplicate number.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int array1[10], i, n, j, flag=0;
clrscr();
printf("\n Enter the size of the array:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%d", &array1[i]);
}
for(i=0;i<n;i++)
{
for(j= i+1;j<n;j++)
{
if(array1[i] == array1[j] && i!=j)
{
flag=1;
printf("\n Duplicate number %d found at location %d and %d", array1[i], i, j);
}
}
}
if(flag==0)
printf("\n No Duplicate");
return 0;
}
/*10. Write a program to read marks of 10 students in the range of 0100. Then make 10 groups: 0
10, 1020, 2030, etc. Count the number of values that falls in each group and display the result.*/
#include <stdio.h>
#include <conio.h>
main()
{
int marks[50], i;
int group[10]={0};
printf("\n Enter the marks of 10 students: \n");
for(i=0;i<10;i++)
{

printf("\n MARKS[%d] = ", i);


scanf("%d", &marks[i]);
++group[(int)(marks[i])/10];
}
printf("\n\n *************************");
printf("\n GROUP \t\t FREQUENCY");
for(i=0;i<10;i++)
printf("\n %d \t\t %d", i, group[i]);
getch();
return 0;
}
/*11. Modify the above program to display frequency histograms of each group.*/
#include <stdio.h>
#include <conio.h>
main()
{
int marks[10], i;
int group[10]={0};
printf("\n Enter the marks of 10 students: \n");
for(i=0;i<10;i++)
{
printf("\n MARKS[%d] = ", i);
scanf("%d", &marks[i]);
++group[(int)(marks[i])/10];
}
printf("\n\n *************************");
printf("\n GROUP \t\t FRQUENCY");
for(i=0;i<10;i++)
printf("\n %d \t\t %d", i, group[i]);
i=0;
printf("\n\n FREQUENCY HISTOGRAM");
for(int index=0;index<10;index++)
{
printf("\n GROUP %d | ",index);
for(i=0;i<group[index];i++)
printf(" *");
}
getch();
return 0;
}
/*12. Write a program to read a sorted list of fl oating point values and then calculate and display
the median of the values.*/
#include <stdio.h>
#include <conio.h>
main()
{
int i, j, n;
float median, values[10];
printf("\n Enter the size of the array: ");

scanf("%d", &n);
printf("\n Enter the values: ");
for(i=0;i<n;i++)
scanf("%f", &values[i]);
if(n%2==0)
median = (values[n/2] + values[n/2+1])/2.0;
else
median = values[n/2 + 1];
printf("\n MEDIAN = %f", median);
getch();
return 0;
}
/* 13. Write a program to insert a number at a given location in an array.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, num, pos, arr[10];
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Arr[%d] = : ", i);
scanf("%d", &arr[i]);
}
printf("\n Enter the number to be inserted: ");
scanf("%d", &num);
printf("\n Enter the position at which the number has to be added: ");
scanf("%d", &pos);
for(i=n;i>=pos;i--)
arr[i+1] = arr[i];
arr[pos] = num;
n++;
printf("\n The array after insertion of %d is: ", num);
for(i=0;i<n+1;i++)
printf("\n Arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
/* 14. Write a program to insert a number in an array that is already sorted in ascending order.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, j, num, arr[10];
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);

printf("\n Enter the array elements:");


for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("\n Enter the number to be inserted: ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] > num)
{
for(j = n; j<=i; j--)
arr[j+1] = arr[j];
arr[i] = num;
break;
}
}
n++;
printf("\n The array after insertion of %d is: ", num);
for(i=0;i<n+1;i++)
printf("\t %d", arr[i]);
getch();
return 0;
}
/* 15. Write a program to delete a number from a given location in an array.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, pos, arr[10];
clrscr();
printf("\n Enter the size of the array:");
scanf("%d", &n);
printf("\n Enter the elements of the array : ");
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("\n Enter the position from which the number has to be deleted: ");
scanf("%d", &pos);
for(i= pos; i<n;i++)
arr[i] = arr [i+1];
n--;
printf("\n The array after deletion is:");
for(i=0;i<n;i++)
printf("\n Arr[%d] = %d", i, arr[i]);
getch();
return 0;
}

/* 16. Write a program to delete a number from an array that is already sorted in ascending
order.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, j, num, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("\n Enter the number to be deleted: ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
for(j=i; j<n;i++)
arr[j] = arr[j+1];
}
}
printf("\n The array after deletion is:");
for(i=0;i<n-1;i++)
printf("\t%d", arr[i]);
getch();
return 0;
}
/* 17. Write a program to merge two unsorted arrays.*/
#include <stdio.h>
#include <conio.h>
main()
{
int arr1[10], arr2[10], arr3[20];
int i, n1, n2, m, index=0;
clrscr();
printf("\n Enter the number of elements in array 1: ");
scanf("%d", &n1);
printf("\n\n Enter the Elements of the first array");
printf("\n ****************************");
for(i=0;i<n1;i++)
scanf("%d", &arr1[i]);
printf("\n Enter the number of elements in array 2: ");
scanf("%d", &n2);
printf("\n\n Enter the Elements of the second array");
printf("\n ****************************");
for(i=0;i<n2;i++)
scanf("%d", &arr2[i]);
m = n1+n2;

for(i=0;i<n1;i++)
{
arr3[index] = arr1[i];
index++;
}
for(i=0;i<n2;i++)
{
arr3[index] = arr2[i];
index++;
}
printf("\n\n The merged array is");
printf("\n ********************");
for(i=0;i<m;i++)
printf("\t Arr[%d] = %d", i, arr3[i]);
getch();
return 0;
}
/* 18. Write a program to merge two sorted arrays.*/
#include <stdio.h>
#include <conio.h>
main()
{
int arr1[10], arr2[10], arr3[20];
int i, n1, n2, m, index=0;
int index_first = 0, index_second = 0;
clrscr();
printf("\n Enter the number of elements in array1: ");
scanf("%d", &n1);
printf("\n\n Enter the elements of the first array");
printf("\n ****************************");
for(i=0;i<n1;i++)
scanf("%d", &arr1[i]);
printf("\n Enter the number of elements in array2 : ");
scanf("%d", &n2);
printf("\n\n Enter the elements of the second array");
printf("\n ****************************");
for(i=0;i<n2;i++)
scanf("%d", &arr2[i]);
m = n1+n2;
while(index_first < n1 && index_second < n2)
{
if(arr1[index_first]<arr2[index_second])
{
arr3[index] = arr1[index_first];
index_first++;
}
else
{
arr3[index] = arr2[index_second];
index_second++;

}
index++;
}
/* if elements of the first array is over and the second array has some elements */
if(index_first == n1)
{
while(index_second<n2)
{
arr3[index] = arr2[index_second];
index_second++;
index++;
}
}
/* if elements of the second array is over and the first array has some elements */
else if(index_second == n2)
{
while(index_first<n1)
{
arr3[index] = arr1[index_first];
index_first++;
index++;
}
}
printf("\n\n The contents of the merged array are");
printf("\n **************************");
for(i=0;i<m;i++)
printf("\n Arr[%d] = %d", i, arr3[i]);
getch();
return 0;
}
/* 19. Write a program to implement linear search.*/
#include <stdio.h>
#include <conio.h>
main()
int arr[10], num, i, n, found = 0, pos = -1;
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements ");
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("\n Enter the number that has to be searched : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
pos=i;

printf("\n %d is found in the array at position = %d", num, i);


break;
}
}
if (found == 0)
printf("\n %d DOES NOT EXIST in the array", num);
getch();
return 0;
}
/* 20. Write a program to implement binary search.*/
#include <stdio.h>
#include <conio.h>
main()
{
int arr[10], num, i, n, pos = -1, beg, end, mid, found =0;
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be searched: ");
scanf("%d", &num);
beg = 0, end = n-1;
while(beg + end)
{
mid = (beg + end)/2;
if (arr[mid] == num)
{
printf("\n %d is present in the array at position = %d", num, mid);
found=1;
break;
}
if (arr[mid]>num)
end = mid-1;
else if (arr[mid] < num)
beg = mid+1;
}
if ( beg > end && found == 0)
printf("\n %d DOES NOT EXIST IN THE ARRAY", num);
getch();
return 0;
}

/* 21. Write a program to read and print an array of n numbers.*/


#include <stdio.h>
#include <conio.h>
void read_array( int arr[], int);
void display_array( int arr[], int);
int main()
{
int num[10], n;
clrscr();
printf("\n Enter the size of the array:");
scanf("%d", &n);
read_array(num, n);
display_array(num, n);
getch();
return 0;
}
void read_array( int arr[10], int n)
{
int i;
for(int i=0;i<n;i++)
{
printf("\n array[%d] = ", i);
scanf("%d", &arr[i]);
}
}
void display_array( int arr[10], int n)
{
for(i=0;i<n;i++)
printf("\n array[%d] = %d", i, arr[i]);
}
/* 22. Write a program to read an array of n numbers, then find out the smallest number.*/
#include <stdio.h>
#include <conio.h>
void read_array(int arr[], int);
int find_small(int arr[], int n);
int main()
{
int num[10], n, smallest;
clrscr();
printf("\n Enter the size of the array:");
scanf("%d", &n);
read_array(num, n);
smallest = find_small(num, n);
printf("\n The smallest number in the array is = %d", smallest);
getch();
return 0;
}
void read_array( int arr[10], int n)
{

int i;
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
}
int find_small(int arr[10], int n)
{
int i, small = 1234567;
for(i=0;i<n;i++)
{
if(arr[i] < small)
small = arr[i];
}
return small;
}
/* 23. Write a program to merge two integer arrays. Also display the merged array in reverse
order.*/
#include <stdio.h>
#include <conio.h>
void read_array(int my_array[], int);
void display_array(int my_array[], int);
void merge_array(int my_array3[], int, int my_array1[], int, int my_array2[], int);
void reverse_array(int my_array[], int);
int main()
{
int arr1[10], arr2[10], arr3[20], n, m, t;
clrscr();
printf("\n Enter the number of elements in the first array: ");
scanf("%d", &m);
read_array(arr1, m);
printf("\n Enter the number of elements in the second array: ");
scanf("%d", &n);
read_array(arr2, n);
t = m + n;
merge_array(arr3, t, arr1, m, arr2, n);
printf("\n The merged array is : ");
display_array(arr3, t);
printf("\n The merged array in reverse order is: ");
reverse_array(arr3, t);
getch();
return 0;
}
void read_array(int my_array[10], int n)
{
int i;
for(i=0;i<n;i++)
scanf("%d", &my_array[i]);
}
void merge_array(int my_array3[], int t, int my_array1[], int m, int my_array2[], int n)
{
int i, j=0;

for(i=0; i<m; i++)


{
my_array3[j] = my_array1[i];
j++;
}
for(i=0; i<n; i++)
{
my_array3[j] = my_array2[i];
j++;
}
}
void display_array(int my_array[], int n)
{
int i;
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, my_array[i]);
}
void reverse_array(int my_array[], int m)
{
int i, j;
for(i=m-1, j=0;i>=0;i--, j++)
printf("\n arr[%d] = %d", j, my_array[i]);
}
/* 24. Write a program to interchange the biggest and the smallest number in the array.*/
#include <stdio.h>
#include <conio.h>
void read_array(int my_array[], int);
void display_array(int my_array[], int);
void interchange(int arr[], int);
int find_biggest_pos(int my_array[10], int n);
int find_smallest_pos(int my_array[10], int n);
int main()
{
int arr[10], n;
clrscr();
printf("\n Enter the size of the array:");
scanf("%d", &n);
read_array(arr, n);
display_array(arr, n);
interchange(arr, n);
display_array(arr, n);
getch();
return 0;
}
void read_array(int my_array[10], int n)
{
int i;
for(i=0;i<n;i++)
scanf("%d", &my_array[i]);

}
void display_array(int my_array[10], int n)
{
int i;
printf("n");
for(i=0;i<n;i++)
printf("\t array[%d] = %d", i, my_array[i]);
}
void interchange(int my_array[10], int n)
{
int temp, big_pos, small_pos;
big_pos = find_biggest_pos(my_arr, n);
small_pos = find_smallest_pos(my_arr,n);
temp = my_array[big_pos];
my_array[big_pos] = my_array[small_pos];
my_array[small_pos] = temp;
}
int find_biggest_pos(int my_array[10], int n)
{
int i, large = -123456, pos=-1;
for(i=0;i<n;i++)
{
if (my_array[i] > large)
{
large = my_array[i];
pos=i;
}
}
return pos;
}
int find_smallest_pos(int my_array[10], int n)
{
int i, small = 123456, pos=-1;
for(i=0;i<n;i++)
{
if (my_array[i] < small)
{
small = my_array[i];
pos=i;
}
}
return pos;
}

/* 25. Write a program to print the elements of a 2D array.*/


#include <stdio.h>
#include <conio.h>
main()
{
int arr[2][2] = {12, 34, 56,32};
int i, j;
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t", arr[i][j]);
}
return 0;
}
/* 26. Write a program to generate Pascals triangle.*/
#include <stdio.h>
#include <conio.h>
main()
{
int arr[7][7]={0};
int row=2, col, i, j;
arr[0][0] = arr[1][0] = arr[1][1] = 1;
while(row <= 7)
{
arr[row][0] = 1;
for(col = 1;col <= row;col++)
arr[row][col] = arr[row-1][col-1] +
arr[row-1][col];
row++;
}
for(i=0;i<7;i++)
{
printf("\n");
for(j=0;j<=i;j++)
printf("\t %d", arr[i][j]);
}
getch();
return 0;
}

/* 27. In a small company there are 5 salesmen. Each salesman is supposed to sell 3 products. Write
a program using two-dimensional array to print (i) the total sales by each salesman and (ii) total
sales of each item.*/
#include <stdio.h>
#include <conio.h>
main()
{
int sales[5][3], i, j, total_sales=0;
//INPUT DATA
printf("\n ENTER THE DATA");
printf("\n *****************");
for(i=0;i<5;i++)
{
printf("\n Enter the sales of 3 items sold by salesman %d: ", i);
for(j=0;j<3;j++)
scanf("%d", &sales[i][j]);
}
// PRINT TOTAL SALES BY EACH SALESMAN
for(i=0;i<5;i++)
{
total_sales = 0;
for(j=0;j<3;j++)
total_sales += sales[i][j];
printf("\n Total Sales By Salesman %d = %d",i, total_sales);
}
// TOTAL SALES OF EACH ITEM
for(i=0;i<3;i++) // for each item
{
total_sales=0;
for(j=0;j<5;j++) // for each salesman
total_sales += sales[j][i];
printf("\n Total sales of item %d = %d", i, total_sales);
}
getch();
return 0;
}
/* 28. In a class there are 10 students. Each student is supposed to appear in 3 tests. Write a
program using two-dimensional arrays to print
(i) the marks obtained by each student in different subjects
(ii) total marks and average obtained by each student
(iii) store the average of each student in a separate 1D array so that it can be used to calculate the
class average.*/
#include <stdio.h>
#include <conio.h>
main()
{
int marks[10][3], i, j;
int total_marks[10]={0};;
float class_avg=0.0, total_avg = 0.0;

float avg[10];
//INPUT DATA
printf("\n ENTER THE DATA");
printf("\n *****************");
for(i=0;i<10;i++)
{
printf("\n Enter the marks of student %d in 3 subjects : ", i);
for(j=0;j<3;j++)
scanf("%d", &marks[i][j]);
}
// CALCULATE TOTAL MARKS OF EACH STUDENT
for(i=0;i<10;i++)
{
for(j=0;j<3;j++)
total_marks[i] += marks[i][j];
}
// CALCULATE AVERAGE OF EACH STUDENT
for(i=0;i<10;i++)
{
for(j=0;j<3;j++)
avg[i] = (float)total_marks[i]/3.0;
}
// CALCULATE CLASS AVERAGE
for(i=0;i<10;i++)
total_avg += avg[i];
class_avg = (float)total_avg/10;
// DISPLAY RESULTS
printf("\n\n STUD NO. MARKS OBTAINED IN THREE SUBJECTS TOTAL MARKS \t AVERAGE");
printf("\n*****************************************************");
for(i=0;i<10;i++)
{
printf("\n %4d", i);
for(j=0;j<3;j++)
printf(" %d", marks[i][j]);
printf("%4d \t%2.2f", total_marks[i], avg[i]);
}
printf("\n\n CLASS AVERAGE = %f", class_avg);
getch();
return 0;
}
/* 29. Write a program to read a two-dimensional array marks which stores marks of 5 students in
3 subjects. Write a program to display the highest marks in each subject.*/
#include <stdio.h>
#include <conio.h>
main()
{
int marks[5][3], i, j, max_marks;
for(i=0;i<5;i++)
{
printf("\n Enter the marks obtained by student %d",i);

for(j=0;j<3;j++)
{
printf("\n marks[%d][%d] = ", i, j);
scanf("%d", &marks[i][j]);
}
}
for(j=0;j<3;j++)
{
max_marks = -999;
for(i=0;i<5;i++)
{
if(marks[i][j]>max_marks)
max_marks = marks[i][j];
}
printf("\n The highest marks obtained in the subject %d= %d", j, max_marks);
}
getch();
return 0;
}
/* 30. Write a program to read and display a 3 X 3 matrix.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, mat[3][3];
clrscr();
printf("\n Enter the elements of the matrix ");
printf("\n *************************");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&mat[i][j]);
}
printf("\n The elements of the matrix are ");
printf("\n *************************");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t %d",i, j, mat[i][j]);
}
return 0;
}

/* 31. Write a program to transpose a 3 X 3 matrix.*/


#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, mat[3][3], transposed_mat[3][3];
clrscr();
printf("\n Enter the elements of the matrix");
printf("\n *************************");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n mat[%d][%d] = ",i, j);
scanf("%d", &mat[i][j]);
}
}
printf("\n The elements of the matrix are ");
printf("\n *************************");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t mat[%d][%d] = %d", i, j, mat[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
transposed_mat[i][j] = mat[j][i];
}
printf("\n The elements of the transposed matrix are ");
printf("\n *************************");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t transposed_mat[%d][%d] = %d",i, j, transposed_mat[i][j]);
}
return 0;
}
/* 32. Write a program to input two m X n matrices and then calculate the sum of their
corresponding elements and store it in a third m X n matrix.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j;
int rows1, cols1, rows2, cols2, rows_sum, cols_sum;
int mat1[5][5], mat2[5][5], sum[5][5];

clrscr();
printf("\n Enter the numbers of rows in the first matrix: ");
scanf("%d",&rows1);
printf("\n Enter the numbers of columns in the first matrix: ");
scanf("%d",&cols1);
printf("\n Enter the numbers of rows in the second matrix: ");
scanf("%d",&rows2);
printf("\n Enter the numbers of columns in the second matrix:");
scanf("%d",&cols2);
if(rows1 != rows2 || cols1 != cols2)
{
printf("\n The number of rows and columns of both the matrices must be equal");
getch();
}
rows_sum = rows1;
cols_sum = cols1;
printf("\n Enter the elements of the first matrix");
printf("\n *************************");
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
scanf("%d",&mat1[i][j]);
}
printf("\n Enter the elements of the second matrix");
printf("\n *************************");
for(i=0;i<rows2;i++)
{
for(j=0;j<cols2;j++)
scanf("%d",&mat2[i][j]);
}
for(i=0;i<rows_sum;i++)
{
for(j=0;j<cols_sum;j++)
sum[i][j] = mat1[i][j] + mat2[i][j];
}
printf("\n The elements of the resultant matrix are");
printf("\n *************************");
for(i=0;i<rows_sum;i++)
{
printf("\n");
for(j=0;j<cols_sum;j++)
printf("\t %d", sum[i][j]);
}
return 0;
}

/* 33. Write a program to multiply two m n matrices.*/


#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, k;
int rows1, cols1, rows2, cols2, res_rows, res_cols;
int mat1[5][5], mat2[5][5], res[5][5];
clrscr();
printf("\n Enter the number of rows in the first matrix: ");
scanf("%d",&rows1);
printf("\n Enter the number of columns in the first matrix: ");
scanf("%d",&cols1);
printf("\n Enter the number of rows in the second matrix: ");
scanf("%d",&rows2);
printf("\n Enter the number of columns in the second matrix:");
scanf("%d",&cols2);
if(cols1 != rows2)
{
printf("\n The number of columns in the first matrix must be equal to the number of rows in the second
matrix");
getch();
}
res_rows = rows1;
res_cols = cols2;
printf("\n Enter the elements of the first matrix");
printf("\n *************************");
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
scanf("%d",&mat1[i][j]);
}
printf("\n Enter the elements of the second matrix");
printf("\n *************************");
for(i = 0; i < rows2;i++)
{
for(j = 0; j < cols2;j++)
scanf("%d",&mat2[i][j]);
}
for(i = 0; i < res_rows;i++)
{
j=0;
for(j= 0; j < res_cols;j++)
{
res[i][j]=0;
for(k = 0; k < res_cols; k++)
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
printf("\n The elements of the product matrix are");

printf("\n *************************");
for(i=0;i<res_rows;i++)
{
printf("\n");
for(j = 0; j < res_cols;j++)
printf("\t %d",res[i][j]);
}
return 0;
}
/* 34. Write a menu-driven program to read and display an m n matrix. Also fi nd the sum,
transpose, and product of two m X n matrices.*/
#include <stdio.h>
#include <conio.h>
void read_matrix(int mat[2][2], int, int);
void sum_matrix(int mat1[2][2], int mat2[2][2], int, int);
void mul_matrix(int mat1[2][2], int mat2[2][2], int, int);
void transpose_matrix(int mat2[2][2], int, int);
void display_matrix(int mat[2][2], int r, int c);
int main()
{
int option, row, col;
int mat1[2][2], mat2[2][2];
clrscr();
do
{
printf("\n ******* MAIN MENU ********");
printf("\n 1. Read the two matrices");
printf("\n 2. Add the matrices");
printf("\n 3. Multiply the matrices");
printf("\n 4. Transpose the matrix");
printf("\n 5. EXIT");
printf("\n\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &row, &col);
printf("\n Enter the first matrix: ");
read_matrix(mat1, row, col);
printf("\n Enter the second matrix: ");
read_matrix(mat2, row, col);
break;
case 2:
sum_matrix(mat1, mat2, row, col);
break;
case 3:
if(col == row)
mul_matrix(mat1, mat2, row, col);
else

printf("\n To multiply two matrices, number of columns in the first matrix must be equal to number of
rows in the second matrix");
break;
case 4:
transpose_matrix(mat1, row, col);
break;
}
}while(option != 5);
getch();
return 0;
}
void read_matrix(int mat[2][2], int r, int c)
{
int i, j;
for(i = 0;i < r;i++)
{ printf("\n");
for(j = 0;j < c;j++)
{
printf("\t mat[%d][%d] = ",i,j);
scanf("%d", &mat[i][j]);
}
}
}
void sum_matrix(int mat1[2][2], int mat2[2][2], int r, int c)
{
int i, j, sum[2][2];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
sum[i][j] = mat1[i][j] + mat2[i][j];
}
display_matrix(sum, r, c);
}
void mul_matrix(int mat1[2][2], int mat2[2][2], int r, int c)
{
int i, j, k, prod[2][2];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
prod[i][j] = 0;
for(k=0;k<c;k++)
prod[i][j] += mat1[i][k] * mat2[k][j];
}
}
display_matrix(prod, r, c);
}
void transpose_matrix(int mat[2][2], int r, int c)
{
int i, j, tp_mat[2][2];
for(i=0;i<r;i++)

{
for(j=0;j<c;j++)
tp_mat[j][i] = mat[i][j];
}
display_matrix(tp_mat, r, c);
}
void display_matrix(int mat[2][2], int r, int c)
{
int i, j;
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("\t mat[%d][%d] = %d", i, j, mat[i][j]);
}
}
/* 35. Write a program to fi ll a square matrix with value zero on the diagonals, 1 on the upper
right triangle, and 1 on the lower left triangle.*/
#include <stdio.h>
#include <conio.h>
void read_matrix(int mat[5][5], int);
void display_matrix(int mat[5][5], int);
int main()
{
int row;
int mat1[5][5], mat2[5][5];
clrscr();
printf("\n Enter the number of rows and columns of the matrix ");
scanf("%d", &row);
read_matrix(mat1, row);
display_matrix(mat1, row);
getch();
return 0;
}
void read_matrix(int mat[5][5], int r)
{
int i, j;
for(i = 0;i < r;i++)
{
for(j = 0;j < r;j++)
{
if(i==j)
mat[i][j] = 0;
if(i>j)
mat[i][j] = -1;
else
mat[i][j] = 1;
}
}
}

void display_matrix(int mat[5][5], int r)


{
int i, j;
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<r;j++)
printf("\t mat[%d][%d] = %d", i, j, mat[i][j]);
}
}
/* 36. Write a program to read and display a 2 x 2 x 2 array.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int array1[3][3][3], i, j, k;
clrscr();
printf("\n Enter the elements of the matrix");
printf("\n ******************************");
for(i = 0;i < 2;i++)
{
for(j = 0;j < 2;j++)
{
for(k = 0;k < 2;k++)
{
printf("\n array[%d][ %d][ %d] = ", i, j, k);
scanf("%d", &array1[i][j][k]);
}
}
}
printf("\n The matrix is: ");
printf("\n *******************************");
for(i = 0;i < 2;i++)
{
printf("\n\n");
for(j = 0;j < 2;j++)
{
printf("\n");
for(k = 0;k < 2;k++)
printf("\tarray[%d][%d] %d] = %d", i, j, k, array1[i][j][k]);
}
}
getch();
return 0;
}

Strings
/* 1. Write a program to display a string using printf().*/
#include <stdio.h>
#include <conio.h>
main()
{
char str[] = "Introduction to C";
clrscr();
printf("\n |%s|", str);
printf("\n |%20s|", str);
printf("\n |%-20s|", str);
printf("\n |%.4s|", str);
printf("\n |%20.4s|", str);
printf("\n |%-20.4s|", str);
getch();
return 0;
}
/* 2. Write a program to read and display a string.*/
#include <stdio.h>
#include <conio.h>
main()
{
char str[50];
printf("\n Enter the string: ");
scanf("%s", str);
clrscr();
printf("\n |%s|", str);
printf("\n |%20s|", str);
printf("\n |%-20s|", str);
printf("\n |%.4s|", str);
printf("\n |%20.4s|", str);
printf("\n |%-20.4s|", str);
getch();
return 0;
}
/* 3. Write a program to print the following pattern.
H
HE
HEL
HELL
HELLO
HELLO
HELL
HEL
HE
H*/
#include <stdio.h>
#include <conio.h>

main()
{
int i, w, p;
char str[] = "HELLO";
printf("\n");
for(i=0;i<=5;i++)
{
p = i+1;
printf("\n %-5.*s", p, str);
}
printf("\n");
for(i=5;i>=0;i--)
{
p = i+1;
printf("\n %-5.*s", p, str);
}
getch();
return 0;
}
/* 4. Write a program to print the following pattern.
H
HE
HEL
HELL
HELLO
HELLO
HELL
HEL
HE
H */
#include <stdio.h>
#include <conio.h>
main()
{
int i, w, p;
char str[] = "HELLO";
printf("\n");
for(i = 0;i <= 5;i++)
{
p = i+1;
printf("\n %5.*s", p, str);
}
printf("\n");
for(i = 5;i >= 0;i--)
{
p = i+1;
printf("\n %5.*s", p, str);
}
getch();
return 0;

}
/* 5. Write a program to fi nd the length of a string.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100], i = 0, length;
clrscr();
printf("\n Enter the string :");
gets(str);
while(str[i] != '\0')
i++;
length = i;
printf("\n The length of the string is : %d", length);
getch();
}
/* 6. Write a program to convert characters of a string to upper case.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100];
char upper_str [100];
int i=0, j;
clrscr();
printf("\n Enter the string:");
gets(str);
while(str[i] != '\0')
{
if(str[i]>='a' && str[i]<='z')
upper_str[j] = str[i] -32;
else
upper_str[i] = str[i];
i++;
}
upper_str[i] = '\0';
printf("\n The string converted into upper case is : ");
puts(upper_str);
return 0;
}
/* 7. Write a program to convert characters of a string into lower case.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100], lower_str [100];
int i = 0, j;
clrscr();

printf("\n Enter the string :");


gets(str);
while(str[i] != '\0')
{
if(str[i]>='A' && str[i]<='Z')
lower_str[j] = str[i] + 32;
else
lower_str[i] = str[i];
i++;
}
lower_str[i] = '\0';
printf("\n The string converted into lower case is : ");
puts(lower_str);
return 0;
}
/* 8. Write a program to concatenate two Strings.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str1[100], str2[100], str3[100];
int i=0, j=0;
clrscr();
printf("\n Enter the first string : ");
gets(str1);
printf("\n Enter the second string : ");
gets(str2);
while(str1[i] != '\0')
{
str3[j] = str1[i];
i++;
j++;
}
i=0;
while(str2[i] != '\0')
{
str3[j] = str2[i];
i++;
j++;
}
str3[j] = '\0';
printf("\n The concatenated string is:");
puts(str3);
getch();
return 0;
}

/* 9. Write a program to append a string to another string.*/


#include <stdio.h>
#include <conio.h>
main()
{
char Dest_Str[100], Source_Str[50];
int i = 0, j = 0;
clrscr();
printf("\n Enter the source string : ");
gets(Source_Str);
printf("\n Enter the destination string:");
gets(Dest_Str);
while(Dest_Str[i] != '\0')
i++;
while(Source_Str[j] != '\0')
{
Dest_Str[i] = Source_Str[j];
i++;
j++;
}
Dest_Str[i] = '\0';
printf("\n After appending, the destination string is: ");
puts(Dest_Str);
getch();
return 0;
}
/* 10. Write a program to compare two strings.*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char str1[50], str2[50];
int i=0, len1 = 0, len2 = 0, same = 0;
clrscr();
printf("\n Enter the first string : ");
gets(str1);
printf("\n Enter the second string : ");
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
if(len1 == len2)
{
while(i<len1)
{
if(str1[i] == str2[i])
i++;
else break;

}
if(i==len1)
{
same=1;
printf("\n The two strings are equal");
}
}
if(len1!=len2)
printf("\n The two strings are not equal");
if(same == 0)
{
if(str1[i]>str2[i])
printf("\n String1 is greater than string2");
else if(str1[i]<str2[i])
printf("\n String2 is greater than string1");
}
getch();
return 0;
}
/* 11. Write a program to reverse the given string.*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char str[100], reverse_str[100], temp;
int i = 0, j = 0;
clrscr();
printf("\n Enter the string: ");
gets(str);
j=strlen(str)-1;
while(i<j)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
i++;
j--;
}
printf("\n The reversed string is: ");
puts(str);
getch();
return 0;
}
/* 12. Write a program to extract the fi rst N characters of a string.*/
#include <stdio.h>
#include <conio.h>
int main()
{

char str[100], substr[100];


int i=0, n;
clrscr();
printf("\n Enter the string: ");
gets(str);
printf("\n Enter the number of characters to be copied: ");
scanf("%d", &n);
i = 0;
while(str[i] != '\0' && i< n)
{
substr[i] = str[i];
i++;
}
substr [i] = '\0';
printf("\n The substring is: ");
puts(substr);
getch();
return 0;
}
/*13. Write a program to extract the last N characters of a string.*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char str[100], substr[100];
int i=0, j=0, n;
clrscr();
printf("\n Enter the string : ");
gets(str);
printf("\n Enter the number of characters to be copied: ");
scanf("%d", &n);
j = strlen(str) - n+1;
while(str[j] != '\0')
{
substr[i] = str[j];
i++, j++;
}
substr [i] = '\0';
printf("\n The substring is : ");
puts(substr);
getch();
return 0;
}
/* 14. Write a program to extract the substring from a given string.*/
#include <stdio.h>
#include <conio.h>
int main()
{

char str[100], substr[100];


int i=0, j=0, n, m;
clrscr();
printf("\n Enter the main string: ");
gets(str);
printf("\n Enter the position from which to start the substring: ");
scanf("%d", &m);
printf("\n Enter the length of the substring: ");
scanf("%d", &n);
i=m;
while(str[i] != '\0' && n>=0)
{
substr[j] = str[i];
i++;
j++;
n--;
}
substr[j] = '\0';
printf("\n The substring is : ");
puts(str);
getch();
return 0;
}
/* 15. Write a program to insert a string in the main text.*/
#include <stdio.h>
#include <conio.h>
main()
{
char text[100], str[20], ins_text[100];
int i=0, j=0, k=0, pos;
clrscr();
printf("\n Enter the main text : ");
gets(text);
printf("\n Enter the string to be inserted : ");
gets(str);
printf("\n Enter the position at which the string has to be inserted: ");
scanf("%d", &pos);
while(text[i]!='\0')
{
if(i==pos)
{
while(str[k]!='\0')
{
ins_text[j]=str[k];
j++;
k++;
}
}
else
{

ins_text[j]=text[i];
j++;
}
i++;
}
ins_text[j]='\0';
printf("\n The new string is: ");
puts(ins_text);
getch();
return 0;
}
/* 16. Write a program to delete a substring from a text.*/
#include <stdio.h>
#include <conio.h>
main()
{
char text[200], str[20], new_text[200], new_str[200];
int i=0, j=0, found=0, k, n=0, copy_loop=0;
clrscr();
printf("\n Enter the main text: ");
gets(text);
fflush(stdin);
printf("\n Enter the string to be deleted: ");
gets(str);
fflush(stdin);
while(text[i]!='\0')
{
j=0, found= 0, k=i;
while(text[k]==str[j] && str[j]!='\0')
{
k++;
j++;
}
if(str[j]=='\0')
copy_loop=k;
new_text[n] = text[copy_loop];
i++;
copy_loop++;
n++;
}
new_str[n]='\0';
printf("\n The new string is: ");
puts(new_str);
getch();
return 0;
}

/* 17. Write a program to replace a pattern with another pattern in the text.*/
#include <stdio.h>
#include <conio.h>
main()
{
char str[200], pat[20], new_str[200], rep_pat[100];
int i=0, j=0, k, n=0, copy_loop=0, rep_index=0;
clrscr();
printf("\n Enter the string: ");
gets(str);
fflush(stdin);
printf("\n Enter the pattern: ");
gets(pat);
fflush(stdin);
printf("\n Enter the replace pattern: ");
gets(rep_pat);
while(str[i]!='\0')
{
j=0,k=i;
while(str[k]==pat[j] && pat[j]!='\0')
{
k++;
j++;
}
if(pat[j]=='\0')
{
copy_loop=k;
while(rep_pat[rep_index] !='\0')
{
new_str[n] = rep_pat[rep_index];
rep_index++;
n++;
}
}
new_str[n] = str[copy_loop];
i++;
copy_loop++;
n++;
}
new_str[n]='\0';
printf("\n The new string is: ");
puts(new_str);
getch();
return 0;
}

/* 18. Write a program to read and print the names of n students of a class.*/
#include <stdio.h>
#include <conio.h>
main()
{
char names[5][10];
int i, n;
clrscr();
printf("\n Enter the number of students:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the name of student %d: ", i+1);
fflush(stdin);
gets(names[i]);
}
printf("\n Names of the students are:\n");
for(i = 0;i < n;i++)
puts(names[i]);
getch();
return 0;
}
/* 19. Write a program to sort names of students.*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char names[5][10], temp[10];
int i, n, j;
clrscr();
printf("\n Enter the number of students: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the name of the student %d: ", i+1);
fflush(stdin);
gets(names[i]);
}
for(i = 0;i < n;i++)
{
for(j = 0;j < n-i-1;j++)
{
if(strcmp(names[j], names[j+1])>0)
{
strcpy(temp, names[j]);
strcpy(names[j], names[j+1]);
strcpy(names[j+1], temp);
}

}
}
printf("\n Names of the students are: ");
for(i=0;i<n;i++)
puts(names[i]);
getch();
return 0;
}
/* 20. Write a program to read a sentence until a . is entered.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000];
int i=0;
clrscr();
printf("\n Enter . to end");
printf("\n **************");
printf("\n Enter the sentence: ");
scanf("%c", &str[i]);
while(str[i] != '.')
{
i++;
scanf("%c", &str[i]);
}
str[i] = '\0';
printf("\n The text is: ");
i=0;
while(str[i]!= '\0')
{
printf("%c", str[i]);
i++;
}
return 0;
}
/* 21. Write a program to read a line until a newline character is entered.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000];
int i=0;
clrscr();
printf("\n Enter a new line character to end");
printf("\n **************");
printf("\n Enter the line of text: ");
scanf("%c", &str[i]);
while(str[i]!= '\n')
{

i++;
scanf("%c", &str[i]);
}
str[i] = '\0';
printf("\n The text is: ");
i=0;
while(str[i]!= '\0')
{
printf("%c", str[i]);
i++;
}
return 0;
}
/* 22. Write a program to read and print the text until a * is encountered. Also count the number of
characters in the text entered.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000];
int i=0;
clrscr();
printf("\n Enter * to end");
printf("\n **************");
printf("\n Enter the text: ");
scanf("%c", &str[i]);
while(str[i] != '*')
{
i++;
scanf("%c", &str[i]);
}
str[i] = '\0';
printf("\n The text is: ");
i=0;
while(str[i]!= '\0')
{
printf("%c", str[i]);
i++;
}
printf("\n The count of characters is: %d",i-1);
return 0;
}
/* 23. Write a program to read a sentence. Then count the number of words in the sentence.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000];
int i=0, count=1;

clrscr();
printf("\n Enter the sentence: ");
gets(str);
while(str[i] != '\0')
{
if(str[i] == ' ' && str[i+1] != ' ')
count++;
i++;
}
printf("\n The total count of words is: %d", count);
return 0;
}
/* 24. Write a program to read multiple lines of text until a * is entered. Then count the number of
characters, words, and lines in the text.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000];
int i=0, word_count = 1, line_count =1, char_count = 1;
clrscr();
printf("\n Enter a * to end");
printf("\n **************");
printf("\n Enter the text: ");
scanf("%c", &str[i]);
while(str[i] != '*')
{
i++;
scanf("%c", &str[i]);
}
str[i] = '\0';
i=0;
while(str[i] != '\0')
{
if(str[i] == '\n' || i==79)
line_count++;
if(str[i] == ' ' &&str[i+1] != ' ')
word_count++;
char_count++;
i++;
}
printf("\n The total count of words is: %d", word_count);
printf("\n The total count of lines is: %d", line_count);
printf("\n The total count of characters is: %d", char_count);
return 0;
}

/* 25. Write a program to copy a string into another using strcpy function.*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
char str[100], copy_str[100];
int i=0, n;
clrscr();
printf("\n Enter the string: ");
gets(str);
strcpy(copy_str, str);
printf("\n The copied text is: ");
puts(copy_str);
return 0;
}
/* 26. Write a program to copy n characters of a string from the mth position in another string.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000], copy_str[1000];
int i=0, j=0, m, n;
clrscr();
printf("\n Enter the text: ");
gets(str);
printf("\n Enter the position from which to start: ");
scanf("%d", &m);
printf("\n Enter the number of characters to be copied: ");
scanf("%d", &n);
i = m;
while(str[i] != '\0' && n>0)
{
copy_str[j] = str[i];
i++;
j++;
n--;
}
copy_str[j] = '\0';
printf("\n The copied text is: ");
puts(copy_str);
return 0;
}

/* 27. Write a program to enter a text that has commas. Replace all the commas with semi colons
and then display the text.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000], copy_str[1000];
int i=0;
clrscr();
printf("\n Enter the text: ");
gets(str);
while(str[i] != '\0')
{
if(str[i] ==',')
copy_str[i] = ';';
else
copy_str[i] = str[i];
i++;
}
copy_str[i] = '\0';
printf("\n The copied text is: ");
i=0;
while(copy_str[i] != '\0')
{
printf("%c", copy_str[i]);
i++;
}
return 0;
}
/* 28. Write a program to enter a text that contains multiple lines. Rewrite this text by printing line
numbers before the text of the line starts.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000];
int i=0, linecount = 1;
clrscr();
printf("\n Enter a * to end");
printf("\n **************");
printf("\n Enter the text: ");
scanf("%c", &str[i]);
while(str[i] != '*')
{
i++;
scanf("%c", &str[i]);
}
str[i] = '\0';
i=0;

while(str[i] != '\0')
{
if(linecount == 1 && i == 0)
printf("\n %d\t", linecount);
if(str[i] == '\n')
{
linecount++;
printf("\n %d\t", linecount);
}
printf("%c", str[i]);
i++;
}
return 0;
}
/* 29. Write a program to enter a text that contains multiple lines. Display the n lines of text
starting from the mth line.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[1000];
int i = 0, m, n, j, linecount = 0;
clrscr();
printf("\n Enter the text: ");
scanf("%c", &str[i]);
while(str[i]!='*')
{
i++;
scanf("%c", &str[i]);
}
str[i+1] = '\0';
printf("\n Enter the line number from which to copy : ");
scanf("%d", &m);
printf("\n Enter the line number till which to copy : ");
scanf("%d", &n);
i=0;
while(str[i] != '\0')
{
if(linecount == m )
{
j = i;
while(n>0)
{
printf("%c", str[j]);
j++;
if(str[j] == '\n')
{
n--;
linecount++;
printf("%d \t", linecount);

}
}
}
else
{
i++;
if(str[i] == '\n')
linecount++;
}
}
getch();
return 0;
}
/* 30. Write a program to enter a text. Then enter a pattern and count the number of times the
pattern is repeated in the text.*/
#include <stdio.h>
#include <conio.h>
main()
{
char str[200], pat[20];
int i=0, j=0, found=0, k, count=0;
clrscr();
printf("\n Enter the string: ");
gets(str);
printf("\n Enter the pattern: ");
gets(pat);
while(str[i]!='\0')
{
j=0, k=i;
while(str[k]==pat[j] && pat[j]!='\0')
{
k++;
j++;
}
if(pat[j]=='\0')
{
found=1;
count++;
}
i++;
}
if(found==1)
printf("\n PATTERN FOUND %d TIMES", count);
else
printf("\n PATTERN NOT FOUND");
return 0;
}

/* 31. Write a program to enter a text. Then enter a pattern, if the pattern exists in the text then
delete the text and display it.*/
#include <stdio.h>
#include <conio.h>
main()
{
char str[200], pat[20], new_str[200];
int i=0, j=0, found=0, k, n=0, copy_loop=0;
clrscr();
printf("\n Enter the string: ");
gets(str);
fflush(stdin);
printf("\n Enter the pattern: ");
gets(pat);
fflush(stdin);
while(str[i]!='\0')
{
j=0, found= 0, k=i;
while(str[k]==pat[j] && pat[j]!='\0')
{
k++;
j++;
}
if(pat[j]=='\0')
copy_loop=k;
new_str[n] = str[copy_loop];
i++;
copy_loop++;
n++;
}
new_str[n]='\0';
printf("\n The new string is: ");
puts(new_str);
return 0;
}
/* 32. Write a program to enter a text. Then enter a pattern, if the pattern exists in the text then
replace it with another pattern and then display the text.*/
#include <stdio.h>
#include <conio.h>
main()
{
char str[200], pat[20], new_str[200], rep_pat[100];
int i = 0, j = 0, k, n = 0, copy_loop=0, rep_index=0;
clrscr();
printf("\n Enter the string: ");
gets(str);
fflush(stdin);
printf("\n Enter the pattern: ");
gets(pat);

fflush(stdin);
printf("\n Enter the replace pattern: ");
gets(rep_pat);
while(str[i]!='\0')
{
j=0,k=i;
while(str[k]==pat[j] && pat[j]!='\0')
{
k++;
j++;
}
if(pat[j]=='\0')
{
copy_loop=k;
while(rep_pat[rep_index] !='\0')
{
new_str[n] = rep_pat[rep_index];
rep_index++;
n++;
}
}
new_str[n] = str[copy_loop];
i++;
copy_loop++;
n++;
}
new_str[n]='\0';
printf("\n The new string is: ");
puts(new_str);
return 0;
}
/* 33. Write a program to fi nd whether a given string is a palindrome or not.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100];
int i = 0, j, length;
clrscr();
printf("\n Enter the string: ");
scanf("%d", &str[i]);
while(str[i] != '*')
{
i++;
scanf("%c", str[i]);
}
str[i] = '\0';
length = i;
i=0;
j = length - 1;

while(i <= length/2)


{
if(str[i] == str[j])
{
i++;
j--;
}
else
break;
}
if(i>=j)
printf("\n PALINDROME");
else
printf("\n NOT A PALINDROME");
getch();
return 0;
}
/* 34. Write a program to implement a quiz program.*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
main()
{
char quest[5][100];
char option1[3][20],option2[3][20], option3[3][20],option4[3][20], option5[3][20];
int response[5], correct_ans[5], option,
i, marks;
clrscr();
strcpy(quest[0],"Name the capital of India");
strcpy(option1[0],"1. Mumbai");
strcpy(option1[1],"2. New Delhi");
strcpy(option1[2],"3. Chennai");
correct_ans[0] = 1;
strcpy(quest[1],"Name the national bird of India");
strcpy(option2[0],"1. Peacock");
strcpy(option2[1],"2. Sparrow");
strcpy(option2[2],"3. Parrot");
correct_ans[1]=0;
strcpy(quest[2],"Name the first prime minister of India");
strcpy(option3[0],"1. M D Gandhi");
strcpy(option3[1],"2. S D Sharma");
strcpy(option3[2],"3. J L Nehru");
correct_ans[2]=2;
strcpy(quest[3],"Name the first female president of India");
strcpy(option4[0],"1. Pratibha Patil");
strcpy(option4[1],"2. Sonia Gandhi");
strcpy(option4[2],"3. Indira Gandhi");
correct_ans[3] = 0;
strcpy(quest[4],"Name the youngest prime minister of India");
strcpy(option5[0],"1. Rajiv Gandhi");

strcpy(option5[1],"2. Sanjay Gandhi");


strcpy(option5[2],"3. Rahul Gandhi");
correct_ans[4] = 0;
do
{
printf("\n\n\n\n QUIZ PROGRAM");
printf("\n*******************");
printf("\n 1. Display Questions");
printf("\n 2. Display Correct Answers");
printf("\n 3. Display Result");
printf("\n 4. EXIT");
printf("\n *************************");
printf("\n\n\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n %s \n", quest[0]);
for(i=0;i<3;i++)
printf("\n %s", option1[i]);
printf("\n\n Enter your answer number: ");
scanf("%d", &response[0]);
printf("\n %s \n", quest[1]);
for(i=0;i<3;i++)
printf("\n %s", option2[i]);
printf("\n\n Enter your answer number: ");
scanf("%d", &response[1]);
printf("\n %s \n", quest[2]);
for(i=0;i<3;i++)
printf("\n %s", option3[i]);
printf("\n\n Enter your answer number: ");
scanf("%d", &response[2]);
printf("\n %s \n", quest[3]);
for(i=0;i<3;i++)
printf("\n %s", option4[i]);
printf("\n\n Enter your answer number: ");
scanf("%d", &response[3]);
printf("\n %s \n", quest[4]);
for(i=0;i<3;i++)
printf("\n %s", option5[i]);
printf("\n\n Enter your answer number: ");
scanf("%d", &response[4]);
break;
case 2:
printf("\n\n CHECK THE CORRECT ANSWERS");
printf("\n ************************");
printf("\n %s \n %s",quest[0],option1[correct_ans[0]]);
printf("\n\n %s \n %s",quest[1],option2[correct_ans[1]]);
printf("\n\n %s \n %s",quest[2],option3[correct_ans[2]]);
printf("\n\n %s \n %s",quest[3],option4[correct_ans[3]]);
printf("\n\n %s \n %s",quest[4],option5[correct_ans[4]]);

break;
case 3:
marks = 0;
for(i = 0;i <= 4;i++)
{
if(correct_ans[i]+1 == response[i])
marks++;
}
printf("\n Out of 5 you score %d", marks);
break;
}
}
while(option!=4);
getch();
return 0;
}

Pointers
/* 1. Write a program to find the size of various data types on your system.*/
#include <stdio.h>
int main()
{
printf("\n The size of short integer is : %d", sizeof(short int));
printf("\n The size of unsigned integer is: %d", sizeof(unsigned int));
printf("\n The size of signed integer is: %d", sizeof(signed int));
printf("\n The size of integer is: %d", sizeof(int));
printf("\n The size of long integer is: %d", sizeof(long int));
printf("\n The size of character is: %d", sizeof(char));
printf("\n The size of unsigned character is: %d", sizeof(unsigned char));
printf("\n The size of signed character is: %d", sizeof(signed char));
printf("\n The size of floating point number is: %d", sizeof(float));
printf("\n The size of double number is: %d", sizeof(double));
return 0;
}
/* 2. Write a program to print Hello World, using pointers.*/
#include <stdio.h>
int main()
{
char *ch = "Hello World";
printf("%s", ch);
return 0;
}
/* 3. Write a program to add two fl oating point numbers. The result should contain only two digits
after the decimal.*/
#include <stdio.h>
int main()
{
float num1, num2, sum = 0.0;
float *pnum1 = &num1, *pnum2 = &num2, *psum = &sum;
printf("\n Enter the two numbers: ");
scanf("%f %f", pnum1, pnum2); // pnum1 = &num1;
*psum = *pnum1 + *pnum2;
printf("\n %f + %f = %.2f", *pnum1, *pnum2, *psum);
return 0;
}
/* 4. Write a program to calculate area of a circle.*/
#include <stdio.h>
#include <conio.h>
int main()
{
double radius, area = 0.0;

double *pradius = &radius, *parea = &area;


printf("\n Enter the radius of the circle: ");
scanf("%lf", pradius);
*parea = 3.14 * (*pradius) * (*pradius);
printf("\n The area of the circle with radius %.2lf = %.2lf", *pradius, *parea);
return 0;
}
/* 5. Write a program to convert a fl oating point number into an integer.*/
#include <stdio.h>
int main()
{
float fnum, *pfnum = &fnum;
int num, *pnum = &num;
printf("\n Enter the floating point no.: ");
scanf("%f", &fnum);
*pnum = (int)*pfnum;
printf("\n The integer equivalent of %f = %d", *pfnum, *pnum);
return 0;
}
/* 6. Write a program to fi nd the biggest of three numbers.*/
#include <stdio.h>
int main()
{
int num1, num2, num3;
int *pnum1 = &num1, *pnum2 = &num2, *pnum3= &num3;
printf("\n Enter the first number: ");
scanf("%d", pnum1);
printf("\n Enter the second number: ");
scanf("%d", pnum2);
printf("\n Enter the third number: ");
scanf("%d", pnum3);
if(*pnum1 > *pnum2 && *pnum1 > *pnum3)
printf("\n %d is the largest number", *pnum1);
if(*pnum2 > *pnum1 && *pnum2 > *pnum3)
printf("\n %d is the largest number", *pnum2);
else
printf("\n %d is the largest number", *pnum3);
return 0;
}
/* 7. Write a program to print a character. Also print its ASCII value and rewrite the character in upper
case.*/
#include <stdio.h>
#include <conio.h>
int main()

{
int ch, *pch = &ch;
clrscr();
printf("\n Enter the character: ");
scanf("%c", &ch);
printf("\n The char entered is: %c", *pch);
printf("\n ASCII value of the char is: %d", *pch);
printf("\n The char in upper case is: %c", *pch - 32);
getch();
return 0;
}
/* 8. Write a program to enter a character and then determine whether it is a vowel or not.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch, *pch = &ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c", pch);
if(*pch=='a' || *pch =='e' || *pch=='i' || *pch=='o' || *pch=='u' || *pch=='A' || *pch=='E' || *pch=='I'
|| *pch=='O' || *pch=='U')
printf("\n %c is a VOWEL", ch);
else
printf("\n %c is not a vowel", *pch);
getch();
return 0;
}
/* 9. Write a program which takes an input from the user and then checks whether it is a number or a
character. If it is a character, determine whether it is in upper case or lower case.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch, *pch = &ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c", pch);
if(*pch >='A' && *pch<='Z')
printf("\n Upper case char was entered");
if(*pch >='a' && *pch<='z')
printf("\n Lower case char was entered");
else if(*pch>='0' && *pch<='9')
printf("\n You entered a number");
getch();
return 0;

}
/* 10. Write a program using pointer variables to read a character until * is entered. If the character is
in upper case, print it in lower case and vice versa. Also count the number of upper and lower case
characters entered.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch, *pch = &ch;
int upper = 0, lower = 0;
clrscr();
printf("\n Enter the character: ");
scanf("%c", pch);
while(*pch != '*')
{
if(*pch >= 'A' && *pch <= 'Z')
{
*pch += 32;
upper++;
}
if(*pch >= 'a' && *pch <= 'z')
{
*pch -= 32;
lower++;
}
printf("%c", *pch);
fflush(stdin);
printf("\n Enter the character: ");
scanf("%c", pch);
}
printf("\n Total number of upper case characters = %d", upper);
printf("\n Total number of lower case characters = %d", lower);
getch();
return 0;
}
/* 11. Write a program to test whether a number is positive, negative, or equal to zero.*/
#include <stdio.h>
int main()
{
int num, *pnum = &num;
printf("\n Enter any number: ");
scanf("%d", pnum);
if(*pnum>0)
printf("\n The number is positive");
else
{

if(*pnum<0)
printf("\n The number is negative");
else
printf("\n The number is equal to zero");
}
return 0;
}
/* 12. Write a program to display the sum and average of numbers from m to n.*/
#include <stdio.h>
int main()
{
int num, *pnum = &num, range;
int m, *pm = &m;
int n, *pn = &n;
int sum = 0, *psum = &sum;
float avg, *pavg = &avg;
printf("\n Enter the starting and ending limit of the numbers to be summed:");
scanf("%d %d", pm, pn);
range = n - m;
while(*pm <= *pn)
{
*psum = *psum + *pm;
*pm = *pm + 1;
}
printf("\n Sum of numbers = %d", *psum);
*pavg = *psum / range;
printf("\n Average of numbers = %f", *pavg);
return 0;
}
/* 13. Write a program to print all even numbers from mn.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int m, *pm = &m;
int n, *pn = &n;
printf("\n Enter the starting and ending limit of the numbers: ");
scanf("%d %d", pm, pn);
while(*pm <= *pn)
{
if(*pm %2 == 0)
printf("\n %d is even", *pm);
else
printf("\n %d is odd", *pm);
}
return 0;

}
/* 14. Write a program to read numbers until -1 is entered. Also display whether the number is prime
or composite.*/
#include <stdio.h>
int main()
{
int num, *pnum = &num;
int i, flag = 0;
printf("\n ***** ENTER 1 TO EXIT ******");
printf("\n Enter any number: ");
scanf("%d", pnum);
while(*pnum != -1)
{
if(*pnum == 1)
printf("\n %d is neither prime nor composite", *pnum);
else if(*pnum == 2)
printf("\n %d is prime", *pnum);
else
{
for(i=2; i<*pnum/2; i++)
{
if(*pnum/i == 0)
flag =1;
}
if(flag == 0)
printf("\n %d is prime", *pnum);
else
printf("\n %d is composite", *pnum);
}
printf("\n Enter any number: ");
scanf("%d", pnum);
}
return 0;
}
/* 15. Write a program to add two integers using functions.*/
#include <stdio.h>
#include <conio.h>
void sum (int *a, int *b, int *t);
int main()
{
int num1, num2, total;
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
sum(&num1, &num2, &total);

printf("\n Total = %d", total);


getch();
return 0;
}
void sum (int *a, int *b, int *t)
{
*t = *a + *b;
}
/* 16. Write a program, using functions, to fi nd the biggest of three integers.*/
#include <stdio.h>
int greater(int *a, int *b, int *c, int *large);
int main()
{
int num1, num2, num3, large;
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
printf("\n Enter the third number: ");
scanf("%d", &num3);
greater(&num1, &num2, &num3, &large);
return 0;
}
int greater (int *a, int *b, int *c, int *large)
{
if(*a > *b && *a > *c)
*large = *a;
if(*b > *a && *b > *c)
*large = *b;
else
*large = *c;
printf("\n Largest number = %d", *large);
}
/* 17. Write a program to calculate area of a triangle.*/
#include <stdio.h>
void read(float *b, float *h);
void calculate_area (float *b, float *h, float *a);
int main()
{
float base, height, area;
read(&base, &height);
calculate_area(&base, &height, &area);
printf("\n Area of the triangle with base %f and height %f = %f", base, height, area);
return 0;
}
void read(float *b, float *h)

{
printf("\n Enter the base of the triangle: ");
scanf("%f", b);
printf("\n Enter the height of the triangle: ");
scanf("%f", h);
}
void calculate_area (float *b, float *h, float *a)
{
*a = 0.5 * (*b) * (*h);
}
/*18 Pointer Comparison*/
#include <stdio.h>
main()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int *ptr1, *ptr2;
ptr1 = arr;
ptr2 = &arr[8];
while(ptr1<=ptr2)
{
printf("%d", *ptr1);
ptr1++;
}
}
/* 19. Write a program to read and display an array of n integers.*/
#include <stdio.h>
int main()
{
int i, n, p;
int arr[10], *parr = arr;
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i = 0; i < n; i++)
scanf("%d", parr+i);
printf("\n The elements entered are: ");
for(i=0; i < n; i++)
printf("\t %d", *(parr+i));
return 0;
}
/* 20. Write a program to fi nd mean of n numbers using arrays.*/
#include <stdio.h>
int main()
{
int i, n, arr[20], sum =0;
int *pn = &n, *parr = arr, *psum = &sum;

float mean = 0.0, *pmean = &mean;


printf("\n Enter the number of elements in the array: ");
scanf("%d", pn);
for(i = 0; i < *pn; i++)
{
printf("\n Enter the number: ");
scanf("%d", (parr + i));
}
for(i=0; i < *pn; i++)
*psum += *(arr + i);
*pmean = *psum / *pn;
printf("\n The numbers you entered are: ");
for(i=0; i < *pn; i++)
printf("%d ", *(arr + i));
printf("\n The sum is: %d", *psum);
printf("\n The mean is: %f", *pmean);
return 0;
}
/* 21. Write a program to fi nd the largest of n numbers using arrays. Also display its position.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], large = -1111, pos = 0;
int *pn = &n, *parr = arr, *plarge = &large, *ppos = &pos;
clrscr();
printf("\n Enter the number of elements in the array: ");
scanf("%d", pn);
for(i = 0; i < *pn;i++)
{
printf("\n Enter the number: ");
scanf("%d", parr+i);
}
for(i = 0; i < *pn; i++)
{
if(*(parr+i) > *plarge)
{
*plarge = *(parr+i);
*ppos = i;
}
}
printf("\n The numbers you entered are: ");
for(i = 0; i < *pn;i++)
printf("%d ", *(parr+i));
printf("\n The largest of these numbers is: %d", *plarge);
printf("\n The position of the largest number in the array is: %d", *ppos);
return 0;

}
/* 22. Write a program to read and print an array of n numbers, then fi nd out the smallest number.
Also print the position of the smallest number.*/
#include <stdio.h>
void read_array(int *arr, int n);
void print_array(int *arr, int n);
void find_small(int *arr, int n, int *small, int *pos);
int main()
{
int num[10], n, small, pos;
printf("\n Enter the size of the array: ");
scanf("%d", &n);
read_array(num, n);
print_array(num, n);
find_small(num, n, &small, &pos);
printf("\n The smallest number in the array is at position %d", small, pos);
return 0;
}
void read_array(int *arr, int n)
{
int i;
printf("\n Enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
}
void print_array(int *arr, int n)
{
int i;
printf("\n The array elements are: ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
}
void find_small(int *arr, int n, int *small, int *pos)
{
int i;
for(i=0;i<n;i++)
{
if(*(arr+i)< *small)
{
*small = *(arr+i);
*pos = i;
}
}
}

/* 23. Write a program to read and print a text. Also count the number of characters, words, and lines
in the text.*/
#include <stdio.h>
int main()
{
char str[100], *pstr;
int chars = 1, lines = 1, words = 1;
pstr=str;
printf("\n Enter the string: ");
gets(str);
pstr = str;
while(*pstr != '\0')
{
if(*pstr == '\n')
lines++;
if(*pstr == ' ' && *(pstr + 1) != ' ')
words++;
chars++;
pstr++;
}
printf("\n The string is: ");
puts(str);
printf("\n Number of characters = %d", chars);
printf("\n Number of lines = %d", lines);
printf("\n Number of words = %d", words);
return 0;
}
/* 24. Write a program to copy a character array in another character array.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100], copy_str[100];
char *pstr, *pcopy_str;
int i = 0;
clrscr();
pstr = str;
pcopy_str = copy_str;
printf("\n Enter the string: ");
gets(str);
while(*pstr != '\0')
{
*pcopy_str = *pstr;
pstr++, pcopy_str++;
}
*pcopy_str = '\0';

printf("\n The copied text is: ");


pcopy_str = copy_str;
while(*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
getch();
return 0;
}
/* 25. Write a program to copy n characters of a character array from the mth position in another
character array.*/
#include <stdio.h>
int main()
{
char str[100], copy_str[100];
char *pstr, *pcopy_str;
int m, n, i = 0;
pstr = str;
pcopy_str = copy_str;
printf("\n Enter the string: ");
gets(pstr);
printf("\n Enter the position from which to start: ");
scanf("%d", &m);
printf("\n Enter the number of characters to be copied: ");
scanf("%d", &n);
pstr = pstr + m - 1;
i=0;
while(*pstr != '\0' && i <n)
{
*pcopy_str = *pstr;
pcopy_str++;
pstr++;
i++;
}
*pcopy_str = '\0';
printf("\n The copied text is: ");
puts(copy_str);
return 0;
}

/* 26. Write a program to copy the last n characters of a character array in another character array.
Also convert the lower case letters into upper case letters while copying.*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
char str[100], copy_str[100];
char *pstr, *pcopy_str;
int i = 0, n;
pstr = str;
pcopy_str = copy_str;
printf("\n Enter the string:");
gets(str);
printf("\n Enter the number of characters to be copied (from the end): ");
scanf("%d", &n);
pstr = pstr + len(str) - n;
while(*pstr != '\0')
{
*pcopy_str = *pstr - 32;
pstr++; pcopy_str++;
}
*pcopy_str = '\0';
printf("\n The copied text is: ");
puts(copy_str);
return 0;
}
/* 27. Write a program to read a text, delete all the semicolons it has, and fi nally replace all . with a
,.*/
#include <stdio.h>
int main()
{
char str[100], copy_str[100];
char *pstr, *pcopy_str;
pstr = str;
pcopy_str = copy_str;
printf("\n Enter the string: ");
gets(str);
pstr = str;
while(*pstr != '\0')
{
if(*pstr != ';')
{ } // do nothing
if (*pstr == '.')
*pcopy_str = ',';
else

*pcopy_str = *pstr;
pstr++; pcopy_str++;
}
*pcopy_str = '\0';
printf("\n The new text is: ");
pcopy_str = copy_str;
while(*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
return 0;
}
/* 28. Write a program to reverse a string.*/
#include <stdio.h>
int main()
{
char str[100], copy_str[100];
char *pstr, *pcopy_str;
pstr = str;
pcopy_str = copy_str;
printf("\n Enter * to end");
printf("\n **************");
printf("\n Enter the string: ");
scanf("%c", pstr);
while(*pstr != '*')
{
pstr++;
scanf("%c", pstr);
}
*pstr = '\0';
pstr--;
while (pstr >= str)
{
*pcopy_str = *pstr;
pcopy_str++;
pstr--;
}
*pcopy_str = '\0';
printf("\n The new text is: ");
pcopy_str = copy_str;
while(*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
return 0;

}
/* 29. Write a program to concatenate two strings.*/
#include <stdio.h>
#include <conio.h>
int main()
{
char str1[100], str2[100], copy_str[2000];
char *pstr1, *pstr2, *pcopy_str;
clrscr();
pstr1 = str1;
pstr2 = str2;
pcopy_str = copy_str;
printf("\n Enter the first string: ");
gets(str1);
printf("\n Enter the second string: ");
gets(str2);
pstr1=str1;
while(*pstr1 != '\0')
{
*pcopy_str = *pstr1;
pcopy_str++, pstr1++;
}
pstr2 = str2;
while(*pstr2 != '\0')
{
*pcopy_str = *pstr2;
pcopy_str++, pstr2++;
}
*pcopy_str = '\0';
printf("\n The new text is: ");
pcopy_str = copy_str;
while(*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
return 0;
}
/* 30. Write a program to read and display a 3 x 3 matrix.*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, mat[3][3];
clrscr();

printf("\n Enter elements of the matrix");


printf("\n *************************");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n mat[%d][%d] = ", i, j);
scanf("%d", &mat[i][j]);
}
}
printf("\n Elements of the matrix are");
printf("\n *************************");
for(i=0;i<3;i++)
{
printf("\n");
for(j = 0; j < 3; j++)
printf("\t mat[%d][%d] = %d",i,j, *(*(mat + i)+j));
}
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, mat[3][3];
clrscr();
printf("\n Enter elements of the matrix");
printf("\n *************************");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n mat[%d][%d] = ",i,j);
scanf("%d", (*(mat+i)+j));
}
}
printf("\n The elements of the matrix are ");
printf("\n *************************");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t mat[%d][%d] = %d",i,j, *(*(mat+i)+j));
}
return 0;
}
#include <stdio.h>
#include <conio.h>

void display(int (*)[3]);


int main()
{
int i, j, mat[3][3];
clrscr();
printf("\n Enter elements of the matrix");
printf("\n *************************");
for(i=0;i<3;i++)
{
for(j = 0; j < 3; j++)
{
printf("\n mat[%d][%d] = ",i,j);
scanf("%d", &mat[i][j]);
}
}
display(mat);
}
void display(int (*mat)[3])
{
int i,j;
printf("\n elements of the matrix are");
printf("\n *************************");
for(i = 0; i < 3; i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t mat[%d][%d] = %d",i,j, *(*(mat + i)+j));
}
return 0;
}
/* 31. Write a program to add, that uses an array of function pointers, subtract, multiply, or divide 2
given numbers.*/
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*fp[4]) (int a, int b);
int main(void)
{
int result;
int num1, num2, op;
fp[0] = sum;
fp[1] = subtract;
fp[2] = mul;
fp[3] = div;

printf("\n Enter the numbers: ");


scanf("%d %d", &num1, &num2);
do
{
printf("\n 0: Add \n 1: Subtract \n 2: Multiply \n 3: Divide \n 4. EXIT\n");
printf("\n\n Enter the operation: ");
scanf("%d", &op);
result = (*fp[op]) (num1, num2);
printf("\n Result = %d", result);
} while(op!=4);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
/* 32. Write a program to read and display values of an integer array. Allocate space dynamically for
the array.*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
main()
{
int i, n;
int *arr;
clrscr();
printf("\n Enter the number of elements ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
if(arr == NULL)
{
printf("\n Memory Allocation Failed");

exit(0);
}
for(i = 0;i < n;i++)
{
printf("\n Enter the value %d of the array: ", i);
scanf("%d", &arr[i]);
}
printf("\n The array contains \n");
for(i = 0;i < n;i++)
printf("%d", arr[i]); // another way is to write *(arr+i)
return 0;
}

Structure , Union and Enumerated data types


/* 1. Write a program using structures to read and display the information about a student.*/
#include <stdio.h>
#include <conio.h>
int main()
{
struct student
{
int roll_no;
char name[80];
float fees;
char DOB[80];
};
struct student stud1;
clrscr();
printf("\n Enter the roll number: ");
scanf("%d", &stud1.roll_no);
printf("\n Enter the name: ");
scanf("%s", &stud1.name);
printf("\n Enter the fees: ");
scanf("%f", &stud1.fees);
printf("\n Enter the DOB: ");
scanf("%s", &stud1.DOB);
printf("\n ********STUDENTS DETAILS*******");
printf("\n ROLL No. = %d", stud1.roll_no);
printf("\n NAME = %s", stud1.name);
printf("\n FEES = %f", stud1.fees);
printf("\n DOB = %s", stud1.DOB);
getch();
return 0;
}
/* 2. Write a program, using structures, to fi nd the biggest of three numbers.*/
#include <stdio.h>
#include <conio.h>
int main()
{
struct numbers
{
int a, b, c;
int largest;
};
struct numbers num;
clrscr();
printf("\n Enter the three numbers: ");
scanf("%d %d %d", &num.a, &num.b, &num.c);
if (num.a > num.b && num.a > num.c)
num.largest = num.a;
if (num.b > num.a && num.b > num.c)
num.largest = num.b;

if (num.c > num.a && num.c > num.b)


num.largest = num.c;
printf("\n The largest number is: %d", num.largest);
getch();
return 0;
}
/* 3. Write a program to read, display, add, and subtract two complex numbers.*/
#include <stdio.h>
#include <conio.h>
int main()
{
typedef struct complex
{
int real;
int imag;
}
COMPLEX;
COMPLEX c1, c2, sum_c, sub_c;
int option;
clrscr();
do
{
printf("\n *** MAIN MENU ***");
printf("\n 1. Read the complex nos.");
printf("\n 2. Display the complex nos.");
printf("\n 3. Add the complex nos.");
printf("\n 4. Subtract the complex nos.");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the real and imaginary parts of the first complex number: ");
scanf("%d %d", &c1.real, &c1.imag);
printf("\n Enter the real and imaginary parts of the second complex number: ");
scanf("%d %d", &c2.real, &c2.imag);
break;
case 2:
printf("\n The first complex number is: %d + %di", c1.real, c1.imag);
printf("\n The second complex number is: %d + %di", c2.real, c2.imag);
break;
case 3:
sum_c.real = c1.real + c2.real;
sum_c.imag = c1.imag + c2.imag;
printf("\n The sum of two complex numbers is: %d + %di", sum_c.real, sum_c.imag);
break;
case 4:
sub_c.real = c1.real - c2.real;
sub_c.imag = c1.imag - c2.imag;

printf("\n The difference between two complex numbers is: %d + %di", sub_c.real, sub_c.imag);
break;
}
}while(option != 5);
getch();
return 0;
}
/* 4. Write a program to enter two points and then calculate the distance between them.*/
#include <stdio.h>
#include <conio.h>
#include<math.h>
int main()
{
typedef struct point
{
int x, y;
}
POINT;
POINT p1, p2;
float distance;
clrscr();
printf("\n Enter the coordinates of the first point: ");
scanf("%d %d", &p1.x, &p1.y);
printf("\n Enter the coordinates of the second point: ");
scanf("%d %d", &p2.x, &p2.y);
distance = sqrt(pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2));
printf("\n The coordinates of the first point are: %dx %dy", p1.x, p1.y);
printf("\n The coordinates of the second point are: %dx %dy", p2.x, p2.y);
printf("\n Distance between p1 and p2 = %f", distance);
getch();
return 0;
}
/* 5. Write a program to read and display information of a student, using a structure within a
structure.*/
#include <stdio.h>
#include <conio.h>
int main()
{
struct DOB
{
int day;
int month;
int year;
};
struct student
{
int roll_no;
char name[100];
float fees;

struct DOB date;


};
struct student stud1;
clrscr();
printf("\n Enter the roll number: ");
scanf("%d", &stud1.roll_no);
printf("\n Enter the name: ");
scanf("%s", stud1.name);
printf("\n Enter the fees: ");
scanf("%f", &stud1.fees);
printf("\n Enter the DOB: ");
scanf("%d %d %d", &stud1.date.day, &stud1.date.month, &stud1.date.year);
printf("\n *** STUDENTS DETAILS ***");
printf("\n ROLL No. = %d", stud1.roll_no);
printf("\n NAME = %s", stud1.name);
printf("\n FEES = %f", stud1.fees);
printf("\n DOB = %d - %d - %d", stud1.date.day, stud1.date.month, stud1.date.year);
getch();
return 0;
}
/* 6. Write a program to read and display the information of all the students in the class.*/
#include <stdio.h>
#include <conio.h>
int main()
{
struct student
{
int roll_no;
char name[80];
int fees;
char DOB[80];
};
struct student stud[50];
int n, i;
clrscr();
printf("\n Enter the number of students: ");
scanf("%d", &n);
for(i = 0;i < n;i++)
{
printf("\n Enter the roll number: ");
scanf("%d", &stud[i].roll_no);
fflush(stdin);
printf("\n Enter the name: ");
gets(stud[i].name);
fflush(stdin);
printf("\n Enter the fees: ");
scanf("%d", &stud[i].fees);
fflush(stdin);
printf("\n Enter the DOB: ");
gets(stud[i].DOB);

fflush(stdin);
}
for(i = 0;i < n;i++)
{
printf("\n ********DETAILS OF STUDENT %d ******", i+1);
printf("\n ROLL No. = %d", stud[i].roll_no);
printf("\n NAME = %s", stud[i].name);
printf("\n FEES = %d", stud[i].fees);
printf("\n DOB = %s", stud[i].DOB);
}
getch();
return 0;
}
/* 7. Write a program to read and display the information of all the students in the class. Then edit
the details of the ith student and redisplay the entire information.*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
struct student
{
int roll_no;
char name[80];
int fees;
char DOB[80];
};
struct student stud[50];
int n, i, rolno, new_rolno;
int new_fees;
char new_DOB[80], new_name[80];
clrscr();
printf("\n Enter the number of students: ");
scanf("%d", &n);
for(i = 0;i < n;i++)
{
printf("\n Enter the roll number: ");
scanf("%d", &stud[i].roll_no);
fflush(stdin);
printf("\n Enter the name: ");
gets(stud[i].name);
fflush(stdin);
printf("\n Enter the fees: ");
scanf("%d", stud[i].fees);
fflush(stdin);
printf("\n Enter the DOB: ");
gets(stud[i].DOB);
fflush(stdin);
}
for(i = 0;i < n;i++)

{
printf("\n ********DETAILS OF STUDENT %d*******", i+1);
printf("\n ROLL No. = %d", stud[i].roll_no);
printf("\n NAME = %s", stud[i].name);
printf("\n FEES = %d", stud[i].fees);
printf("\n DATE OF BIRTH = %s", stud[i].DOB);
}
printf("\n Enter the roll no. of the student whose record has to be edited: ");
scanf("%d", &rolno);
printf("\n Enter the new roll number: ");
scanf("%d", &new_rolno);
printf("\n Enter the new name: ");
scanf("%s", new_name);
printf("\n Enter the new fees: ");
scanf("%d", &new_fees);
printf("\n Enter the new date of birth: ");
scanf("%s", new_DOB);
stud[rolno].roll_no = new_rolno;
strcpy(stud[rolno].name, new_name);
stud[rolno].fees = new_fees;
strcpy(stud[rolno].DOB,new_DOB);
for(i=0;i<n;i++)
{
printf("\n ********DETAILS OF STUDENT %d*******", i+1);
printf("\n ROLL No. = %d", stud[i].roll_no);
printf("\n NAME = %s", stud[i].name);
printf("\n FEES = %d", stud[i].fees);
printf("\n DATE OF BIRTH = %s", stud[i].DOB);
}
getch();
return 0;
}
/* 8. Write a program to read, display, add, and subtract two distances. Distance must be defi ned
using kms and metres.*/
#include <stdio.h>
#include <conio.h>
typedef struct distance
{
int kms;
int metres;
}DISTANCE;
DISTANCE add_distance(DISTANCE, DISTANCE);
DISTANCE subtract_distance(DISTANCE, DISTANCE);
DISTANCE d1, d2, d3, d4;
int main()
{
int option;
clrscr();
do
{

printf("\n *** MAIN MENU ***");


printf("\n 1. Read the distances ");
printf("\n 2. Display the distances");
printf("\n 3. Add the distances");
printf("\n 4. Subtract the distances");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the first distance in kms and metres: ");
scanf("%d %d", &d1.kms, &d1.metres);
printf("\n Enter the second distance kms and metres: ");
scanf("%d %d", &d2.kms, &d2.metres);
break;
case 2:
printf("\n The first distance is: %d kms %d metres", d1.kms, d1.metres);
printf("\n The second distance is: %d kms %d metres", d2.kms, d2.metres);
break;
case 3:
d3 = add_distance(d1, d2);
printf("\n The sum of two distances is: %d kms %d metres", d3.kms, d3.metres);
break;
case 4:
d4 = subtract_distance(d1, d2);
printf("\n The difference between two distances is: %d kms %d metres", d4.kms, d4.metres);
break;
}
}while(option != 5);
getch();
return 0;
}
DISTANCE add_distance(DISTANCE d1, DISTANCE d2)
{
DISTANCE sum;
sum.metres = d1.metres + d2. metres;
sum.kms = d1.kms + d2.kms;
if(sum.metres >= 1000)
{
sum.metres = sum.metres%1000;
sum.kms += 1;
}
return sum;
}
DISTANCE subtract_distance(DISTANCE d1, DISTANCE d2)
{
DISTANCE sub;
if(d1.kms > d2.kms)
{
sub.metres = d1.metres - d2. metres;

sub.kms = d1.kms - d2.kms;


}
else
{
sub.metres = d2.metres - d1. metres;
sub.kms = d2.kms - d1.kms;
}
if(sub.metres < 0)
{
sub.kms = sub.kms - 1;
sub.metres = sub.metres + 1000;
}
return sub;
}
/* 9. Write a program, using pointer to structure, to initialize the members in the structure. Use
functions to print the students information.*/
#include <stdio.h>
#include <conio.h>
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
void display(struct student *);
main()
{
struct student *ptr_stud1;
struct student stud1 = {01, "Rahul", "BCA", 45000};
clrscr();
ptr_stud1 = &stud1;
display(ptr_stud1);
return (0);
}
void display(struct student *ptr_stud1)
{
printf("\n DETAILS OF STUDENT");
printf("\n ----------------------------");
printf("\n ROLL NO. = %d", ptr_stud1 -> r_no);
printf("\n NAME = ", puts(ptr_stud1 -> name));
printf("\n COURSE = ", puts(ptr_stud1 -> course));
printf("\n FEES = %f", ptr_stud1 -> fees);
}

/* 10. Write a program to read, display, add, and subtract two time defi ned using hour, minutes,
and values of seconds.*/
#include <stdio.h>
#include <conio.h>
typedef struct
{
int hr;
int min;
int sec;
}TIME;
TIME t1, t2, t3, t4;
TIME subtract_time(TIME, TIME);
TIME add_time(TIME, TIME);
int main()
{
int option;
clrscr();
do
{
printf("\n **** MAIN MENU *****");
printf("\n 1. Read time ");
printf("\n 2. Display time");
printf("\n 3. Add");
printf("\n 4. Subtract");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the first time in hrs, mins, and secs: ");
scanf("%d %d %d", &t1.hr, &t1.min, &t1.sec);
fflush(stdin);
printf("\n Enter the second time in hrs, mins, and secs: ");
scanf("%d %d %d", &t2.hr, &t2.min, &t2.sec);
fflush(stdin);
break;
case 2:
printf("\n The first time is: %d hr %d min %d sec", t1.hr, t1.min, t1.sec);
printf("\n The second time is: %d hr %d min %d sec", t2.hr, t2.min, t2.sec);
break;
case 3:
t3 = add_time(t1, t2);
printf("\n The sum of the two time values is: %dhr %dmin %dsec", t3.hr, t3.min, t3.sec);
break;
case 4:
t4 = subtract_time(t1, t2);
printf("\n The difference in time is: %d hr %d min %d sec", t4.hr, t4.min, t4.sec);
break;
}

}while(option != 5);
getch();
return 0;
}
TIME add_time(TIME t1, TIME t2)
{
TIME sum;
sum.sec = t1.sec + t2.sec;
while(sum.sec >= 60)
{
sum.sec -=60;
sum.min++;
}
sum.min = t1.min + t2.min;
while(sum.min >= 60)
{
sum.min -=60;
sum.hr++;
}
sum.hr = t1.hr + t2.hr;
return sum;
}
TIME subtract_time(TIME t1, TIME t2)
{
TIME sub;
if(t1.hr > t2.hr)
{
if(t1.sec < t2.sec)
{
t1.sec += 60;
t1.min--;
}
sub.sec = t1.sec - t2.sec;
if(t1.min < t2.min)
{
t1.min += 60;
t1.hr--;
}
sub.min = t1.min - t2.min;
sub.hr = t1.hr - t2.hr;
}
else
{
if(t2.sec < t1.sec)
{
t2.sec += 60;
t2.min--;
}
sub.sec = t2.sec - t1.sec;
if(t2.min < t1.min)
{

t2.min += 60;
t2.hr--;
}
sub.min = t2.min - t1.min;
sub.hr = t2.hr - t1.hr;
}
return sub;
}
/* 11. Write a program, using a pointer to a structure, to initialize the members in the structure.*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct student
{
int r_no;
char name[20];
char course[20];
int fees;
};
main()
{
struct student stud1, stud2, *ptr_stud1, *ptr_stud2;
clrscr();
ptr_stud1 = &stud1;
ptr_stud2 = &stud2;
ptr_stud1 -> r_no = 01;
strcpy(ptr_stud1 -> name, "Rahul");
strcpy(ptr_stud1 -> course, "BCA");
ptr_stud1 -> fees = 45000;
printf("\n Enter the details of the second student:");
printf("\n Enter the Roll Number =");
scanf("%d", &ptr_stud2 -> r_no);
fflush(stdin);
printf("\n Enter the Name = ");
gets(ptr_stud2 -> name);
fflush(stdin);
printf("\n Enter the Course = ");
gets(ptr_stud2 -> course);
fflush(stdin);
printf("\n Enter the Fees = ");
scanf("%d", &ptr_stud2 -> fees);
printf("\n DETAILS OF FIRST STUDENT");
printf("\n ----------------------------");
printf("\n ROLL NUMBER = %d", ptr_stud1 -> r_no);
printf("\n NAME = %s", ptr_stud1 -> name);
printf("\n COURSE = %s", ptr_stud1 -> course);
printf("\n FEES = %d", ptr_stud1 -> fees);
printf("\n\n\n\n DETAILS OF SECOND STUDENT");
printf("\n ----------------------------");
printf("\n ROLL NUMBER = %d", ptr_stud2 -> r_no);

printf("\n NAME = %s", ptr_stud2 -> name);


printf("\n COURSE = %s", ptr_stud2 -> course);
printf("\n FEES = %d", ptr_stud2 -> fees);
return 0;
}
/* 12. Write a program, using a pointer to a structure, to initialize the members in the structure
using an alternative technique.*/
#include <stdio.h>
#include <conio.h>
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
main()
{
struct student *ptr_stud1;
struct student stud1 = {01, "Rahul", "BCA", 45000};
clrscr();
ptr_stud1 = &stud1;
printf("\n DETAILS OF STUDENT");
printf("\n ----------------------------");
printf("\n ROLL NUMBER = %d", ptr_stud1 -> r_no);
printf("\n NAME = ", puts(ptr_stud1 -> name));
printf("\n COURSE = ", puts(ptr_stud1 -> course));
printf("\n FEES = %f", ptr_stud1 -> fees);
return 0;
}
/* 13. Write a program, using an array of pointers to a structure, to read and display the data of a
student.*/
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
typedef struct student
{
int r_no;
char name[20];
char course[20];
int fees;
};
struct student *ptr[10];
main()
{
int i;
struct ptr_std;
for(i=0;i<10;i++)
{

ptr[i] = (struct student *)malloc(sizeof(struct student));


printf("\n Enter the data for student %d ", i+1);
printf("\n \t ROLL NO.: ");
scanf("%d", ptr[i] -> r_no);
printf("\n \t NAME: ");
gets(ptr[i] -> name);
printf("\n \t COURSE: ");
gets(ptr[i] -> course);
printf("\n \t FEES: ");
scanf("%d", ptr[i] -> fees);
}
printf("\n DETAILS OF STUDENT");
printf("\n ----------------------------");
for(i=0;i<10;i++)
{
printf("\n ROLL NUMBER = %d", ptr_stud[i] -> r_no);
printf("\n NAME = ", puts(ptr_stud[i] -> name));
printf("\n COURSE = ", puts(ptr_stud[i] -> course));
printf("\n FEES = %d", ptr_stud[i] -> fees);
}
return 0;
}
/* 14. Write a program to read, display, add, and subtract two heights. Height should be given in
feet and inches.*/
#include <stdio.h>
#include <conio.h>
typedef struct
{
int ft;
int inch;
}HEIGHT;
HEIGHT h1, h2, h3;
HEIGHT add_height(HEIGHT *, HEIGHT *);
HEIGHT subtract_height(HEIGHT *, HEIGHT *);
main()
{
int option;
clrscr();
do
{
printf("\n *** MAIN MENU ***");
printf("\n 1. Read height ");
printf("\n 2. Display height");
printf("\n 3. Add");
printf("\n 4. Subtract");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{

case 1:
printf("\n Enter the first height in feet and inches: ");
scanf("%d %d", &h1.ft, &h1.inch);
printf("\n Enter the second height in feet and inches: ");
scanf("%d %d", &h2.ft, &h2.inch);
break;
case 2:
printf("\n The first height is: %d ft %d inch", h1.ft, h1.inch);
printf("\n The second height is: %d ft %d inch", h2.ft, h2.inch);
break;
case 3:
h3 = add_height(&h1, &h2);
printf("\n The sum of two heights is: %d ft %d inch", h3.ft, h3.inch);
break;
case 4:
h3 = subtract_height(&h1, &h2);
printf("\n The difference of two heights is: %d ft %d inch", h3.ft, h3.inch);
break;
}while(option != 5);
getch();
return 0;
}
HEIGHT add_height(HEIGHT *h1, HEIGHT *h2)
{
HEIGHT sum;
sum.inch = h1 -> inch + h2 -> inch;
while(sum.inch > 12)
{
sum.inch -= 12;
sum.ft++;
}
sum.ft = h1 -> ft + h2 -> ft;
return sum;
}
HEIGHT subtract_height(HEIGHT *h1, HEIGHT *h2)
{
HEIGHT sub;
if(h1 -> ft > h2 -> ft)
{
if(h1 -> inch < h2 -> inch)
{
h1 -> inch += 12;
h1 -> ft--;
}
sub.inch = h1 -> inch - h2 -> inch;
sub.ft = h1 -> ft - h2 -> ft;
}
else
{
if(h2 -> inch < h1 -> inch)
{

h2 -> inch += 12;


h2 -> ft--;
}
sub.inch = h2 -> inch - h1 -> inch;
sub.ft = h2 -> ft - h1 -> ft;
}
return sub;
}
/* 15. Write a program that passes a pointer to a structure to a function.*/
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
typedef struct student
{
int r_no;
char name[20];
char course[20];
int fees;
};
void display(struct student *);
main()
{
struct student *ptr;
ptr = (struct student *)malloc(sizeof (struct student));
printf("\n Enter the data for the student ");
printf("\n \t ROLL NO.: ");
scanf("%d", ptr -> r_no);
printf("\n \t NAME: ");
gets(ptr -> name);
printf("\n \t COURSE: ");
gets(ptr -> course);
printf("\n \t FEES: ");
scanf("%d", ptr -> fees);
display(ptr);
getch();
return 0;
}
void display(struct student *ptr)
{
printf("\n DETAILS OF STUDENT");
printf("\n ----------------------------");
printf("\n ROLL NUMBER = %d", ptr -> r_no);
printf("\n NAME = ", puts(ptr -> name));
printf("\n COURSE = ", puts(ptr -> course));
printf("\n FEES = %d", ptr -> fees);
}

/* 16. Write a program to illustrate the use of arrays within a structure.*/


#include <stdio.h>
#include <conio.h>
#include <alloc.h>
typedef struct student
{
char name[20];
int roll_no;
int marks[3];
};
void display(struct student* s)
{
int i;
printf("\n\n\n NAME: %s \n ROLL NO = %d\n", s -> name, s -> roll_no);
for(i = 0;i < 3;i++)
printf(" %d", s -> marks[i]);
}
main()
{
struct student *s[2];
int i,j;
clrscr();
for(i = 0;i < 2;i++)
{
s[i]=(struct student*) malloc(sizeof(struct student));
printf("\n\n Enter the name of student %d: ", i);
gets(s[i] -> name);
fflush(stdin);
printf("\n Enter the roll number of student %d: ", i);
scanf("%d", &s[i] -> roll_no);
printf("\n Enter the marks obtained in three subjects by student %d: ", i);
for(j = 0;j < 3;j++)
scanf("%d",&s[i] -> marks[j]);
fflush(stdin);
}
printf("\n \n\n ******DETAILS*****");
for(i=0;i<5;i++)
display(s[i]);
getch();
return 0;
}

/* 17. Write a program to display the name of the colors using an enumerated type.*/
#include <stdio.h>
enum COLORS {RED, BLUE, BLACK, GREEN, YELLOW, PURPLE, WHITE};
main()
{
enum COLORS c;
char *color_name[] = {"RED", "BLUE", "BLACK", "GREEN", "YELLOW", "PURPLE", "WHITE"};
for(c = RED; c <= WHITE; c++)
printf("\n %s", color_name[c]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
enum COLORS {red, blue, black, green, yellow,
purple, white};
main()
{
enum COLORS c;
c = rand()%7;
switch(c)
{
case red: printf("\n RED"); break;
case blue: printf("\n BLUE"); break;
case black: printf("\n BLACK"); break;
case green: printf("\n GREEN"); break;
case yellow: printf("\n YELLOW");
break;
case purple: printf("\n PURPLE");
break;
case white: printf("\n WHITE"); break;
}
return 0;
}

FILES
/* 1. Write a program to read a file character by character, and display it simultaneously on the
screen.*/
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int ch;
char filename[20];
printf("\n Enter the filename: ");
fp = fopen(filename, "r");
if(fp==NULL)
{
printf("\n Error Opening the File");
exit(1);
}
ch= fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
}
/* 2. Write a program to count the number of characters and number of lines in a file.*/
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int ch, no_of_characters = 0, no_of_lines = 1;
char filename[20];
printf("\n Enter the filename: ");
fp = fopen(filename, "r");
if(fp==NULL)
{
printf("\n Error Opening the File");
exit(1);
}
ch= fgetc(fp);
while(ch!=EOF)
{
if(ch=='\n')
no_of_lines++;
no_of_characters++;
ch = fgetc(fp);
}

if(no_of_characters > 0)
printf("\n In the file %s, there are %d lines and %d characters", filename, no_of_lines, no_of_characters);
else
printf("\n File is empty");
fclose(fp);
}
/*3. Write a program to print the text of a fi le on screen by printing the text line by line and
displaying the line numbers before the text in each line. Use command line argument to enter the fi
lename.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp1;
char text[100], ch;
int line = 1;
int i = 0;
clrscr();
if(argc != 2)
{
printf("\n Full information is not provided. Please provide a filename");
return 0;
}
fp1 = fopen(argv[1], "r");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
i = 0;
while(feof(fp1) == 0)
{
fscanf(fp1, "%c", &ch);
if (ch == '\n')
{
line++;
text[i] = '\0';
printf("%d %s", line, text);
i = 0;
}
text[i] = ch;
i++;
}
text[i] = '\0';
printf("%s", text);
fclose(fp1);
getch();
return 0;
}

/* 4. Write a program to compare two fi les to check whether they are identical or not.*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
FILE *fp1, *fp2;
int ch1, ch2;
char filename1[20], filename2[20];
clrscr();
printf("\n Enter the name of the first file: ");
gets(filename1);
fflush(stdin);
printf("\n Enter the name of the second file: ");
gets(filename2);
fflush(stdin);
if((fp1=fopen(filename1, "r"))==0)
{
printf("\n Error opening the first file");
exit(1);
}
if((fp2=fopen(filename2, "r"))==0)
{
printf("\n Error opening the second file");
exit(1);
}
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
while(ch1!=EOF && ch2!=EOF && ch1==ch2)
{
/* Reading and comparing the contents of two files */
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
}
if(ch1==ch2)
printf("\n Files are identical");
else
printf("\n Files are not identical");
fclose(fp1);
fclose(fp2);
getch();
return 0;
}

/* 5. Write a program to copy one fi le into another. Copy one character at a time.*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
FILE *fp1, *fp2;
int ch;
char filename1[20], filename2[20];
clrscr();
printf("\n Enter the name of the first file: ");
gets(filename1);
fflush(stdin);
printf("\n Enter the name of the second file: ");
gets(filename2);
fflush(stdin);
if((fp1=fopen(filename1, "r"))==0)
{
printf("\n Error opening the first file");
exit(1);
}
if((fp2=fopen(filename2, "w"))==0)
{
printf("\n Error opening the second file");
exit(1);
}
// Copy from fp1 to fp2
ch = fgetc(fp1);
while(ch!=EOF)
{
putc(ch, fp2);
ch = fgetc(fp1);
}
printf("\n FILE COPIED");
fclose(fp1);
fclose(fp2);
getch();
return 0;
}
/* 6. Write a program to copy one fi le into another. Copy multiple characters simultaneously.*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
FILE *fp1, *fp2;
char filename1[20], filename2[20], str[30];
clrscr();
printf("\n Enter the name of the first filename: ");

gets(filename1);
fflush(stdin);
printf("\n Enter the name of the second filename: ");
gets(filename2);
fflush(stdin);
if((fp1=fopen(filename1, "r"))==0)
{
printf("\n Error opening the first file");
exit(1);
}
if((fp2=fopen(filename2, "w"))==0)
{
printf("\n Error opening the second file");
exit(1);
}
while((fgets(str, sizeof(str), fp1))!=NULL) fputs(str, fp2);
fclose(fp1);
fclose(fp2);
getch();
return 0;
}
/* 7. Write a program to read a fi le that contains characters. Encrypt the data in this fi le while
writing it into another file. (For example while writing the data in another file you can use the
formula ch = ch 2, i.e., if the data contains character red the encrypted data becomes pcb*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char ch;
clrscr();
if(argc != 3)
{
printf("\n Full information is not provided");
return 0;
}
fp1 = fopen(argv[1], "r");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
fp2 = fopen(argv[2], "w");
if(fp2 == NULL)
{
printf("\n File Opening Error");
return 0;
}
while (feof(fp1) == 0)

{
fscanf(fp1, "%c", &ch);
fprintf(fp2, "%c", ch - 2);
/*encrypted character is printed on the screen */
}
printf("\n The encrypted data is written to the file");
fclose(fp2);
fcloseall();
getch();
return 0;
}
/* 8. Write a program to read a fi le that contains lower case characters. Then write these
characters into another file with all lower case characters converted into upper case. (For example
if the fi le contains data red it must be written in another fi le as RED).*/
#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char ch;
clrscr();
if(argc != 3)
{
printf("\n Full information is not provided");
return 0;
}
fp1 = fopen(argv[1], "r");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
fp2 = fopen(argv[2], "w");
if(fp2 == NULL)
{
printf("\n File Opening Error");
return 0;
}
while (feof(fp1) == 0)
{
fscanf(fp1, "%c", &ch);
fprintf(fp2, "%c", ch - 32);
}
fcloseall();
printf("\n File copied with upper case characters");
getch();
return 0;
}

/* 9. Write a program to merge two files into a third file. The names of the files must be entered
using command line arguments.*/
#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
FILE *fp1, *fp2, *fp3;
char ch;
clrscr();
if(argc != 4) // Read three filenames from the user
{
printf("\n Full information is not provided");
return 0;
}
fp1 = fopen(argv[1], "r");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
fp2 = fopen(argv[2], "r");
if(fp2 == NULL)
{
printf("\n File Opening Error");
return 0;
}
fp3 = fopen(argv[3], "w");
if(fp3 == NULL)
{
printf("\n File Opening Error");
return 0;
}
while (feof(fp1) != 0)
{
fscanf(fp1, "%c", &ch);
fprintf(fp3, "%c", ch);
}
while (feof(fp2) != 0)
{
fscanf(fp2, "%c", &ch);
fprintf(fp3, "%c", ch);
}
fcloseall();
printf("\n File merged");
getch();
return 0;
}

/* 10. Write a program to read some text from the keyboard and store it in a file.*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
FILE *fp;
char filename[20], str[100];
printf("\n Enter the filename: ");
fp = fopen(filename, "w");
if(fp==NULL)
{
printf("\n Error Opening The File");
exit(1);
}
printf("\n Enter the text: ");
gets(str);
fflush(stdin);
fprintf(fp, "%s", str);
fclose(fp);
}
/* 11. Write a program to read the details of a student and then print it on the screen as well as
write it into a file.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name[80];
float fees;
char DOB[80];
}
STUDENT;
STUDENT stud1;
clrscr();
fp = fopen("student_details.dat", "w");
if(fp == NULL)
{
printf("\n File Opening Error");
return 0;
}
printf("\n Enter the roll number: ");
scanf("%d", &stud1.roll_no);
printf("\n Enter the name: ");
scanf("%s", &stud1.name);
printf("\n Enter the fees: ");

scanf("%f", &stud1.fees);
printf("\n Enter the DOB: ");
scanf("%s", &stud1.DOB);
// PRINT ON SCREEN
printf("\n *** STUDENTS DETAILS ***");
printf("\n ROLL No. = %d", stud1.roll_no);
printf("\n NAME = %s", stud1.name);
printf("\n FEES = %f", stud1.fees);
printf("\n DOB = %s", stud1.DOB);
// WRITE TO FILE
fprintf(fp,"%d %s %f %s", stud1.roll_no, stud1.name, stud1.fees, stud1.DOB);
fclose(fp);
getch();
return 0;
}
/* 12. Write a program to read the details of a student from a file and then print it on the screen.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name[80];
float fees;
char DOB[80];
}STUDENT;
STUDENT stud1;
clrscr();
fp = fopen("student_details.dat", "r");
if(fp == NULL)
{
printf("\n File Opening Error");
return 0;
}
// READ FROM FILE
fscanf(fp,"%d %s %f %s", &stud1.roll_no, stud1.name, &stud1.fees, stud1.DOB);
// PRINT ON SCREEN
printf("\n *** STUDENTS DETAILS ***");
printf("\n ROLL No. = %d", stud1.roll_no);
printf("\n NAME = %s", stud1.name);
printf("\n FEES = %f", stud1.fees);
printf("\n DOB = %s", stud1.DOB);
fclose(fp);
getch();
return 0;
}

/* 13. Write a program to read the details of student until a 1 is entered and simultaneously write
the data to a file.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name[80];
float fees;
char DOB[80];
}STUDENT;
STUDENT stud1;
clrscr();
fp = fopen("student_details.dat", "w");
if(fp == NULL)
{
printf("\n File Opening Error");
return 0;
}
printf("\n Enter the roll number: ");
scanf("%d", &stud1.roll_no);
while(stud1.roll_no != -1)
{
printf("\n Enter the name: ");
scanf("%s", stud1.name);
printf("\n Enter the fees: ");
scanf("%f", &stud1.fees);
printf("\n Enter the DOB: ");
scanf("%s", stud1.DOB);
fprintf(fp,"%d %s %f %s", stud1.roll_no, stud1.name, stud1.fees, stud1.
DOB);
fflush(stdin);
printf("\n Enter the roll number: ");
scanf("%d", &stud1.roll_no);
}
fclose(fp);
getch();
return 0;
}
/* 14. Write a program to read characters until a * is entered. Simultaneously store these
characters in a file.*/
#include <stdio.h>
#include <conio.h>
int main()
{
file *fp;

char ch;
clrscr();
fp = fopen("characters.dat", "w");
if(fp == NULL)
{
printf("\n File Opening Error");
return 0;
}
printf("\n Enter the characters: ");
scanf("%c", &ch);
while(ch != '*')
{
fprintf(fp, "%c", ch);
scanf("%c", &ch);
}
printf("\n Written to the file");
fclose(fp);
getch();
return 0;
}
/* 15. Write a program to count the number of lower case, upper case, numbers, and special
characters present in the contents of a file. (Assume that the file contains the following data: 1.
Hello, How are you?)*/
#include <stdio.h>
#include <conio.h>
int main(int arg c, char *argv[])
{
FILE *fp;
int ch, upper_case = 0, lower_case = 0, numbers = 0, special_chars = 0;
clrscr();
if(argc != 2)
{
printf("\n Full information is not provided");
return 0;
}
fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("\n File Opening Error");
return 0;
}
i = 0;
while(feof(fp) == 0)
{
fscanf(fp, "%c", &ch);
if (ch >= 'A' && ch <= 'Z')
upper_case++;
if (ch >= 'a' && ch <= 'z')
lower_case++;
if (ch >= '0' && ch <= '9')

numbers++;
else
special_chars++;
}
fclose(fp);
printf("\n Number of upper case characters = %d", upper_case);
printf("\n Number of lower case characters = %d", lower_case);
printf("\n Number of digits = %d", numbers);
printf("\n Number of special characters = %d", special_chars);
getch();
return 0;
}
/* 16. Write a program to write record of students to a file using array of structures.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name[80];
int marks;
}STUDENT;
STUDENT stud1[5];
int i;
clrscr();
fp = fopen("student_details.txt", "w");
if(fp == NULL)
{
printf("\n File Opening Error");
return 0;
}
for(i = 0; i < 5;i++)
{
printf("\n Enter the roll number: ");
scanf("%d", &stud1[i].roll_no);
printf("\n Enter the name: ");
scanf("%s", stud1[i].name);
printf("\n Enter the marks: ");
scanf("%d", &stud1[i].marks);
}
// PRINT ON SCREEN
for(i = 0;i < 5;i++)
{
printf("\n *** STUDENTS DETAILS ***");
printf("\n ROLL No. = %d", stud1[i].roll_no);
printf("\n NAME = %s", stud1[i].name);
printf("\n MARKS = %d", stud1[i].marks);
// WRITE TO FILE

fprintf(fp,"%d %s %d", stud1[i].roll_no, stud1[i].name, stud1[i].marks);


}
printf("\n Data Written to the file");
fclose(fp);
getch();
return 0;
}
/* 17. Write a program to append a record to the students file.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name[80];
int marks;
}STUDENT;
STUDENT stud1;
clrscr();
fp = fopen("student_details.txt", "a");
if(fp == NULL)
{
printf("\n File Opening Error");
return 0;
}
printf("\n Enter the Roll Number = ");
scanf("%d", &stud1.roll_no);
printf("\n Enter the Name = ");
scanf("%s", &stud1.name);
printf("\n Enter the Marks = ");
scanf("%d", &stud1.marks);
fprintf("fp", "\n %d %s %d", stud1.roll_no, stud1.name, stud1.marks);
/* After entering the record add a -1 to the file to denote the end of records */
fprintf("fp, %d", -1);
printf("\n Data Appended");
fclose(fp);
getch();
return 0;
}

/* 18. Write a program to read the record of a particular student.*/


#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp1;
typedef struct student
{
int roll_no;
char name[80];
int marks;
}STUDENT;
STUDENT stud1;
int found =0, rno;
clrscr();
fp1 = fopen("student_details.txt", "r");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
printf("\n Enter the roll number of the student whose record has to be read: ");
scanf("%d", &rno);
while(1)
{
fscanf(fp1, "%d %s %d", &stud1.roll_no, stud1.name, &stud1.marks);
if(stud1.roll_no == -1)
break;
if(stud1.roll_no == rno)
{
found = 1;
printf("\n The details of student are");
printf(" %d %s %d", stud1.roll_no, stud1.name, stud1.marks);
break;
}
}
if (found==0)
printf("\n Record not found in the file");
fclose(fp1);
return 0;
}
/* 19. Write a program to edit the record of a particular student.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp1, *fp2;
typedef struct student
{

int roll_no;
char name[80];
int marks;
}STUDENT;
STUDENT stud1;
int found =0, rno;
clrscr();
fp1 = fopen("student_details.txt", "r");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
fp2 = fopen("temp.txt", "w");
if(fp2 == NULL)
{
printf("\n File Opening Error");
return 0;
}
printf("\n Enter the roll number of the student whose record has to be modified: ");
scanf("%d", &rno);
while(1)
{
fscanf(fp1, "%d", &stud1.roll_no);
if(stud1.roll_no == -1)
break;
if(stud1.roll_no == rno)
{
found = 1;
fscanf(fp1, "%s %d", stud1.name, &stud1.marks);
printf("\n The details of existing record are ");
printf(" %d %s %d", stud1.roll_no, stud1.name, stud1.marks);
printf("\n Enter the modified name of the student: ");
scanf("%s", stud1.name);
printf("\n Enter the modified marks of the student: ");
scanf("%d", &stud1.marks);
/* Write the modified record to the temporary file */
fprintf(fp2, "%d %s %d", stud1.roll_no, stud1.name, stud1.marks);
}
else
{
/* Copy the non-matching records to the temporary file */
fscanf(fp1, "%s %d", stud1.name, &stud1.marks);
fprintf(fp2, "%d %s %d", stud1.roll_no, stud1.name, stud1.marks);
}
}
fprintf(fp2,"%d", -1);
fclose(fp1);
fclose(fp2);
if(found==0)
printf("\n The record with roll number &d was not found in the file", rno);

else
{
fp1 = fopen("student_details.txt", "w");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
fp2 = fopen("temp.txt", "r");
if(fp2 == NULL)
{
printf("\n File Opening Error");
return 0;
}
/* Copy the contents of temporary file into actual file */
while(1)
{
fscanf(fp2,"%d",&stud1.roll_no);
if(stud1.roll_no==-1)
break;
fscanf(fp2, "%s %d",stud1.name, &stud1.marks);
fprintf(fp1, "%d %s %d", stud1.roll_no, stud1.name, stud1.marks);
}
}
fclose(fp1);
fclose(fp2);
printf("\n Record Updated");
getch();
return 0;
}
/* 20. Write a program to delete the record of a particular student.*/
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp1, *fp2;
typedef struct student
{
int roll_no;
char name[80];
int marks;
}STUDENT;
STUDENT stud1;
int found =0, rno;
clrscr();
fp1 = fopen("student_details.txt", "r");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;

}
fp2 = fopen("temp.txt", "w");
if(fp2 == NULL)
{
printf("\n File Opening Error");
return 0;
}
printf("\n Enter the roll number of the student whose record has to be deleted: ");
scanf("%d", &rno);
while(1)
{
fscanf(fp1, "%d", &stud1.roll_no);
if(stud1.roll_no == -1)
break;
if(stud1.roll_no == rno)
{
found = 1;
fscanf(fp1, "%s %d", stud1.name, &stud1.marks);
}
// The matching record is not copied to temp file
else
{
// Copy the non-matching records to the temporary file
fscanf(fp1, "%s %d", stud1.name, &stud1.marks);
fprintf(fp2, "%d %s %d ", stud1.roll_no, stud1.name, stud1.marks);
}
}
fprintf(fp2," %d", -1);
fclose(fp1);
fclose(fp2);
if(found==0)
printf("\n The record with roll number &d was not found in the file", rno);
else
{
fp1 = fopen("student_details.txt", "w");
if(fp1 == NULL)
{
printf("\n File Opening Error");
return 0;
}
fp2 = fopen("temp.txt", "r");
if(fp2 == NULL)
{
printf("\n File Opening Error");
return 0;
}
// Copy the contents of temporary file into actual file
while(1)
{
fscanf(fp2,"%d",&stud1.roll_no);
if(stud1.roll_no==-1)

break;
fscanf(fp2, "%s %d",stud1.name, &stud1.marks);
fprintf(fp1, "%d %s %d ", stud1.roll_no, stud1.name, stud1.marks);
}
}
fprintf(fp1, "%d ", -1);
fclose(fp1);
fclose(fp2);
printf("\n Record Deleted");
/* The programmer may delete the temp file which will no longer be required */
getch();
return 0;
}
/* 21. Write a program to store records of an employee in employee fi le. The data must be stored
using binary file.*/
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e[5];
int i;
fp = fopen("employee.txt", "wb");
if(fp==NULL)
{
printf("\n Error opening file");
exit(1);
}
printf("\n Enter the details ");
for(i = 0; i < 5; i++)
{
printf("\n\n Enter the employee code:");
scanf("%d", &e[i].emp_code);
printf("\n\n Enter the name of the employee: ");
scanf("%s", e[i].name);
printf("\n\n Enter the HRA, DA, and TA: ");
scanf("%d %d %d", &e[i].hra, &e[i].da, &e[i].ta);
fwrite(&e[i], sizeof(e[i]), 1, fp);
}
fclose(fp);
getch();
return 0;

}
/* 22. Write a program to read the records stored in employee.txt fi le in binary mode.*/
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e;
int i;
clrscr();
fp = fopen("employee.txt", "rb");
if(fp==NULL)
{
printf("\n Error opening file");
exit(1);
}
printf("\n THE DETAILS OF THE EMPLOYEES ARE ");
while(1)
{
fread(&e, sizeof(e), 1, fp);
if(feof(fp))
break;
printf("\n\n Employee Code: %d", e.emp_code);
printf("\n\n Name: %s", e.name);
printf("\n\n HRA, DA, and TA: %d %d %d", e.hra, e.da, e.ta);
}
fclose(fp);
getch();
return 0;
}
/* 23. Write a program to append a record in the employee file (binary file).*/
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;

int ta;
};
FILE *fp;
struct employee e;
int i;
fp = fopen("employee.txt", "ab");
if(fp==NULL)
{
printf("\n Error opening file");
exit(1);
}
printf("\n\n Enter the employee code:");
scanf("%d", &e.emp_code);
printf("\n\n Enter the name of employee: ");
scanf("%s", &e.name);
printf("\n\n Enter the HRA, DA, and TA:");
scanf("%d %d %d", &e.hra, &e.da, &e.ta);
fwrite(&e, sizeof(e), 1, fp);
fclose(fp);
printf("\n Record Appended");
getch();
return 0;
}
/* 24. Write a program to edit the employee record stored in a binary file.*/
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp1, *fp2;
struct employee e;
int i, ec, found = 0;
clrscr();
fp1 = fopen("employee.txt", "rb");
if(fp1==NULL)
{
printf("\n Error opening file");
exit(1);
}
fp2 = fopen("temp_emp.txt", "wb");
if(fp2==NULL)
{
printf("\n Error opening file");

exit(1);
}
printf("\n Enter the code of the employee whose information has to be edited: ");
scanf("%d",&ec);
while(1)
{
fread(&e,sizeof(e),1,fp1);
if(feof(fp1))
break;
if(e.emp_code==ec)
{
found=1;
printf("\n The existing record is: %d %s %d %d %d", e.emp_code, e.name, e.hra, e.ta, e.da);
printf("\n Enter the modified name: ");
scanf("%s", e.name);
printf("\n Enter the modified HRA, TA, and DA: ");
scanf("%d %d %d", &e.hra, &e.ta, &e.da);
fwrite(&e, sizeof(e),1,fp2);
}
else
fwrite(&e, sizeof(e),1,fp2);
}
fclose(fp1);
fclose(fp2);
if(found==0)
printf("\n Record not found");
else
{
fp1 = fopen("employee.txt", "wb");
if(fp1==NULL)
{
printf("\n Error opening file");
exit(1);
}
fp2 = fopen("temp_emp.txt", "rb");
if(fp2==NULL)
{
printf("\n Error opening file");
exit(1);
}
while(1)
{
fread(&e, sizeof(e),1,fp2);
if(feof(fp2))
break;
fwrite(&e, sizeof(e), 1, fp1);
}
}
fclose(fp1);
fclose(fp2);
printf("\n Record Edited");

getch();
return 0;
}
/* 25. Write a program to randomly read the nth records of a binary file.*/
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e;
int result, rec_no;
fp = fopen("employee.txt", "rb");
if(fp==NULL)
{
printf("\n Error opening file");
exit(1);
}
printf("\n\n Enter the rec_no you want to read: ");
scanf("%d", &rec_no);
if(rec_no >= 0)
{
/* from the file pointed by fp read a record of the specified record starting from the beginning of the file*/
fseek(fp, (rec_no-1)*sizeof(e), SEEK_SET);
result = fread(&e, sizeof(e), 1, fp);
if(result == 1)
{
printf("\n EMPLOYEE CODE: %d", e.emp_code);
printf("\n Name: %s", e.name);
printf("\n HRA, TA and DA: %d %d %d", e.hra, e.ta, e.da);
}
else
printf("\n Record Not Found");
}
fclose(fp);
getch();
return 0;
}

/* 26. Write a program to print the records in reverse order. The file must be opened in binary
mode. Use fseek ().*/
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e;
int result, i;
fp = fopen("employee.txt", "rb");
if(fp==NULL)
{
printf("\n Error opening file");
exit(1);
}
for(i=5;i>=0;i--)
{
fseek(fp, i*sizeof(e), SEEK_SET);
fread(&e, sizeof(e), 1, fp);
printf("\n EMPLOYEE CODE: %d", e.emp_code);
printf("\n Name: %s", e.name);
printf("\n HRA, TA, and DA: %d %d %d", e.hra, e.ta, e.da);
}
fclose(fp);
getch();
return 0;
}
/* 27. Write a program to edit a record in binary mode using fseek ().*/
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp;

struct employee e;
int rec_no;
fp = fopen("employee.txt", "r+");
if(fp==NULL)
{
printf("\n Error opening file");
exit(1);
}
printf("\n Enter record no. to be modified: ");
scanf("%d", &rec_no);
fseek(fp, (rec_no-1)*sizeof(e), SEEK_SET);
fread(&e, sizeof(e), 1, fp);
printf("\n Enter modified name of the employee: ");
scanf("%s", e.name);
printf("\n Enter the modified HRA, TA, and DA of the employee: ");
scanf("%d %d %d", &e.hra, &e.ta, &e.da);
fwrite(&e, sizeof(e), 1, fp);
fclose(fp);
printf("\n Record Edited");
getch();
return 0;
}

You might also like