Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 43

COMPUTER SCIENCE & ENGINEERING

I YEAR
PRACTICAL- II
C - PROGRAMMING (SYLLABUS)
The List of sample Programs are given below :
{ Practice some more related programs on each unit}

1. Performing Addition, Subtraction, Multiplication, Division and Modulus operation on two integers.
2. Reading integers, characters and strings from the keyboard and displaying them.
3. Reading the ASCII code of a character and vice versa.
4. Finding the Area and Circumference of a Circle.
5. Calculating simple and compound interest.
6. Converting temperature in Celsius to Fahrenheit, Miles to Kms and Kgs to Pounds.
7. Finding the Average of the marks of a student.
8. Finding if a number is even or odd.
9. Finding if a student’s result is “pass” or “fail” based on marks.
10. Finding the weekly wages of a worker taking overtime work into consideration.
11. Finding the grade obtained by a student based on the total marks obtained.
12. Printing numbers from 1 to n, where n is read from the keyboard.
13. Generating multiplication Table of given number n.
14. Perform Lowercase to Uppercase Character Conversion and vice versa.
15. Calculation of Factorial to given number
16. Largest of three given numbers
17. Creating a single dimensional array of numbers and displaying the contents.
18. Picking the largest number from a single dimensional array of numbers.
19. Arranging a single dimensional array of numbers into ascending / descending order.
20. Finding the length of a given character array.
21. Displaying the Reverse of a given array.
22. Adding two single dimensional arrays.
23. Adding 2 two dimensional matrices.
24. Checking whether a given number is a palindrome or not.
25. Using string functions like stringcat(), strlen(), strcpy() etc.
26. Writing and calling a function to print 25 ‘*’ in a line. &EERING SYLLABUS
27. Write and call a function to print n number of ‘*’ in a line, where n is the parameter passed to
the function.
28. Writing and calling functions to add, subtract and multiply two numbers.
29. Use trigonometric functions to display Sin and Cos value of degrees from 0 to 180 degrees in
Steps of 30 degrees.
30. Use the sqrt() function to find the real roots of a quadratic equation.
31. Create a structure by name “book” containing book no., book name, author and cost as members.
Create book1 and book2 as copies of this structure and display the values for two books.
Display the total cost of the books.
32. Create a structure by name “employee” with necessary data members and create an array of 5
employees and display the values.
33. Use file operation functions to read write append data to and from files.
34. Writing a program to create a simple text file and write and read data from it using file operation
functions like fopen() etc.
35. Writing a program to write integers to a file, read them and print them into two file depending on
whether they are even or odd.
36. Write a C program to print Fibonacci Series
37. Write a C program to find whether a given number is prime or not.
38. Write a C Program to find first n prime numbers.
39. Write a c program for matrix multiplication
1.Write a C program for addition, subtraction, multiplication, division
and modulus operator on two integers.

#include<stdio.h>
void main()
{ int a,b;
printf("\n enter two values: ");
scanf("%d %d",&a,&b);
printf("\n a + b = %d", a+b);
printf("\n a - b = %d", a-b);
printf("\n a * b = %d", a*b);
printf("\n a / b = %d", a/b);
printf("\n a modulus b = %d", a%b);
getch();
}

input:

enter two values: 13 5

output:

a + b = 18
a-b=8
a * b = 65
a/b=2
a modulus b = 3

2. Program for reading integers, characters and strings from the keyboard and displaying them.

#include<stdio.h>
void main()
{int i;
char c , a[20];

printf("\n enter a character");


scanf("%c",&c);
printf("\n enter an integer");
scanf("%d",&i);
printf("\n enter a string");
scanf("%s",a);
printf("\n given character = %c",c);
printf("\n given integer = %d",i);
printf("\n given string = %s",a);
getch();
}

Input
enter a character A
enter an integer 24
enter a string suresh

output

given character = A
given integer = 24
given string = suresh

3. program for reading the ASCII code of a character and vice versa.

#include<stdio.h>
#include<conio.h>
void main()
{ char x;
int n;

/*converts a character to ASCII code */


clrscr();
printf("\n enter a character");

scanf("%c",&x);
printf ("\n given character is %c", x);
printf ("\n ASCII code is %d\n " , x);
/* converts ASCII code into a character */
printf("\n enter a ASCII code");
scanf("%d",&n);
printf ("\n ASCII code is %d", n);
printf ("\n equivalent character is %c", n);

getch();

}
Input
enter a character A
output
given character is A
ASCII code is 65
Input
enter a ASCII code 66
output
ASCII code is 66
equivalent character is B

4. Write a C program to find area and circumference of


a circle.

Area of a circle = ∏r2

Circumference of a circle = 2∏r

/* finds area and circumference of a circle */


#include<stdio.h>
void main()
{ float rad,area,circum;
printf("\n enter radius: ");
scanf("%f",&rad);
printf("\n radius = %f",rad);
area = 22.0/7.0 * rad * rad;
circum = 2.0 * 22.0/7.0 * rad;
printf("\n area of a circle = %f", area);
printf("\n circumference of a circle = %f", circum);
getch();
}

input
enter radius: 7
radius = 7.000000

output

area of a circle = 154.000000


circumference of a circle = 44.000000

5. Write a C Program to find Simple interest and compound interest.

Simple interest = PNR/100


Compound interest = P(1+r/100)n – P
/*finds simple interest and compound interest*/
#include<stdio.h>
#include<math.h>
void main()
{ float si,ci,p,n,r;
printf("\n enter principal:");
scanf("%f",&p);
printf("\n enter no of years:");
scanf("%f",&n);
printf("\n enter rate of interest:");
scanf("%f",&r);
si= p*n*r/100.0;
ci = p*pow((1.0 + r/100.0), (int) n)-p;
printf("\n simple interest = %f",si);
printf("\n compound interest = %f", ci);
getch();
}
input:
enter principal: 1000
enter no of years: 2
enter rate of interest: 10
output:
simple interest = 200.000000
compound interest = 210.000000

