0% found this document useful (0 votes)
26 views36 pages

Nayana

The document discusses 5 programs written in C programming language. Program 1 simulates a simple calculator. Program 2 computes the roots of a quadratic equation. Program 3 calculates electricity bill based on units consumed. Program 4 displays a pattern of numbers. Program 5 implements binary search on integers.

Uploaded by

Shubas Sr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views36 pages

Nayana

The document discusses 5 programs written in C programming language. Program 1 simulates a simple calculator. Program 2 computes the roots of a quadratic equation. Program 3 calculates electricity bill based on units consumed. Program 4 displays a pattern of numbers. Program 5 implements binary search on integers.

Uploaded by

Shubas Sr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Principles of Programming Using C (BPOPS103)

PROGRAM – 1

Simulation of a Simple Calculator.

#include<stdio.h>
void main( )
{
int a, b, sum, dif, pro, quo, rem;
char operator;
printf("Enter an arithmetic expression\n");
scanf("%d%c%d", &a, &operator, &b); ‘
switch(operator)
{
case ‘+’: sum = a+b;
printf("The sum is=%d\n", sum);
break;
case ‘-’ : dif = a-b;
printf("The difference is=%d\n", dif);
break;
case ‘*’: pro = a*b;
printf("The product is=%d\n", pro);
break;
case ‘/ ’: if(b!=0)
{
quo = a/b;
printf("The quotient is=%d\n", quo);
}
else
{
printf("Divide by zero error\n");
}
break;
case ‘%’: rem = a%b;
printf("The remainder is=%d\n", rem);
break;
default : printf("Invalid expression\n");
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 1


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program1.c
Execution : ./a.out

Enter an arithmetic expression


2+3
The sum is=5

Enter an arithmetic expression


10-5
The difference is=5

Enter an arithmetic expression


3*4
The product is=12

Enter an arithmetic expression


6/3
The quotient is=2

Enter an arithmetic expression


7/0
Divide by zero error

Enter an arithmetic expression


10%3
The remainder is=1

Enter an arithmetic expression


7$8
Invalid expression

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 2


Principles of Programming Using C (BPOPS103)

PROGRAM - 2

Compute the roots of a quadratic equation by accepting the


coefficients. Print appropriate messages.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main( )
{
int a, b, c, d;
float r1, r2;
printf("Enter the coefficients\n");
scanf("%d %d %d", &a, &b, &c);
d = b*b - 4*a*c;
if (a*b*c == 0 )
{
printf("Invalid input\n");
exit( 0 );
}
if(d == 0)
{
printf("Roots are real and equal\n");
r1=r2=-b/(2*a);
printf("Root1=%f\n", r1);
printf("Root2=%f\n", r2);
}
if ( d > 0 )
{
printf("Roots are real and distinct\n");
r1= (-b + sqrt(d)) / (2*a);
r2= (-b – sqrt(d)) / (2*a);
printf("Root1=%f\n", r1);
printf("Root2=%f\n", r2);
}
if ( d < 0 )
{
printf("Roots are imaginary\n");

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 3


Principles of Programming Using C (BPOPS103)

r1= -b/(2*a);
r2= sqrt(-d)/(2*a);
printf("Root1 = %f + i %f\n", r1, r2);
printf("Root2 = %f – i %f\n", r1, r2);
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 4


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program2.c -lm


Execution : ./a.out

Enter the coefficients


101
Invalid input

Enter the coefficients


121
Roots are real and equal
Root1= -1.000000
Root2= -1.000000

Enter the coefficients


141
Roots are real and distinct
Root1= -0.267949
Root2= -3.732051

Enter the coefficients


111
Roots are imaginary
Root1= -0.500000+i0.866025
Root2= -0.500000-i0.866025

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 5


Principles of Programming Using C (BPOPS103)

PROGRAM – 3

An electricity board charges the following rates for the use of


electricity:
for the first 200 units 80 paise per unit.
for the next 100 units 90 paise per unit.
beyond 300 units Rs 1 per unit.
All users are charged a minimum of Rs. 100 as meter charge.
If the total amount is more than Rs 400, then an additional
surcharge of 15% of total amount is charged. Write a program to
read the name of the user, number of units consumed and print out
the charges.

#include<stdio.h>
void main( )
{
char name[20];
float units, charge;
printf("Enter customer name\n");
scanf("%s", name);
printf("Enter the number of units consumed\n");
scanf("%f ", &units);
if ( units <= 200 )
{
charge = units*0.8+100 ;
}
if ( units > 200 && units <= 300)
{
charge = 260 + ( units – 200 ) * 0.9 ;
}
if ( units > 300)
{
charge = 350 + ( units – 300 ) * 1;
}
if ( charge > 400 )
{
Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 6
Principles of Programming Using C (BPOPS103)

charge = charge + charge * 0.15 ;


}
printf("Total charge = %f\n", charge);
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 7


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program3.c
Execution : ./a.out

Enter customer name


abc
Enter the number of units consumed
200
Total charge = 260

Enter customer name


pqr
Enter the number of units consumed
300
Total charge = 350

Enter customer name


xyz
Enter the number of units consumed
360
Total charge = 471.5

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 8


Principles of Programming Using C (BPOPS103)

PROGRAM – 4

Write a C Program to display the following as output by reading the


number of rows as input.
1
121
12321
1234321
---------------------
---------------------------
‘n’th row

#include<stdio.h>
void main( )
{
int i, j, n;
printf("Enter the number of rows\n");
scanf("%d", &n);
for (i = 0; i <= n; i++)
{
for (j = 1; j <= n-i ; j++)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("%d", j);
}
for (j = i - 1; j >= 1; j--)
{
printf("%d", j);
}
printf("\n");
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 9


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program4.c
Execution : ./a.out

Enter the number of rows


5
1
121
12321
1234321
123454321

Enter the number of rows


10
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 10


Principles of Programming Using C (BPOPS103)

PROGRAM – 5

Implement Binary Search on Integers.

#include<stdio.h>
void main( )
{
int i, n, low, high, mid, flag, key, a[100];
printf("Enter the size of an array\n");
scanf("%d", &n);
printf("Enter the elements of an array in ascending order\n");
for (i = 0; i < n; i++)
{
scanf( "%d", &a[i]);
}
printf("Enter the key element to be searched\n");
scanf( "%d", &key );
flag = 0;
low = 0;
high = n-1;
while (low <= high)
{
mid = (low + high) / 2;
if (key == a[mid])
{
flag = 1;
printf("The key element is found at position %d\n", mid+1);
break;
}
if (key > a[mid])
{
low = mid+1;
}
if (key < a[mid])
{
high = mid-1;
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 11


Principles of Programming Using C (BPOPS103)

if (flag == 0)
{
printf("Element not found. Search is unsuccessful\n");
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 12


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program5.c
Execution : ./a.out

Enter the size of an array


10
Enter the elements of an array in ascending order
5 7 10 13 15 17 20 22 25 27 30
Enter the key element to be searched
15
The key element is found at position 5

Enter the size of an array


10
Enter the elements of an array in ascending order
5 7 10 13 15 17 20 22 25 27 30
Enter the key element to be searched
35
Element not found. Search is unsuccessful

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 13


Principles of Programming Using C (BPOPS103)

PROGRAM – 6
Implement Matrix Multiplication and validate the rules of
multiplication.
#include<stdio.h>
void main( )
{
int a[10][10], b[10][10], c[10][10];
int m, n, p, q, i, j, k;
printf("Enter the order of matrix A\n");
scanf("%d%d", &m, &n);
printf("Enter the order of matrix B \n");
scanf("%d%d", &p, &q);
if (n != p)
{
printf("Matrix multiplication not possible\n");
}
else
{
printf("Enter the elements of matrix A\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", &a[i][j] );
}
}
printf("Enter the elements of matrix B\n");
for (i=0; i<p; i++)
{
for (j=0; j<q; j++)
{
scanf("%d", &b[i][j] );
}
}
for (i=0; i<m; i++)
{
for (j=0; j<q; j++)
{
Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 14
Principles of Programming Using C (BPOPS103)

c[i][j] = 0;
for (k=0; k<n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
printf(“Matrix A is\n");
for (i=0; i<m; i++)
{
for (j=0 ;j<n; j++)
{
printf("%d\t", a[i][j] );
}
printf("\n");
}
printf("Matrix B is\n");
for (i=0; i<p; i++)
{
for (j=0; j<q; j++)
{
printf("%d\t", b[i][j] );
}
printf("\n");
}
printf("The product of matrices A and B is\n");
for (i=0; i<m; i++)
{
for (j=0; j<q; j++)
{
printf("%d\t", c[i][j] );
}
printf("\n");
}
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 15


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program6.c
Execution : ./a.out

Enter the order of matrix A


2 2
Enter the order of matrix B
3 3
Matrix multiplication not possible

Enter the order of matrix A


2 2
Enter the order of matrix B
2 2
Enter the elements of matrix A
1 2 3 4
Enter the elements of matrix B
1 2 3 4
Matrix A is
1 2
3 4
Matrix B is
1 2
3 4
The product of matrices A and B is
7 10
15 22

Enter the order of matrix A


2 3
Enter the order of matrix B
3 2

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 16


Principles of Programming Using C (BPOPS103)

Enter the elements of matrix A


1 2 3 4 5 6
Enter the elements of matrix B
1 2 3 4 5 6
Matrix A is
1 2 3
4 5 6
Matrix B is
1 2
3 4
5 6
The product of matrices A and B is
22 28
49 64

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 17


Principles of Programming Using C (BPOPS103)

PROGRAM – 7
Compute sin(x)/cos(x) using Taylor Series approximation. Compute
your result with the built-in function. Print both the results with
appropriate inferences.
#include<stdio.h>
#include<math.h>
void main( )
{
int i, n, deg;
float rad, term, sum;
printf("Enter degree\n");
scanf("%d", &deg);
printf("Enter the number of terms\n");
scanf("%d", &n);
rad = deg*(3.14/180);
term = sum = rad;
for (i=1; i<n; i++)
{
term = term* -rad*rad / (2*i*(2*i+1));
sum = sum + term;
}
printf("Using Taylor's series the sum is =%f\n", sum);
printf("Using in-built function the sum is =%f\n", sin(rad));
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 18


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program7.c -lm


Execution : ./a.out

Enter degree
90
Enter the number of terms
10
Using Taylor's series the sum is = 1.000000
Using in-built function the sum is = 1.000000

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 19


Principles of Programming Using C (BPOPS103)

PROGRAM – 8
Sort the given set of N numbers using Bubble Sort.
#include<stdio.h>
void main( )
{
int n, i, j, temp, a[10];
printf(" Enter the size of an array\n");
scanf("%d", &n );
printf("Enter the elements of the array\n");
for (i=0; i<n; i++)
{
scanf("%d", &a[i] );
}
for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
if( a[j] > a[j+1] )
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Sorted elements are\n");
for (i=0; i<n; i++)
{
printf("%d\n", a[i] );
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 20


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program8.c
Execution : ./a.out

Enter the size of an array


5
Enter the elements of the array

15 6 25 10 20

Sorted elements are

6 10 15 20 25

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 21


Principles of Programming Using C (BPOPS103)

PROGRAM – 9
Write functions to implement String operations such as comparison,
concatenation and finding string length. Use parameter passing
techniques.
#include < stdio.h >

int string_length (char str[ ])


{
int len=0;
while (str[len] != ’\0’)
{
len++;
}
return len;
}

void string_concat (char str1[ ], char str2[ ])


{
int i , j ;
char str3[30];
for (i=0; i < string_length(str1); i++)
{
str3[i] = str1[i];
}
for (j=0; j < string_length(str2); j++)
{
i++;
str3[i] = str2[j];
}
str3[i] = ’\0’;
printf("The concatenated string = %s\n", str3);
}

void string_compare (char str1[ ], char str2[ ])


{
int len1, len2;

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 22


Principles of Programming Using C (BPOPS103)

len1 = string_length(str1);
len2 = string_length(str2);
if( len1 != len2 )
{
printf("The strings are not equal\n" );
}
else
{
for (i=0; str1[i] != ’\0’; i++)
{
if (str1[i] != str2[i])
{
printf("The strings are not equal\n");
exit(0);
}
}
printf("The strings are equal\n");
}
}

void main ( )
{
char str[10], str1[10], str2[10];
int choice, length ;
printf("Enter your choice \n") ;
printf("Enter 1 for String Length\n");
printf("Enter 2 for String Concatenation\n");
printf("Enter 3 for String Comparison\n");
scanf("%d", &choice );
switch (choice)
{
case 1: printf("Enter a string\n");
scanf("%s", str);
length = string_length(str);
printf("The length of the string = %d\n" , length );
break;
case 2: printf("Enter first string\n");
scanf("%s", str1);
printf("Enter second string\n");
scanf("%s", str2);
Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 23
Principles of Programming Using C (BPOPS103)

string_concat (str1, str2);


break;
case 3: printf("Enter first string\n");
scanf("%s", str1);
printf("Enter second string\n");
scanf("%s", str2);
string_compare (str1, str2);
break;
default : printf("Invalid choice\n");
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 24


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program9.c
Execution : ./a.out

Enter your choice


Enter 1 for String Length
Enter 2 for String Concatenation
Enter 3 for String Comparison
1
Enter a string
RIT HASSAN
The length of the string = 10

Enter your choice


Enter 1 for String Length
Enter 2 for String Concatenation
Enter 3 for String Comparison
2
Enter first string
RIT,
Enter second string
HASSAN
The concatenated string = RIT,HASSAN

Enter your choice


Enter 1 for String Length
Enter 2 for String Concatenation
Enter 3 for String Comparison
3
Enter first string
RIT
Enter second string
HASSAN
The strings are not equal

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 25


Principles of Programming Using C (BPOPS103)

Enter your choice


Enter 1 for String Length
Enter 2 for String Concatenation
Enter 3 for String Comparison
3
Enter first string
RIT
Enter second string
CSE
The strings are not equal

Enter your choice


Enter 1 for String Length
Enter 2 for String Concatenation
Enter 3 for String Comparison
3
Enter first string
CSE
Enter second string
CSE
The strings are equal

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 26


Principles of Programming Using C (BPOPS103)

PROGRAM – 10
Implement structures to read, write and compute average marks for
class of N students. List the students scoring above and below
average marks.
#include<stdio.h>

struct student
{
int marks;
char name[20],;
};

void main( )
{
struct student s[10];
int i, n;
float sum, avg;
printf("Enter the number of students\n");
scanf("%d", &n);
printf("Enter %d students details:\n", n);
for (i=0; i<n; i++)
{
printf("Enter student name\n");
scanf("%s", s[i].name);
printf("Enter marks \n");
scanf("%d", &s[i].marks);
sum = sum + s[i].marks;
}
avg = sum / n ;
printf("Average marks = %f\n ", avg);
printf("All %d student details:\n", n );
for (i = 0 ; i < n ; i++)
{
printf("%s\t", s[i].name);
printf("%d\n" , s[i].marks);
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 27


Principles of Programming Using C (BPOPS103)

printf("Students scoring above average marks:\n" );


for ( i = 0 ; i < n ; i++)
{
if (s[i].marks >= avg)
{
printf("%s\t", s[i].name);
printf("%d\n", s[i].marks);

}
}
printf("Students scoring below average marks:\n");
for (i = 0 ; i < n ; i++)
{
if (s[i].marks < avg)
{
printf("%s\t", s[i].name);
printf("%d\n", s[i].marks);
}
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 28


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program10.c
Execution : ./a.out

Enter the number of students


5
Enter 5 student details

Enter student name


abc
Enter marks
10
Enter student name
ijk
Enter marks
20
Enter student name
mno
Enter marks
30
Enter student name
pqr
Enter marks
40
Enter student name
xyz
Enter marks
50
Average marks = 30
Students scoring above average marks:
mno 30
pqr 40
xyz 50

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 29


Principles of Programming Using C (BPOPS103)

Students scoring below average marks:


abc 10
ijk 20

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 30


Principles of Programming Using C (BPOPS103)

PROGRAM – 11
Develop a program using pointers to compute the sum, mean and
standard deviation of all the elements stored in an array of N real
numbers.
#include<stdio.h>
#include<math.h>
void main( )
{
int i, n;
float a[10], *ptr, mean, stddev, variance=0, sum=0;
printf("Enter the size of the array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (i=0; i<n; i++)
{
scanf("%f", &a[i]);
}
ptr=a;
for (i=0; i<n; i++)
{
sum=sum + *ptr;
ptr++;
}
mean = sum / n;
ptr = a;
for (i=0; i<n; i++)
{
variance = variance + pow(( *ptr - mean ) , 2 );
ptr++;
}
stddev = sqrt(variance / n);
printf("Sum=%f\n", sum);
printf("Mean=%f\n", mean);

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 31


Principles of Programming Using C (BPOPS103)

printf("Standard deviation=%f\n", stddev);


}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 32


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc progra11.c -lm


Execution : ./a.out

Enter the size of the array


5
Enter the array elements
1 2 3 4 5
Sum= 15
Mean= 3
Standard deviation= 1.414214

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 33


Principles of Programming Using C (BPOPS103)

PROGRAM – 12
Write a C program to copy a text file to another file. Read both the
input file name and the target file name.
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *ptr1, *ptr2;
char ch, input[20], target[20];
printf("Enter the input file name\n");
scanf("%s", input);
ptr1=fopen(input, "r");
if(ptr1==NULL)
{
printf("File not found\n");
exit(0);
}
printf("Enter the target file name\n");
scanf("%s", target);
ptr2=fopen(target, "w");
if(ptr2==NULL)
{
printf("File not found\n");
exit(0);
}
while(1)
{
ch=fgetc(ptr1);
if(ch == EOF)
{
break;
}
else
{
fputc(ch, ptr2);
}
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 34


Principles of Programming Using C (BPOPS103)

printf("The file copied successfully\n");


fclose(ptr1);
fclose(ptr2);
}

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 35


Principles of Programming Using C (BPOPS103)

OUTPUT:

Compilation : cc program12.c
Execution : ./a.out

Enter the input file name


input.txt
Enter the target file name
target.txt
The file copied successfully

Enter the input file name


results.docx
File not found

Enter the input file name


input.txt
Enter the target file name
analysis.docx
File not found

Nayanashree D, Assistant Professor, Dept. of CSE, RIT Page 36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy