C Programs Lab
C Programs Lab
C Programs Lab
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:
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];
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;
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
input
enter radius: 7
radius = 7.000000
output
#include<stdio.h>
void main()
{ float centi,fahren,miles,km,pounds,kgs; char x;
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
output
student name = surya
getch();
}
#include<stdio.h>
void main()
{
int 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();
}
main()
{
int n;
printf("\nEnter an integer:");
scanf("%d",&n);
getch();
}
input output
9. Write a C program to find the result whether the student passed or failed based on
marks.
input:
output:
student name is surya
total marks = 160
Result is passed
10. Finding the weekly wages of a worker taking overtime work into consideration.
Input
Output
Total payment in the week = 1800 Rupees
11. Finding the grade obtained by a student based on the total marks obtained.
/*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);
getch();
}
Input
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
input
enter a number: 10
output
1
2
3
4
5
6
7
8
9
10
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:
return (result);
}
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
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
#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("\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
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();
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
#include<stdio.h>
#include<string.h>
void main()
{
char a[100];
int length;
length = strlen(a);
getch();
}
Input
Enter a string to calculate it's length: rama rao
output
Length of entered string is = 8
21
32
12
42
36
Reverse of array
36
42
12
32
21
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]);
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
/*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("\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("\n");
}
goto endpara;
lastpara: printf("\n rows and columns are not equal, hence can not add matrices");
endpara:
getch();
Input
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
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.
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
void main()
{
char arr[100];
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];
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";
strcpy(destination, source);
printf("\n After copy\n");
}
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];
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.
Input
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
= (22/(7*180)) radians
#include<stdio.h>
#include<math.h>
void main()
{
double degrees,radians;
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
#include <stdio.h>
#include <math.h>
void main()
{
int a, b, c, d;
double root1, root2;
d = b*b - 4*a*c;
getch();
}
output
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);
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
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){
for (i=0;i<n;++i){
getch();
}
Output:
employee list
a) Read a file.
#include<stdio.h>
#include<stdlib.h>
void main()
{
char ch, file_name[25];
FILE *fp;
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
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);
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
Type file.txt
Dharmaraja
Bheema
Arjuna
b)append a file
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.
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
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.
void main()
{
int n, first = 0, second = 1, next, c;
getch();
}
int n, i = 0, c;
printf("Enter the number of terms:\n");
scanf("%d",&n);
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
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.
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
#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
/*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("\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");
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
output
matrix-A is
1 2
3 4
matrix-B is
5 6
7 8
A x B :-
19 22
43 50