6. Write a C Program to convert temperature from centigrade to Fahrenheit, miles to


kilometers and Kilograms to pounds.

/* Converting temperature in Celsius to Fahrenheit, Miles to Kms and Kgs to Pounds.

1 Fahrenheit degree = 32.0 + 9 C / 5 = 33.8 celcius(centigrade) degrees


1 Km = 1.60934 Miles
1 Kg = 2.20462 pounds */

#include<stdio.h>
void main()
{ float centi,fahren,miles,km,pounds,kgs; char x;

printf("\n which conversion you want?(T)emperature/(D)istance/(W)eight:");


x = toupper(getchar());
switch (x){
case 'T' :
{ /*converts from centigrade to fahrenheit*/
printf("\enter centigrade reading");
scanf("%f",&centi);
fahren = 32.0 + 9.0 * centi/5.0;
printf("\n %f Centigrade degrees equals %f fahrenheit degrees",centi,fahren);
break;
}

case 'D' :
{ /*converts from miles to kilometers*/
printf("\enter miles reading");
scanf("%f",&miles);
km = 1.60934 * miles;
printf("\n %f miles equals %f kilometers",miles,km);
break;
}
case 'W' :
{ /*converts from kilograms to pounds */
printf("\enter kilograms reading");
scanf("%f",&kgs);
pounds =2.20462 * kgs ;
printf("\n %f kilograms equals %f pounds",kgs,pounds);
}
}
getch();
}
output

which conversion you want?(T)emperature/(D)istance/(W)eight: t

enter centigrade reading -40


-40.000000 Centigrade degrees equals -40.000000 fahrenheit degrees

which conversion you want?(T)emperature/(D)istance/(W)eight: d

enter miles reading 2


2.000000 miles equals 3.218680 kilometers
which conversion you want?(T)emperature/(D)istance/(W)eight: w

enter kilograms reading 3

3.000000 kilograms equals 6.613860 pounds

7. Write a C program to find average of marks of five subjects.

Total marks = marks of (GFC + ENG + CFMSO + CProg + Acct)

Average marks = total marks/5

/*finds average marks of a student */


#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
char stname[10];
int gfcm,engm,cfmsom,cprogm,acctm,total;
float avg;
clrscr();
printf("\n enter student name: ");
scanf("%s", stname);
printf("\n enter gfc marks(0-50): ");
scanf("%d", &gfcm);
printf("\n enter english marks(0-50): ");
scanf("%d" ,&engm);
printf("\n enter cfmso marks(0-50): ");
scanf("%d", &cfmsom);
printf("\n enter c programming marks(0-50): ");
scanf("%d", &cprogm);
printf("\n enter accountancy & tally marks(0-50): ");
scanf("%d", &acctm);
printf(“\nstudent name = %s\n”,stname);
total = gfcm+engm+cfmsom+cprogm+acctm;
printf("\n total marks = %d",total);
avg = (float)(total) / 5.0;
printf("\n\n average marks of %s is %5.2f" ,stname , avg);
getch();
}
input
enter student name: surya
enter gfc marks(0-50): 38
enter english marks(0-50): 32
enter cfmso marks(0-50): 28
enter c programming marks(0-50): 42
enter accountancy & tally marks(0-50): 35

output
student name = surya

total marks = 175.00

average marks of surya is 35.00

8. Write a c program to find a number either odd or even.

/* finds odd or even number by using modulus operator */


