You are on page 1of 24

CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,

LUCKNOW

COMPUTER SYSTEMS AND PROGRAMMING IN ‘C’


LAB
RCS-151

FACULTY-
NIHARIKA SRIVASTAVA
ASSISTANT PROFESSOR
M.TECH (SOFTWARE ENGINEERING)

1|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 1
WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by
the student.

#include<stdio.h>
int main() {
int s1, s2, s3, s4, s5, sum, total = 500;
float per;
printf("\nEnter marks of 5 subjects : ");
scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
sum = s1 + s2 + s3 + s4 + s5;
printf("\nSum : %d", sum);
per = (sum * 100) / total;
printf("\nPercentage : %f", per);
return (0);
}

Program 2
WAP that calculates the Simple Interest and Compound Interest. The Principal , Amount, Rate of
Interest and Time are entered through the keyboard.

#include<stdio.h>
void main()
{
float p,r,t,i;
clrscr();
printf("Enter principal amount : ");
scanf("%f",&p);
printf("\nEnter Rate of Interest : ");
scanf("%f",&r);
printf("\nEnter time period : ");
scanf("%f",&t);
i=(p*r*t)/100;
printf("\nInterest calculated is %f",i);

2|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

getch();
}

Program 3
WAP to calculate the area and circumference of a circle.

#include<stdio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}

Program 4

WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the formula
C/5=(F-32)/9.

#include<stdio.h>
int main()
{
float celsius, fahrenheit;
printf("\nEnter temp in Celsius : ");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
return (0);
}

3|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 5

WAP that swaps values of two variables using a third variable

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

int main()
{
int a,b,temp;
printf("enter two values\n");
printf("\na = ");
scanf("%d",&a);
printf("\nb = ");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("\na = %d\tb = %d ", a, b);
getch();
}

Program 6

WAP that checks whether the two numbers entered by the user are equal or not.

#include <stdio.h>
void main()
{
int m,n;
printf("Enter the values for M and N\n");
scanf("%d %d", &m,&n);
if(m == n )
printf("M and N are equal\n");
else
printf("M and N are not equal\n");

4|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 7

WAP to find the greatest of three numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();

printf("Enter the value of a:");


scanf("%d",&a);
printf("\nEnter the value of b:");
scanf("%d",&b);
printf("\nEnter the value of c:");
scanf("%d",&c);
if(a>b)
{if(a>c)
printf("a is largest");
else
printf("c is largest");}
else
if(b>c)
printf("b is largest");
else
printf("c is largest");
getch();
}

Program 8

WAP that finds whether a given number is even or odd.


#include<stdio.h>
int main(){
int number;
printf("Enter any integer: ");
scanf("%d",&number);
if(number % 2 ==0)
printf("%d is even number.",number);
else
printf("%d is odd number.",number);
return 0;
}

5|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 9

WAP that tells whether a given year is a leap year or not.


#include <stdio.h>
void main()
{
int year;
printf("Enter a year\n");
scanf("%d",&year);
if ( (year % 4) == 0)
printf("%d is a leap year",year);
else
printf("%d is not a leap year\n",year);
}

Program 10

WAP that accepts marks of five subjects and finds percentage and prints grades according to
the following criteria:
Between 90-100%--------------Print ‘A’
80-90%----------------------------Print ‘B’
60-80%---------------------------Print ‘C’
Below 60%----------------------Print ‘D’

#include<stdio.h>
#include<conio.h>
void main()
{
float m1,m2,m3,m4,m5,percent;
clrscr();
printf("Enter the marks of the student in 5 subjects:\n");
scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
percent=(m1+m2+m3+m4+m5)/5;
printf("\nPercentage=%f",percent);
if(percent>90.0 && percent<=100.0)
printf("\nGrade:A");
else if(percent>80.0 && percent<=90.0)
printf("\nGrade:B");
else if(percent>60.0 && percent<=80.0)
printf("\nGrade:C");
else

6|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

printf("\nGrade:D");
getch();
}

Program 11

WAP that takes two operands and one operator from the user and perform the operation and
prints the result by using Switch statement.

#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}

7|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 12

WAP to print the sum of all numbers up to a given number.


#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);

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


{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}

Program 13

WAP to find the factorial of a given number.


#include<stdio.h>
void main()
int fact=1, n,i;
printf(“Enter the number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“The factorial is”);
printf(“%d”,fact);
}

8|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 14

WAP to print sum of even and odd numbers from 1 to N numbers.

#include <stdio.h>
void main()
{
int i, num, odd_sum = 0, even_sum = 0;
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}

Program 15

WAP to print the Fibonacci series.

#include<stdio.h>
int main() {
int first, second, sum, num, counter = 0;
printf("Enter the term : ");
scanf("%d", &num);
printf("\nEnter First Number : ");
scanf("%d", &first);
printf("\nEnter Second Number : ");
scanf("%d", &second);
printf("\nFibonacci Series : %d %d ", first, second);
while (counter < num) {
sum = first + second;
printf("%d ", sum);
first = second;
second = sum;
counter++;
}
return (0);
}

