Nayana
Nayana
PROGRAM – 1
#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");
}
}
OUTPUT:
Compilation : cc program1.c
Execution : ./a.out
PROGRAM - 2
#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");
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);
}
}
OUTPUT:
PROGRAM – 3
#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)
OUTPUT:
Compilation : cc program3.c
Execution : ./a.out
PROGRAM – 4
#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");
}
}
OUTPUT:
Compilation : cc program4.c
Execution : ./a.out
PROGRAM – 5
#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;
}
}
if (flag == 0)
{
printf("Element not found. Search is unsuccessful\n");
}
}
OUTPUT:
Compilation : cc program5.c
Execution : ./a.out
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");
}
}
}
OUTPUT:
Compilation : cc program6.c
Execution : ./a.out
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", °);
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));
}
OUTPUT:
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
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] );
}
OUTPUT:
Compilation : cc program8.c
Execution : ./a.out
15 6 25 10 20
6 10 15 20 25
PROGRAM – 9
Write functions to implement String operations such as comparison,
concatenation and finding string length. Use parameter passing
techniques.
#include < stdio.h >
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)
OUTPUT:
Compilation : cc program9.c
Execution : ./a.out
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);
}
}
}
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);
}
}
}
OUTPUT:
Compilation : cc program10.c
Execution : ./a.out
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);
OUTPUT:
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);
}
}
OUTPUT:
Compilation : cc program12.c
Execution : ./a.out