#include<stdio.h>
void main()
{
int n;

printf("\n Enter an integer: ");


scanf("%d",&n);
if ( n%2 == 0 )
printf("\n %d is an Even”,n);
else
printf("\n %d is an Odd",n);

getch();
}

/*C program to check odd or even using bitwise operator*/

#include<stdio.h>
void main()
{
int n;

printf("\nEnter an integer: ");


scanf("%d",&n);

if ( n & 1 == 1 )
printf("\n %d is an Odd ”, n);
else
printf("\n %d is an Even ”, n);

getch();
}

/*C program to check odd or even without using bitwise or modulus operator */
#include<stdio.h>

main()
{
int n;

printf("\nEnter an integer:");
scanf("%d",&n);
if ( (n/2)*2 == n )
printf("\n %d is an Even number\n",n);
else
printf("\n %d is an Odd number \n",n);

getch();
}

/*Finds odd or even using conditional operator*/


#include<stdio.h>

main()
{
int n;

printf("\nEnter an integer:");
scanf("%d",&n);

n%2 == 0 ? printf("\n%d is an Even number\n",n) : printf("\n%d is an Odd number\n",n);

getch();
}

input output

Enter an integer: 5 5 is an Odd number


Enter an integer: 8 8 is an Even number

9. Write a C program to find the result whether the student passed or failed based on
marks.

Suppose there are five subjects in a class(GFC, English, CFMSO,


C-Programming, Accountancy&Tally) and a student must get minimum 18 marks in all subjects to pass
otherwise student is failed.

/* finds student result whether pass or fail based on marks */


#include<stdio.h>
#include<conio.h>
void main()
{
char stname[10];
int gfcm, engm, cfmsom, cprogm, acctm, total;
float avg;
clrscr();
printf("\n enter student name: ");
scanf("%s", stname);
printf("\n enter gfc marks(0-50):");
scanf("%d", &gfcm);
printf("\n enter english marks(0-50):");
scanf("%d" ,&engm);
printf("\n enter cfmso marks(0-50):");
scanf("%d", &cfmsom);
printf("\n enter c programming marks(0-50):");
scanf("%d", &cprogm);
printf("\n enter accountancy & tally marks(0-50):");
scanf("%d", &acctm);
printf("\n student name is %s " ,stname);
total = gfcm+engm+cfmsom+cprogm+acctm;
printf("\n total marks = %d",total);
if(gfcm>=18 && engm >=18 && cfmsom >=18 && cprogm >=18 &&
acctm >= 18) printf("\n Result is passed");
else printf("\n Result is failed");
getch();
}

input:

enter student name: surya


enter gfc marks(0-50): 35
enter english marks(0-50): 30
enter cfmso marks(0-50): 25
enter c programming marks(0-50):40
enter accountancy & tally marks(0-50):30

output:
student name is surya
total marks = 160
Result is passed

enter student name chandra


enter gfc marks(0-50): 26
enter english marks(0-50): 24
enter cfmso marks(0-50):15
enter c programming marks(0-50):35
enter accountancy & tally marks(0-50):32
student name is chandra
total marks = 132
Result is failed

10. Finding the weekly wages of a worker taking overtime work into consideration.

Note: Wage for overtime duty is twice to the daily wage.

/* finding weekly wages of a worker taking


overtime duty into consideration*/
#include<stdio.h>
#include<conio.h>
void main()
{ int wage,days,ods,total;
clrscr();
printf("\n How much the daily wage?: ");
scanf("%d",&wage);
printf("\n How many days worked in the week(1-7): ");
scanf("%d",&days);
printf("\n How many overtime duties: ");
scanf("%d",&ods);
total = wage * days + 2 * wage * ods;
printf("\n Total payment in the week = %d Rupees",total);
getch();
}

Input

How much the daily wage?: 200


How many days worked in the week(1-7): 5
How many overtime duties: 2

Output
Total payment in the week = 1800 Rupees

11. Finding the grade obtained by a student based on the total marks obtained.

Student is passed if he/she gets minimum marks in all subjects as follows


Telugu >=35, Hindi >=20,English >=35, Maths >=35,Science >=35 and Social >=35.
Otherwise student is failed.
If a student is passed , then we can derive grade as follows.
If total marks >=360 then grade is first class
else
If total marks >=300 then the grade is Second class.
else the grade is third class.

/*finding grades*/
#include<stdio.h>
#include<conio.h>
void main()

{ int sno,em,tm,hm,mm,scim,socm,totmar;

clrscr();
printf("\n enter student number: ");
scanf("%d",&sno);
printf("\n enter English marks: ");
scanf("%d",&em);
printf("\n enter Telugu marks: ");
scanf("%d",&tm);
printf("\n enter Hindi marks: ");
scanf("%d",&hm);
printf("\n enter Maths marks: ");
scanf("%d",&mm);
printf("\n enter Science marks: ");
scanf("%d",&scim);
printf("\n enter Social marks: ");
scanf("%d",&socm);
totmar= em+tm+hm+mm+scim+socm;
printf("\n student number : %d ",sno);
printf("\n English marks : %d ",em);
printf("\n Telugu marks : %d ",tm);
printf("\n Hindi marks : %d ",hm);
printf("\n maths marks : %d ",mm);
printf("\n science marks : %d ",scim);
printf("\n Social marks : %d ",socm);
printf("\n total marks : %d ",totmar);

if ((em<35)||(tm<35) || (hm<20) || (mm<35) || (scim <35) || (socm <35)) printf("\n Fail");


else
if (totmar >=360) printf("\n Grade = First class");
else if ((totmar >= 300) && (totmar <360)) printf("\n Grade = Second class");
else printf("\n grade = Third class");

getch();
}

Input

enter student number: 111


enter English marks:54
enter Telugu marks: 56
enter Hindi marks: 58
enter Maths marks: 75
enter Science marks:46
enter Social marks: 68
Output
student number : 111
English marks : 54
Telugu marks : 56
Hindi marks : 58
maths marks : 75
science marks : 46
Social marks : 68
total marks : 357
Grade = Second class

Input
enter student number: 222
enter English marks: 37
enter Telugu marks: 22
enter Hindi marks: 67
enter Maths marks: 54
enter Science marks: 46
enter Social marks: 38
Output
student number : 222
English marks : 37
Telugu marks : 22
Hindi marks : 67
maths marks : 54
science marks : 46
Social marks : 38
total marks : 264
Fail
Input
enter student number: 333
enter English marks: 78
enter Telugu marks: 89
enter Hindi marks: 97
enter Maths marks: 76
enter Science marks: 59
enter Social marks: 87
Output
student number : 333
English marks : 78
Telugu marks : 89
Hindi marks : 97
maths marks : 76
science marks : 59
Social marks : 87
total marks : 486
Grade = First class

12. Write a C program to print n natural numbers

/*prints n natural numbers by using while loop */


#include<stdio.h>
void main()
{int i,n;
printf("\n enter a number: ");
scanf("%d",&n);
i=1;
while(i<=n){
printf("\n %d",i);
++i;}
getch();
}
/*prints n natural numbers by using do while loop */
#include<stdio.h>
void main()
{int i,n;
printf("\n enter a number: ");
scanf("%d",&n);
i=1;
do{
printf("\n %d",i);
++i;}
while(i<=n);
getch();
}
/*prints n natural numbers by using go to statement */
#include<stdio.h>
void main()
{int i,n;
printf("\n enter a number :");
scanf("%d",&n);
i=1;
start:printf("\n %d",i);
++i;
if (i<=n) goto start ;
getch();
}
/*prints n natural numbers by using for loop */
#include<stdio.h>
void main()
{int i,n;
printf("\n enter a number :");
scanf("%d",&n);
for(i=1;i<=n;++i)printf("\n %d",i);
getch();
}

input

enter a number: 10

output
1
2
3
4
5
6
7
8
9
10

13. Write a c program to generate a multiplication table of given number N.

/*generates multiplication table of n */


#include<stdio.h>
void main()
{int i,n;
printf("\n enter a number for multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)printf("\n%d x %d = %d",n,i,n*i);
getch();
}

input:
enter a number for multiplication table: 5
output:
5x1= 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

14. Write a C program to convert string from upper case to lower case and vice versa.

/*converts string from upper case to lower case and vice versa*/
#include<stdio.h>
#include<string.h>
void main()
{ char a[20];
printf("\n enter lower case string to convert:");
scanf("%s",a);
printf("\nlower case string = %s",a);
printf("\nupper case string = %s",strupr(a));

getch();
printf("\n enter upper case string to convert:");
scanf("%s",a);
printf("\nuupper case string = %s",a);
printf("\nlowerr case string = %s",strlwr(a));
getch();

}
input:
enter lower case string to convert: rama
output:
lower case string = rama
upper case string = RAMA
input:

enter upper case string to convert: SURYA


output:

upper case string = SURYA


lower case string = surya

15. Write a C program to find factorial of given number N.

Factorial of n is written as n! and it’s value is


n! = n x (n-1) x (n-2) x (n-3) …3 x 2 x 1. In such a way
5! = 5 x 4 x 3 x 2 x 1 = 120 note that zero factorial is defined as one
i.e. 0!=1.

Here we are finding factorial in three ways


a) By using for loop.
b) By using function.
c) By using Recursive function.
/* factorial by using for loop */
/*finds factorial by using for loop*/
#include<stdio.h>
#include<conio.h>
void main()
{ int i,n,f=1;
clrscr();
printf("\n enter a number n=");
scanf("%d",&n);
for(i=1;i<=n;++i)f*=i;
printf("\n factorial of %d is %d",n,f);
getch();
}_

/* finds factorial by using function*/


#include <stdio.h>
#include<conio.h>
void main()
{
long int factorial();
int number;
clrscr();
printf("Enter a number n= ");
scanf("%d", &number);
printf("factorial of %d is %ld\n", number, factorial(number));
getch();
}
long factorial(int n)
{
int i;
long result = 1;

for (i = 1; i <= n; i++)


result = result * i;

return (result);
}

/* factorial by using recursive function */

/*finds factorial by using recursion*/


#include<stdio.h>
#include<conio.h>
void main()
{
long factorial(int);
int num;
long f;
clrscr();
printf("Enter a number n=");
scanf("%d", &num);

if (num < 0)
printf("Negative numbers are not allowed.\n");
else
{
f = factorial(num);
printf("factorial of %ld is %ld\n", num, f);
}
getch();
}

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

input
Enter a number n= 5
output
Factorial of 5 is 120

16.Write a c program to find largest of three given numbers.

/* finds biggest among three values*/


#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c,big;
clrscr();
printf("\ enter three values:");
scanf("%d %d %d",&a,&b,&c);
big=a;
if(big<b) big = b;
if (big<c) big = c;
printf("\n biggest of %d , %d and %d is %d",a,b,c,big);
getch();
}