9|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 16
WAP to check whether the entered number is prime or not.

#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}

Program 17
WAP to find the sum of digits of the entered number

#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}

10 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 18

WAP to find the reverse of a number.


#include <stdio.h>
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}

Program 19
WAP to print Armstrong numbers from 1 to 100.
#include <stdio.h>
void main()
{
int number, temp, digit1, digit2, digit3;

printf("Print all Armstrong numbers between 1 and 1000:\n");


number = 001;
while (number <= 900)
{
digit1 = number - ((number / 10) * 10);
digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);
temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3);
if (temp == number)
{
printf("\n Armstrong no is:%d", temp);
}
number++;
}
}

11 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 20

WAP to convert binary number into decimal number and vice versa.

#include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
scanf("%c",&c);
if (c =='d' || c == 'D')
{
printf("Enter a binary number: ");
scanf("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c == 'B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}

int decimal_binary(int n) /* Function to convert decimal to binary.*/


{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}

int binary_decimal(int n) /* Function to convert binary to decimal.*/


12 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}

Program 21

WAP that simply takes elements of the array from the user and finds the sum of these
elements.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,sum;
float avg=0.0;
printf("Enter the elements of array");
for(i=0;i<5;i++)
scanf("%d" ,&a[i]);
sum=a[0]+a[1]+a[2]+a[3]+a[4];
printf("\n The sum of array is %d " ,sum);
getch();
}

Program 22

WAP that inputs two arrays and saves sum of corresponding elements of these arrays in a third
array and prints them.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];

13 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

int b[10];
int sum[10];
int i , n;
clrscr();
printf(" Enter the size of Array A and B\n");
scanf("%d" , &n);
printf(" Enter the elements of Array A\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &a[i]);
}
printf(" Enter the elements of Array B\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &b[i]);
}
for(i=0 ; i<n ; i++)
sum[i] = a[i] + b[i];
printf(" Sum of elements of A and B\n");
{
printf("%d\n" , sum[i] );
}
}

Program 23

WAP to find the minimum and maximum element of the array.

#include<stdio.h>
int main() {
int a[30], i, num, largest, smallest;
printf("\nEnter no of elements :");
scanf("%d", &num);
for (i = 0; i < num; i++)
scanf("%d", &a[i]);
largest = a[0];
for (i = 0; i < num; i++) {
if (a[i] > largest) {
largest = a[i];
}
}
printf("\nLargest Element : %d", largest);
smallest = a[0];

14 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

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


if (a[i] < smallest) {
smallest = a[i];
}
}

printf("\nSmallest Element : %d", smallest);


return (0);
}

Program 24

WAP to search an element in a array using Linear Search.

#include <stdio.h>
int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");


scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

return 0;
}

Program 25

15 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

WAP to sort the elements of the array inascending order using Bubble Sort technique

#include <stdio.h>
#define MAXSIZE 10

void main()
{ int array[MAXSIZE];
int i, j, num, temp;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}

16 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

Program 26
.
WAP to add and multiply two matrices of order nxn.

#include<stdio.h>
int main() {
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;

printf("\nEnter the number of Rows of Mat1 : ");


scanf("%d", &row1);
printf("\nEnter the number of Cols of Mat1 : ");
scanf("%d", &col1);

printf("\nEnter the number of Rows of Mat2 : ");


scanf("%d", &row2);
printf("\nEnter the number of Columns of Mat2 : ");
scanf("%d", &col2);

/* Before accepting the Elements Check if no of


rows and columns of both matrices is equal */
if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same ");
exit(0);
}

//Accept the Elements in Matrix 1


for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("Enter the Element a[%d][%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}

//Accept the Elements in Matrix 2


for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++) {
printf("Enter the Element b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]);

17 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

//Addition of two matrices


for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}

//Print out the Resultant Matrix


printf("\nThe Addition of two Matrices is : \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("%d\t", mat3[i][j]);
}
printf("\n");
}

return (0);
}
Program 27
WAP that finds the sum of diagonal elements of a mxn matrix.
#include<stdio.h>
int main() {

int i, j, mat[10][10], row, col;


int sum = 0;

printf("\nEnter the number of Rows : ");


scanf("%d", &row);

printf("\nEnter the number of Columns : ");


scanf("%d", &col);

//Accept the Elements in m x n Matrix


for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("\nEnter the Element a[%d][%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}

18 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

//Addition of all Diagonal Elements


for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (i == j)
sum = sum + mat[i][j];
}
}

//Print out the Result


printf("\nSum of Diagonal Elements in Matrix : %d", sum);

return (0);
}
Program 28
WAP to implement strlen (), strcat (),strcpy () using the concept of Functions