input
Enter three values: 5 2 7
output
biggest of 5,2 and 7 is 7

17. Creating a single dimensional array of numbers and displaying the contents.
/*creating a single dimensional array of numbers and displaying them*/
#include<stdio.h>
#include<conio.h>
void main()
{int i,j,n,a[10];
clrscr();
printf("\n how many elements:");
scanf("%d",&n);
printf("\n enter %d values line by line\n",n);
for(i=0;i<n;++i)scanf("\n%d",&a[i]);
printf("\n given array\n");
for(j=0;j<n;++j)printf("\n%d",a[j]);
getch();
}

input
how many elements: 6
enter 6 values line by line
34
45
56
72
12
39
Output

given array

34
45
56
72
12
39

18. Write a C program to find largest number from an array.

#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], maximum, size, c, location = 1;

clrscr();
printf("Enter the number of elements in an array:");
scanf("%d", &size);

printf("Enter %d integers\n", size);

for (c = 0; c < size; c++)


scanf("%d", &array[c]);

printf("\n given array is \n");


for(c=0;c<size;++c)printf("\n %d",array[c]);
maximum = array[0];

for (c = 1; c < size; c++)


{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}

printf("\nMaximum element is present at location number %d and it's value is %d.\n", location, maximum);
getch();
}
Input
Enter the number of elements in an array:5
Enter 5 integers
22
34
53
45
26
output

given array
22
34
53
45
26
Maximum element is present at location number 3 and it's value is 53

19. Write a C program to sort the numbers in ascending/descending order.

Sorting means arranging the elements either in ascending order or descending order.
Given elements: 34,56,45,21,89,78
Ascending order: 21, 34 ,45,56,78,89
Descending order: 89,78,56,45,34,21

/* Bubble sort */

#include <stdio.h>
#include<conio.h>

void main()
{
int array[100], n, c, d, swap;
clrscr();

printf("Enter number of elements:");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("\n Given Array\n");

for (c = 0; c < n; c++)


printf("\n%d",array[c]);

for (c = 0 ; c < n - 1 ; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

getch();
}
Input:
Enter number of elements: 6
Enter 6 integers
34
56
45
21
89
78
output
Given Array
34
56
45
21
89
78
Sorted list in ascending order:
21 34 45 56 78 89

20. Write a C program to find length of a given array.

#include<stdio.h>
#include<string.h>

void main()
{
char a[100];
int length;

printf("Enter a string to calculate it's length:\n");


gets(a);

length = strlen(a);

printf("Length of entered string is = %d\n",length);

getch();
}
Input
Enter a string to calculate it's length: rama rao
output
Length of entered string is = 8

21. Displaying the Reverse of a given array.

/*displays reverse of a given array*/


#include<stdio.h>
#include<conio.h>
void main()
{int i,j,n,a[10];
clrscr();
printf("\n How many elements: ");
scanf("%d",&n);
printf("Enter the %d elements line by line\n ",n);
for(i=0;i<n;++i)scanf("\n%d",&a[i]);
printf("\n given array\n");
for(i=0;i<n;++i)printf("\n%d",a[i]);
printf("\n Reverse of array\n");
for(j=n-1;j>=0;--j)printf("\n%d",a[j]);
getch();
}
input
How many elements: 5
Enter the 5 elements line by line
21
32
12
42
36
output
given array

21
32
12
42
36
Reverse of array

36
42
12
32
21

22. Adding two single dimensional arrays.

A[0] + B[0] =sum[0]


A[1] + B[1] =sum[1]
A[2] + B[2] =sum[2]
A[3] + B[3] =sum[3]

A[n] + B[n] =sum[n]

/*adding two single dimensional arrays*/


#include <stdio.h>
#include<conio.h>
void main()
{
int c,n, a[10], b[10], sum[10];

clrscr();
printf("How many elements in the array :");
scanf("%d", &n);
printf("\n Enter %d values of array A line by line\n",n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &a[c]);

printf("Enter %d values of array B line by line\n",n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &b[c]);
/* process for sum*/
for ( c = 0 ; c < n ; c++ )
sum[c] = a[c] + b[c];

printf("\n Array-A is \n");


for ( c = 0 ; c < n ; c++ )
printf("\n %d", a[c]);

printf("\n Array-B is\n");


for ( c = 0 ; c < n ; c++ )
printf("\n %d", b[c]);

printf("\n Sum of array A and array B (A+B):-\n");

for ( c = 0 ; c < n ; c++ )


printf("\n%d", sum[c]);

getch();

}
input
How many elements in the array :5
Enter 5 values of array A line by line
12
3
4
2
6
Enter 5 values of array B line by line
6
8
7
4
3
Output

Array-A is

12
3
4
2
6
Array-B is

6
8
7
4
3
Sum of array A and array B (A+B):-

18
11
11
6
9

23 . Write a C program for matrix addition.

/*matrix addition*/
#include <stdio.h>
#include<conio.h>
void main()
{
int ar,ac,br,bc, c, d, a[10][10], b[10][10], sum[10][10];

clrscr();
printf("Enter the number of rows in matrix-A: ");
scanf("%d", &ar);
printf("Enter the number of columns in matrix-A: ");
scanf("%d", &ac);

printf("Enter the number of rows in matrix-B: ");


scanf("%d", &br);
printf("Enter the number of columns in matrix-B: ");
scanf("%d", &bc);

if (( ar != br) || (ac!=bc)) goto lastpara;


printf("Enter %d elements of first matrix-A\n",ar*ac);

for ( c = 0 ; c < ar ; c++ )


for ( d = 0 ; d < ac ; d++ )
scanf("%d", &a[c][d]);
printf("Enter %d elements of second matrix-B\n",br*bc);

for ( c = 0 ; c < br ; c++ )


for ( d = 0 ; d < bc ; d++ )
scanf("%d", &b[c][d]);

for ( c = 0 ; c <ar ; c++ )


for ( d = 0 ; d <ac ; d++ )
sum[c][d] = a[c][d] + b[c][d];

printf("\n matrix-A is \n");


for ( c = 0 ; c < ar ; c++ )
{
for ( d = 0 ; d < ac; d++ )
printf("%d\t", a[c][d]);

printf("\n");
}
printf("\n matrix-B is\n");
for ( c = 0 ; c < br ; c++ )
{
for ( d = 0 ; d < bc; d++ )
printf("%d\t", b[c][d]);

printf("\n");
}

printf("Sum of entered matrices(A+B):-\n");

for ( c = 0 ; c < ar ; c++ )


{
for ( d = 0 ; d < ac; d++ )
printf("%d\t", sum[c][d]);

printf("\n");
}
goto endpara;
lastpara: printf("\n rows and columns are not equal, hence can not add matrices");
endpara:
getch();

Input

Enter the number of rows in matrix-A 2


Enter the number of columns in matrix-A 2
Enter the number of rows in matrix-B 2
Enter the number of columns in matrix-B 3
rows and columns are not equal, hence can not add matrices

Enter the number of rows in matrix-A: 2


Enter the number of columns in matrix-A: 2
Enter the number of rows in matrix-B: 2
Enter the number of columns in matrix-B: 2
Enter 4 elements of first matrix-A
1
2
3
4

Enter 4 elements of second matrix-B


5
6
7
8

output
matrix-A is
1 2
3 4

matrix-B is
5 6
7 8
Sum of entered matrices(A+B):-
6 8
10 12

24.Write a C program to find whether the given number is palindrome or not.

Palindrome Numbers
A palindrome number is a number such that if we reverse it, it will not change. For example some
palindrome numbers examples are 121, 232, 12321,545. To check whether a number is palindrome or not
first we reverse it and then compare the number obtained with the original, if both are same then number is
palindrome otherwise not a palindrome.

Palindrome number algorithm


1. Get the number from user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.

/*finds palindrome or not */


#include<stdio.h>
void main()
{ int n, rn = 0 ,temp;
printf("\n enter a number to check whether palindrome or not");
scanf("%d",&n);
temp = n;
while (temp != 0){
rn = rn*10 + temp%10;
temp = temp/10;}
if(n==rn) printf("\n %d is a palindrome",n);
else printf("\n %d is not a palindrome",n);
getch();
}

input:
enter a number to check whether palindrome or not 12
output:
12 is not a palindrome

input:
enter a number to check whether palindrome or not 232
output:
232 is a palindrome

25. String handling functions:


Using string functions like strlen(), strcat(),strcpy(),strrev(), etc.
a) String length
/*string length*/
#include<stdio.h>
#include<string.h>

void main()
{
char arr[100];

printf("Enter a string to to find length of it: \n");


gets(arr);

printf("The string length = %d ", strlen(arr));

getch();
}
input
Enter a string to to find length of it: rama rao
output
The string length = 8

B)String concatenation
/*string concatenation */
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char a[100], b[100];

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