1. Length of the string (strlen)


The syntax of strlen is :

strlen(string);
It calculates the length of the string and returns its length. For example:

#include<string.h>

string = "Mumbai";

printf("Length = %d",strlen(string));
The above code displays 5, because Mumbai consists of 5 characters. Note: it does not count null
character.
2. Joining two strings (strcat)
The syntax of strcat is

strcat(string1,string2);
Now it removes the null character from string1 and joins the first character of string2 at that
position. Now string1 consists of both string1 and string2 in joined form. Example:

#include<string.h>

char string1[] = "Anti";

char string2[] = "Particle";

19 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

strcat(string1,string2);

printf("%s",string1); //display AntiParticle


3. Comparing two strings(strcmp)
The syntax of strcmp is

strcmp(string1,string2);
It returns 0 if string1 is same as string2 and returns 1 if they are not same. Example:

#include<string.h>

char string1 = "Nepal";

char string2 = "Srilanka";

if(strcmp(string1,string2)==0){

printf("They are equal");

}else{

printf("They are not equal"); //this is executed

}
4. Copying one string to another (strcpy)
The syntax of strcpy is

strcpy(destination_string, source_string);
It copies the content of source_string to destination_string. Example:

#include<string.h>

char source[] = "Hello";

char destination[10]; //uninitialized

strcpy(destination,source);

printf("%s",destination); //prints Hello

20 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

These are some of the functions in string.h for string operation. To use these functions you must
include header file <string.h>. But we can make our own functions to perform above task
without including string,h. Here is the complete source code that has own functions find_length
(like strlen) to find length, join_strings( like strcat) for joining strings, compare_strings(like
strcmp) for comparing two strings and copy_string(like strcpy) to copy one string from another.
Observer carefully the code, if you are beginner, you will learn a lot of things about string
operation.

Program 30
WAP to swap two elements using the concept of pointers.

#include<stdio.h>
void swap(int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}

int main() {
int num1, num2;
printf("\nEnter the first number : ");
scanf("%d", &num1);
printf("\nEnter the Second number : ");
scanf("%d", &num2);
swap(&num1, &num2);
printf("\nFirst number : %d", num1);
printf("\nSecond number : %d", num2);

return (0);
}
Program 31
WAP to compare the contents of two files and determine whether they are same or not.

#include<stdio.h>
int main() {
FILE *fp1, *fp2;
int ch1, ch2;
char fname1[40], fname2[40];

21 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

printf("Enter name of first file :");


gets(fname1);

printf("Enter name of second file:");


gets(fname2);

fp1 = fopen(fname1, "r");


fp2 = fopen(fname2, "r");

if (fp1 == NULL) {
printf("Cannot open %s for reading ", fname1);
exit(1);
} else if (fp2 == NULL) {
printf("Cannot open %s for reading ", fname2);
exit(1);
} else {
ch1 = getc(fp1);
ch2 = getc(fp2);

while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2)) {


ch1 = getc(fp1);
ch2 = getc(fp2);
}

if (ch1 == ch2)
printf("Files are identical n");
else if (ch1 != ch2)
printf("Files are Not identical n");

fclose(fp1);
fclose(fp2);
}
return (0);
}
Program 32
WAP to check whether a given word exists in a file or not. If yes then find the number of times it
occurs.

#include "stdafx.h"
# include <stdio.h>

22 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

# include <conio.h>
# include<string.h>
# include <stdlib.h>
# include <conio.h>
# include <stdlib.h>

void checkRepeats( char sentence[], char text[]);

int _tmain(int argc, _TCHAR* argv[])


{
while(1)
{
system("cls");
char sentence[500], x, text[10];

printf("Enter the string/sentence\n=-=-=->");


fgets(sentence, sizeof(sentence), stdin);
printf("please enter the search word\n=-=-=->");
fgets(text, sizeof(text), stdin);

checkRepeats(sentence, text);

printf("\n\nPress ENTER to try again, '$' to quit : ");


x = getchar();
if (x=='$')
break;
}
}

void checkRepeats ( char sentence[], char text[])


{
int i=0, j, k, len, chkword , words=0, again=0;
char wrd[10];
len = strlen(sentence);

while(sentence[i]!='\0')
{

if(sentence[i]==' ')
{

23 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW

words++;
i++;

wrd[i] = sentence[i];

if(strcmp(wrd,text)==1)
{

again++;
}

else
printf("The search word does not exist in the sentence");
}

if (len==0)
{
printf("\n\nRESULTS:\n\nNumber of words in the text: %d",words);
printf("\n\nCharacters:%d \n",len);
}

else
{
printf("\n\nRESULTS:\n\nNumber of words: %d",words+1);
printf("\nCharacters:%d \n",len-1);
}

printf("The search word repeats %d times in the sentence\n", again);}

24 | P a g e

You might also like