gets(a);

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


gets(b);

strcat(a,b);
printf("String obtained on concatenation is %s\n",a);

getch();

}
input
Enter the first string Bheemeswara
Enter the second string Rao
output
String obtained on concatenation is BheemeswaraRao

C) string copy
/*string copy */
#include<stdio.h>
#include<string.h>

void main()
{
char source[] = "abc";
char destination[]="xyz";

printf("\n Before copy\n");


printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);

strcpy(destination, source);
printf("\n After copy\n");

printf("Source string: %s\n", source);


printf("Destination string: %s\n", destination);
getch();

}
output
Before copy
Source string: abc
Destination string: xyz

After copy
Source string: abc
Destination string: abc

D)string reverse
/*string reverse*/
#include<stdio.h>
#include<string.h>

void main()
{
char arr[100];

printf("Enter a string to reverse:\n ");


gets(arr);

strrev(arr);
printf("Reverse of entered string is: \n%s\n ",arr);

getch();
}
input
Enter a string to reverse: abcdef
output
Reverse of entered string is: fedcba

26. Writing and calling a function to print 25 ‘*’ in a line. & ENGINEERING SYLLABUS
/* Writing and calling a function to print 25 * in a line*/

#include<stdio.h>
#include<conio.h>
void main()
{
void printstars(void);

clrscr();
printstars();
getch();
}

void printstars(void)
{ int i;
for(i=1;i<=25;++i)printf("*");
}

Output

*************************

27. Write and call a function to print n number of ‘*’ in a line, where n is the parameter passed to
The function.
/* Writing and calling a function to print ‘*’ in a line*/
/* where n is a parameter passed to a function */
#include<stdio.h>
#include<conio.h>
void main()
{
void printstars(int x);
int n;
clrscr();
printf("\n enter number of stars to be printed ");
scanf("%d",&n);
printstars(n);
getch();
}

void printstars(int x)
{ int i;
for(i=1;i<=x;++i)printf("*");
}
Input
enter number of stars to be printed 5

Output

*****

28. Writing and calling functions to add, subtract and multiply two numbers.

/*writing and calling functions to add, subtract and multiply */


/* two integers */
#include<stdio.h>
#include<conio.h>
void main()
{
int add(int x , int y);
int sub(int x , int y);
int mul(int x , int y);
int m,n;
clrscr();
printf("\nenter two integers ");
scanf("%d %d",&m,&n);
printf("\n %d + %d is %d",m,n,add(m,n));
printf("\n %d - %d is %d",m,n,sub(m,n));
printf("\n %d * %d is %d",m,n,mul(m,n));
getch();
}
int add(int x,int y)
{ int p;
p=x+y;
return(p);}

int sub(int x,int y)


{ int p;
p=x-y;
return(p);}
int mul(int x,int y)
{ int p;
p=x*y;
return(p);}

Input

enter two integers 3 5

Output
3 + 5 is 8
3 - 5 is -2
3 * 5 is 15

29. Use trigonometric functions to display Sin and Cos value of degrees from 0 to 180 degrees in
Steps of 30 degrees.
To find the value of trigonometric functions such as sin() ,or cos() etc., the degrees must be converted into
radians as follows

180 degrees = ∏ radians


= 22/7 radians
= 3.142 radians

1 degree = (22/7)/180 radians

= (22/(7*180)) radians

In the program must include <math.h> function.

With these things, the program becomes quite simple :

/*Trigonmetric functions to display sin and cos values */


/* of degrees from 0 to 180 in steps of 30 degrees */

#include<stdio.h>
#include<math.h>
void main()
{
double degrees,radians;

printf("\n Degrees Sin() Cos() " );


for(degrees = 0 ;degrees <=180;degrees += 30){
radians = degrees * (22.0/(7.0 * 180.0));
printf("\n %lf %lf %lf ", degrees, sin(radians),cos(radians));}
getch();
}

Output
Degrees Sin() Cos()
0.000000 0.000000 1.000000
30.000000 0.500183 0.865920
60.000000 0.866236 0.499635
90.000000 1.000000 -0.000632
120.000000 0.865604 -0.500730
150.000000 0.499087 -0.866552
180.000000 -0.001264 -0.999999

30. Write a C Program to find real roots of a quadratic equation by using


sqrt() function.

#include <stdio.h>
#include <math.h>

void main()
{
int a, b, c, d;
double root1, root2;

printf("Enter values of a, b and c ");


scanf("%d%d%d", &a, &b, &c);

d = b*b - 4*a*c;

if (d < 0) { /*complex roots*/


printf("First root = %.2lf + j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second root = %.2lf - j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
}
else { /*real roots*/
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);

printf("First root = %.2lf\n", root1);


printf("Second root = %.2lf\n", root2);
}

getch();
}

output

Enter values of a, b and c 1 2 1


First root = -1.00
Second root = -1.00

Enter values of a, b and c 2 4 5


First root = -1.00 + j1.22
Second root = -1.00 - j1.22

31. Write C Program for following case: Create a structure by name “book” containing book no, book name,
author and cost as members. Create Book1 and Book2 as copies of this structure and display the values for
two books and display the total cost of the books.

#include <stdio.h>
#include <conio.h>
void main()
{
struct book
{ int bookno;
char bookname[20];
char author[20];
int cost;
} book1 , book2;
int total;

clrscr();
printf("\n Enter Book No : ");
scanf("%d",&book1.bookno);
printf("\n Enter Book Name : ");
scanf("%s",book1.bookname);
printf("\n Enter author name : ");
scanf("%s",book1.author);
printf("\n Enter Book cost : ");
scanf("%d" ,&book1.cost);
printf("\n Enter Book No : ");
scanf("%d",&book2.bookno);
printf("\n Enter Book Name : ");
scanf("%s",book2.bookname);
printf("\n Enter author name : ");
scanf("%s",book2.author);
printf("\n Enter Book cost : ");
scanf("%d" ,&book2.cost);

printf("\n Book No : %d" , book1.bookno);


printf("\n Book Name : %s", book1.bookname);
printf("\n author name : %s",book1.author);
printf("\n Book cost : %d",book1.cost);
printf("\n");

printf("\n Book No : %d" , book2.bookno);


printf("\n Book Name : %s", book2.bookname);
printf("\n author name : %s",book2.author);
printf("\n Book cost : %d",book2.cost);
printf("\n");
total = book1.cost + book2.cost;
printf("\n Total cost of books = %d",total);

getch();
}

output

Book No : 101
Book Name : GFC
author name : SURESH
Book cost : 120

Book No : 102
Book Name : MSOFFICE
author name : BHEEMESWAR
Book cost : 200

Total cost of books = 320

32. Write a C Program in this case: Create a structure by name “employee” with necessary data members and
create an array of five employees and display the values.
/*displays details of employees by using structures with arrays */
#include <stdio.h>
#include <conio.h>
void main()
{
struct employee
{ int empidno;
char empname[20];
char designation[20];
int salary;
} emp[5];

int i,n;

clrscr();
printf("\n how many employees are to be entered");
scanf("%d",&n);
for (i=0;i<n;++i){

printf("\n Enter Employee IDNo : ");


scanf("%d",&emp[i].empidno);
printf("\n Enter Employee Name : ");
scanf("%s",emp[i].empname);
printf("\n Enter designation : ");
scanf("%s",emp[i].designation);
printf("\n Enter salary : ");
scanf("%d" ,&emp[i].salary);
printf("\n");}
printf("\n employee list\n");

for (i=0;i<n;++i){

printf("\n Employee IDNo : ");


printf("%d",emp[i].empidno);
printf("\n Employee Name : ");
printf("%s",emp[i].empname);
printf("\n Designation : ");
printf("%s",emp[i].designation);
printf("\n Salary : ");
printf("%d" ,emp[i].salary);
printf("\n");}

getch();
}

Output:

employee list

Employee IDNo : 101


Employee Name : SRR
Designation : JUDGE
Salary : 45000
Employee IDNo : 102
Employee Name : SBR
Designation : LECTURER
Salary : 42000

Employee IDNo : 103


Employee Name : STR
Designation : ENGINEER
Salary : 60000

33.File handling functions

a) Read a file.
#include<stdio.h>
#include<stdlib.h>

void main()
{
char ch, file_name[25];
FILE *fp;

printf("Enter the name of file you wish to see ");


gets(file_name);

fp = fopen(TEST.C,"r"); /* read mode */

if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}

printf("The contents of %s file are :- \n\n", file_name);

while( ( ch = fgetc(fp) ) != EOF )


printf("%c",ch);

fclose(fp);
getch();
}

b) File copying

#include <stdio.h>
#include <stdlib.h>

void main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);

source = fopen(source_file, "r");

if( source == NULL )


{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}

printf("Enter name of target file\n");


gets(target_file);

target = fopen(target_file, "w");

if( target == NULL )


{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}

while( ( ch = fgetc(source) ) != EOF )


fputc(ch, target);

printf("File copied successfully.\n");

fclose(source);
fclose(target);

getch();
}

34. Writing a program to create a simple text file and write and read data from it using file operation
functions like fopen() etc.

a)write a file

/*write a file(file creation) with some names*/


#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int i,n;
char sname[20];
clrscr();
fp = fopen("file.txt","w");
/*Create a file and add text*/
printf("\n how many names you want to write into a file");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("\enter a name :");
scanf("%s",sname);
fprintf(fp,"\n %s",sname); /*writes data to the file*/
}
fclose(fp); /*done!*/
getch();
}
input
how many names you want to write into a file 3
enter a name :Dharmaraja
enter a name :Bheema
enter a name :Arjuna
Output
Output is stored in the file named file.txt.
so to get the output
give the following command at the location where file is stored

Type file.txt

Dharmaraja
Bheema
Arjuna

b)append a file

/*append a file(add some names to a file)*/


#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int i,n;
char sname[20];
clrscr();
fp = fopen("file.txt","a");
/*Create a file and add text*/
printf("\n how many names you want to add(append) to a file:");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("\enter a name :");
scanf("%s",sname);
fprintf(fp,"\n %s",sname); /*writes data to the file*/
}
fclose(fp); /*done!*/
getch();
}

input
how many names you want to add(append) to a file: 2
enter a name : Nakula
enter a name : Sahadeva

Output
Output is stored in the file named file.txt.
so to get the output
give the following command at the location where the file is stored

Type file.txt

Dharmaraja
Bheema
Arjuna
Nakula
Sahadeva

c) Read a file

/*reads a file */

#include <stdio.h>
#include<conio.h>
void main( )
{
FILE *fp;
char c;
clrscr();
fp = fopen("file.txt", "r");
if (fp == NULL)
printf("File doesn't exist\n");
else
do {
c = getc(fp); /* get one character from the file */
putchar(c);} /* display it on the monitor */
while(c != EOF);
fclose(fp);
getch();
}

Output

Dharmaraja
Bheema
Arjuna
Nakula
Sahadeva

35. Writing a program to write integers to a file, read them and print them into two file depending on
whether they are even or odd.

/* Reads integers from a file and writes integers into a file */


/* named even and writes odd numbers into a file named odd */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i;

clrscr();
f1=fopen("data","w");
for(i=1;i<20;i++)
{
printf("\n enter a number ");
scanf("%d",&number);
if(number == -1)
break;
putw(number,f1);
}
fclose(f1);

f1=fopen("data","r");
f2=fopen("ODD","w");
f3=fopen("EVEN","w");
printf("\n integers in the first file is\n");
while((number=getw(f1))!=EOF)/* Reading data from the file*/
{printf(" %d",number);
if(number%2==0)
putw(number,f3);/*Write to even Numbers file*/
else
putw(number,f2);/*write to odd Numbers file*/
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\nData From the odd file\n");
while((number=getw(f2))!=EOF)
printf(" %d",number);
printf("\n\nData from the even file\n");
while((number=getw(f3))!=EOF)
printf(" %d",number);
fclose(f2);
fclose(f3);
getch();
}

Input
enter a number 12
enter a number 34
enter a number 45
enter a number 67
enter a number 24
enter a number 78
enter a number 55
enter a number 89
enter a number -1
Output
integers in the first file is
12 34 45 67 24 78 55 89

Data From the odd file


45 67 55 89

Data from the even file


12 34 24 78

36.Write a C program to print Fibonacci Series

C program for Fibonacci series without and with recursion. Using the code below you can print as many
number of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First
few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the
sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in
mathematics and Computer Science.

/* Fibonacci Series c language */


#include<stdio.h>

void main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");


scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);

for ( c = 0 ; c < n ; c++ )


{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

getch();
}

/* Fibonacci series with recursion */


#include<stdio.h>
void main()
{ int Fibonacci(int);

int n, i = 0, c;
printf("Enter the number of terms:\n");
scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);

for ( c = 1 ; c <= n ; c++ )


{
printf("%d\n", Fibonacci(i));
i++;
}

getch();
}

int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Input
Enter the number of terms: 10
Output
First 10 terms of Fibonacci series are :-
0
1
1
2
3
5
8
13
21
34

37. Write a C program to find whether a given number is prime or not.

Prime number: A number which is only divisible by one and itself is called a prime number. Divisible means
remainder is zero. 5 is a prime number because it is only divisible by one and five. 6 is not a prime number
because it is divisible by one, two, three and six.

/*finds prime or not */


#include<stdio.h>
void main()
{
int c=0,i,n;
printf("enter the number to be checked: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

if(n%i==0)
{
c=c+1;
}
}
if (c==2)
printf("%d is a prime",n);
else
printf("%d is not a prime",n);
getch();
}
input
enter the number to be checked: 5
output
5 is a prime
Input
enter the number to be checked: 6
output
6 is not a prime

38. Write a C Program to find first n prime numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i,j,p,c;
clrscr();
printf("Enter the number of prime numbers required: ");
scanf("%d",&n);
printf("\n first %d prime numbers \n",n);
p=2;
i=1;
while( i <= n) {

c=0;

for ( j=1;j<=p;++j)
{
if ( p%j == 0 )++c;

}
if ( c == 2 )
{
printf("%d\n",p);
++i;
}

++p;
}

getch();
}
input
Enter the number of prime numbers required: 5
output
first 5 prime numbers
2
3
5
7
11

39. write a c program for matrix multiplication

/*matrix multiplication*/

#include<stdio.h>
#include<conio.h>
void main()
{
int ar,ac,br,bc, c, d, e, a[10][10], b[10][10], mul[10][10];

clrscr();
printf("Enter the number of rows in matrix-A: ");
scanf("%d", &ar);
printf("Enter the number of columns in matrix-A: ");
scanf("%d", &ac);

printf("Enter the number of rows in matrix-B: ");


scanf("%d", &br);
printf("Enter the number of columns in matrix-B: ");
scanf("%d", &bc);

if ( ac != br) goto lastpara;


printf("Enter %d elements of first matrix-A\n",ar*ac);

for ( c = 0 ; c < ar ; c++ )


for ( d = 0 ; d < ac ; d++ )
scanf("%d", &a[c][d]);

printf("Enter %d elements of second matrix-B\n",br*bc);

for ( c = 0 ; c < br ; c++ )


for ( d = 0 ; d < bc ; d++ )
scanf("%d", &b[c][d]);

for ( c = 0 ; c <ar ; c++ )


for ( d = 0 ; d <bc ; d++ ){
mul[c][d] = 0;
for(e = 0;e<ac;++e)
mul[c][d] = mul[c][d] + a[c][e] * b[e][d];}
printf("\n matrix-A is \n");
for ( c = 0 ; c < ar ; c++ )
{
for ( d = 0 ; d < ac; d++ )
printf("%d\t", a[c][d]);

printf("\n");
}
printf("\n matrix-B is\n");
for ( c = 0 ; c < br ; c++ )
{
for ( d = 0 ; d < bc; d++ )
printf("%d\t", b[c][d]);

printf("\n");
}

printf(" A x B :-\n");

for ( c = 0 ; c < ar ; c++ )


{
for ( d = 0 ; d < bc; d++ )
printf("%d\t", mul[c][d]);

printf("\n");
}
goto endpara;
lastpara:printf("\n columns in matrix -A is not equal to rows in matrix -B, hence can not multiply");
endpara:
getch();

}
input
Enter the number of rows in matrix-A:2
Enter the number of columns in matrix-A: 2
Enter the number of rows in matrix-B:2
Enter the number of columns in matrix-B: 2
Enter 4 elements of first matrix-A
1
2
3
4

Enter 4 elements of second matrix-B


5
6
7
8

output
matrix-A is
1 2
3 4
matrix-B is
5 6
7 8
A x B :-
19 22
43 50

You might also like