0% found this document useful (0 votes)
20 views

C Lab Programs

Uploaded by

rajitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C Lab Programs

Uploaded by

rajitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

C PROGRAMS ( LAB )

1. Write programs using arithmetic, logical, bitwise and ternary operators.


2. Write programs simple control statements :

(a)Roots of a Quadratic Equation,


(b) extracting integers,
(c) reversing number,
(d) finding sum of number ,
(e)printing multiplication tables,
(f) Armstrong numbers,
(g) checking for prime,
(h) magic number
3. sinx and cosx values using series expansion
4. Conversion of BinarytoDecimal,Octal,HexaandViceversa
5. Generating a Pascal triangle and Pyramidofnbers
6. Recursion:
(a) Factorial,
(b) Fibonacci,
(c) GCD
7. Finding the maximum, minimum, average and standard deviation of
given set of numbers using arrays
8. Reversing an array ,removal of duplicates from array
9. (a) Matrix addition,
(b) multiplication
(c) transpose of a square matrix using functions
10. Functions of string manipulation: inputting and outputting string , using
string functions suchasstrlen(),strcat( ),strcpy( )………etc
11. Writing simple programs for strings without using stringfunctions.
12. Finding the No.ofcharacters,words and lines of given text file
13. File handling programs:student memo printing
1. Write programs using arithmetic, logical, bitwise and ternary
operators.
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c,result;

c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

printf("a = %u, b = %u\n", a, b);


printf("a&b = %u\n", a & b);
printf("a|b = %u\n", a | b);
printf("a^b = %u\n", a ^ b);
printf("~a = %u\n", a = ~a);
printf("b<<1 = %u\n", b << 1);
printf("b>>1 = %u\n", b >> 1)
int x = 2, y = 5;
(x & y) ? printf("True ") : printf("False ");
(x && y) ? printf("True ") : printf("False ");

return 0;
}
Write programs simple control statements :
a)Roots of a Quadratic Equation,
b)extractingnumbers ofintegers,
c)reversing of a given number ,
d)finding sum of individual numbers ,
e)printing multiplication tables,
f)Armstrongnumber,
g)checking for prime,
h)magicnumberr,
2.
a) Roots of a Quadratic Equation
Ax2+bx+c=0

#include <math.h>
#include <stdio.h>
int main() {
int a, b, c, d, r1, r2;
printf("Enter coefficients a, b and c: ");
scanf("%d %d %d", &a, &b, &c);
d = b * b - 4 * a * c;
if (d > 0) {
r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
printf("r1 = %d and r2 = %d", r1, r2);
}
else if (d == 0) {
r1 = r2 = -b / (2 * a);
printf("r1 = r2 = %d;", r1);
}
else {
printf(“Roots are imaginary roots”);
}
return 0;
}
b)extracting numbers of integers
#include <stdio.h>
#include<conio.h>

int main()
{
int n ;
printf(“Enter a number”);
scanf(“%d”,&n);
while(n != 0)
{
int r = n % 10;
n = n / 10;
printf("%d\n", r);
}
return 0;
}
c) reversing of a given number
#include<stdio.h>
int main()
{
int n, rev=0, r;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("Reverse of a Number: %d",rev);
return 0;
}
d) finding sum of individual numbers
#include<stdio.h>
int main()
{
int n,sum=0,r;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
e)Multiplication table
#include<stdio.h>
int main()
{
int i,n;
printf(“enter n multiplication table :”):
scanf(“%d”,&n);
for(i=1;i<=10;i++)
printf(“%d * %d = %d. \n “,n,i,(n*i));
}
f)Armstrong Number
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153
371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1
So:
27+343+1=371
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the 3 digit number only");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}

g)prime or not
#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}

h) //Magic number
Suppose we have a 1729 number and we need to validate whether the
number is a magic number or not.
So, first we need to get the sum of all digits of the number which is
19 (1 + 7 + 2 + 9 = 19).
Reverse the sum of the number is 91,
and then get the product of the sum of original digits with the reverse of
that sum as
19 * 91 = 1729.
So, it is the magic number.

#include <stdio.h>
#include <conio.h>
int main ()
{
int n, temp, rev = 0, digit, sum= 0;
printf (" Enter a Number: \n");
scanf (" %d", &n); // get the number
temp = n; // assign the number to temp variable
while ( temp > 0)
{
sum= sum+ temp % 10;
temp = temp / 10;
}
temp = sum; // assign the sumto temp variable
printf (" \n The sum of the digits = %d", temp);

// get the reverse sum of given digits


while ( temp > 0)
{
rev = rev * 10 + temp % 10;
temp = temp / 10;
}
printf (" \n The reverse of the digits = %d", rev);
printf (" \n The product of %d * %d = %d", sum, rev, rev * sum);
// use if else statement to check the magic number
if ( rev * sum== n)
{
printf (" \n %d is a Magic Number. ", n);
}
else
{
printf (" \n %d is not a Magic Number. ", n);
}
return 0;

}
3. Sinx and Cosx values using series expansion
// sinx series
#include <stdio.h>
intmain(){
int i, j, n, fact, sign = - 1;
float x, p, sum = 0;
printf("Enter the value of x : ");
scanf("%f", &x);
printf("Enter the value of n : ");
scanf("%d", &n);
for (i = 1; i <= n; i += 2)
{
p = 1;
fact = 1;
for (j = 1; j <= i; j++)
{
p = p * x;
fact = fact * j;
}
sign = - 1 * sign;
sum += sign * p / fact;
}
printf("sin %0.2f = %f", x, sum);
return0;
}
Enter the value of x : 2
Enter the value of n : 3
sin 2.00 = 0.666667
// cos x series
#include <stdio.h>
intmain()
{
int i, j, n, fact, sign = - 1;
float x, p, sum = 0;
printf("Enter the value of x : ");
scanf("%f", &x);
printf("Enter the value of n : ");
scanf("%d", &n);
for (i = 2; i <= n; i += 2)
{
p = 1;
fact = 1;
for (j = 1; j <= i; j++)
{
p = p * x;
fact = fact * j;
}
sum += sign * p / fact;
sign = - 1 * sign;
}
printf("cos %0.2f = %f", x, 1+sum);
return0;
}
Enter the value of x : 4
Enter the value of n : 3
cos 4.00 = -7.000000

4. Conversion of Binary to Decimal,Octal,HexaandViceversa

//Convert binary to decimal


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

// function prototype
int convert(long long);

int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;

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

return dec;
}
Enter a binary number: 1101
1101 in binary = 13 in decimal

//Convert decimal to binary


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

long long convert(int);

int main() {
int n, bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}

long long convert(int n) {


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

return bin;
}
Enter a decimal number: 13
13 in decimal = 1101 in binary

//Binary to octal
#include <math.h>
#include <stdio.h>
int convert(long long bin);
int main() {
long long bin;
printf("Enter a binary number: ");
scanf("%lld", &bin);
printf("%lld in binary = %d in octal", bin, convert(bin));
return 0;
}

int convert(long long bin) {


int oct = 0, dec = 0, i = 0;

// converting binary to decimal


while (bin != 0) {
dec += (bin % 10) * pow(2, i);
++i;
bin /= 10;
}
i = 1;

// converting to decimal to octal


while (dec != 0) {
oct += (dec % 8) * i;
dec /= 8;
i *= 10;
}
return oct;
}
Enter a binary number: 101001
101001 in binary = 51 in octal
//octal to binary
#include <math.h>
#include <stdio.h>
long long convert(int oct);
int main() {
int oct;
printf("Enter an octal number: ");
scanf("%d", &oct);
printf("%d in octal = %lld in binary", oct, convert(oct));
return 0;
}

long long convert(int oct) {


int dec = 0, i = 0;
long long bin = 0;

// converting octal to decimal


while (oct != 0) {
dec += (oct % 10) * pow(8, i);
++i;
oct /= 10;
}
i = 1;

// converting decimal to binary


while (dec != 0) {
bin += (dec % 2) * i;
dec /= 2;
i *= 10;
}
return bin;
}
Enter an octal number: 67
67 in octal = 110111 in binary

//Binary to hexadecimal
#include<stdio.h>
int main(){
longint binaryval, hexadecimalval =0, i =1, remainder;
printf("Enter the binary number: ");
scanf("%ld",&binaryval);
while(binaryval !=0){
remainder = binaryval %10;
hexadecimalval = hexadecimalval + remainder * i;
i = i *2;
binaryval = binaryval /10;
}
printf("Equivalent hexadecimal value: %lX",
hexadecimalval);
return0;
}
Output: Enter the binary number:11100
Equivalent hexadecimal value:1C
5. a) pyramid of numbers
#include <stdio.h>
int main() {
int i, j, n, k = 0;
printf("Enter the number of n: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i, k = 0) {
for (j = 1; j <= n - i; ++j) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("%d ",i);
++k;
}
printf("\n");
}
return 0;
}
b) pascal triangle
#include <stdio.h>
void main()
{
int n,c=1,b,i,j;
printf("Input number of rows: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(b=1;b<=n-i;b++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("% 4d",c);
}
printf("\n");
}
}
6. a) factorial using recursion
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
b)gcd using recursion
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
c) Fibonacci Series using Recursion
#include <stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}

int main()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}
7. Finding the maximum, minimum, average and standard
deviation of given set of numbers using arrays

#include <stdio.h>
#include<math.h>
int main()
{
int a[1000],i,n,min,max,sum=0;
float avg=0.0,sd=0.0;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
min=max=a[0];
for(i=0; i<n; i++)
{
sum+=a[i];
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
for(i=0; i<n; i++)
{
sum=sum+a[i];
avg=sum/n;
sd += pow(a[i] - avg, 2);
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
printf("\naverage of array is : %f",avg);
printf("\nsd of array is : %f",sqrt(sd/(float)n));
return 0;
}

8. Reversing an array ,removal of duplicates from the array


#include<stdio.h>
#include<stdlib.h>
int main(){
int a[50],i,j,k,dup[50],n;
printf("Enter size of the array\n");
scanf("%d",&n);
printf("Enter Elements of the array:\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: \n");
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
printf("Array in reverse order: \n");
for (i = n-1; i >= 0; i--) {
printf("%d ",a[i]);
}
for(i=0;i<n;i++){
for(j = i+1; j < n; j++){
if(a[i] == a[j]){
for(k = j; k <n; k++){
a[k] = a[k+1];
}
j--;
n--;
}
}
}
printf("\nAfter deleting the duplicate element the Array is:\n");
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
return 0;
}

9. a) Matrix addition
#include<stdio.h>
int main()
{
int a[2][2], b[2][2], i, j, c[2][2];
printf("Enter 2*2 matrix 1 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&a[i][j]);
}
printf("Enter 2*2 matrix 2 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&b[i][j]);
}
printf("\nAdding the two matrix.....");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("\nBoth matrices added successfully!");
printf("\nHere is the new matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
b)matrix multiplication
#include<stdio.h>
int main()
{
int a[2][2], b[2][2], i, j, c[2][2];
printf("Enter 2*2 matrix 1 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&a[i][j]);
}
printf("Enter 2*2 matrix 2 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&b[i][j]);
}
printf("\nAdding the two matrix.....");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]+b[k][j];
}
}
}
printf("\nHere is the multiplication matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
c) transpose of a matrix
#include <stdio.h>
int main() {
int i, j;
int a[3][3], t[3][3];

printf("Enter matrix a\n");

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


for (j = 0; j < 3; j++) {
arintf("Enter the elements of matrix a [%d,%d]: ", i, j);
scanf("%d", & a[i][j]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
t[i][j] = a[j][i];
}
}
printf("Transaose of matrix a is:\n\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
arintf("%d ", t[i][j]);
}
arintf("\n");
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
t[i][j] = a[j][i];
}
}

}
10.string functions
#include <stdio.h>
#include <string.h>
int main() {
char string1[20] = "Annie Besant Women’s College",string2;
printf("Length of string1: %d \n", strlen(string1));
printf("String copy %s", strcpy(string2,string1));
printf("String Lower %s", strlwr(string2,string1));
printf("String upper%s", strupr(string2,string1));
printf("String rev %s", strrev(string2));
printf("String concatenation %s", strcat(string2,string1));
printf("String compare %s", strcmp(string2,string1);
}
11. strings without using string functions
#include <stdio.h>
int main()
{
char str1[100],str2[100];
int i;
printf("Enter the first string:\n");
scanf("%s", &str1);
printf("Enter the second string:\n");
scanf("%s", &str2);
for(i = 0; str1[i] != '\0'; ++i);
printf("Length of first string: %d", i);
for(i = 0; str2[i] != '\0'; ++i);
printf("Length of second string: %d", i);
for(i=0;str1[i]!='\0' || str2[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
printf("Given strings are not identical\n");
return;
}
}
printf("Given strings are identical\n");
return 0;
}
12. FindingtheNo.ofcharacters,wordsandlinesofgiventextfile
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * file;
char path[100];
char ch;
int characters, words, lines;
file=fopen("counting.txt","w");
printf("enter the text.press cntrl Z:");
while((ch = getchar())!=EOF){
putc(ch,file);
}
fclose(file);
printf("Enter source file path: ");
scanf("%s", path);
file = fopen(path, "r");
if (file == NULL){
printf("Unable to open file.");
exit(EXIT_FAILURE);
}
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF){
characters++;
if (ch == '\n' || ch == '\0')
lines++;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if (characters > 0){
words++;
lines++;
}
printf("Total characters = %d \n", characters);
printf("Total words = %d \n", words);
printf("Total lines = %d \n", lines);
fclose(file);
return 0;
}

13. File handling programs of student memo


#include <stdio.h>
#include<stdlib.h>
int main() {
char name[50];
int marks,i,n;
printf("Enter number of students: ");
scanf("%d",&n);
FILE *fptr;
fptr=(fopen("student.txt","w"));
if(fptr==NULL) {
printf("Error!");
exit(1);
}
for (i=0;i<n;++i) {
printf("For student %d \nEnter name: ",i+1);
scanf("%s",name);
printf("Enter marks: ");
scanf("%d",&marks);
fprintf(fptr,"\nName: %s \nMarks=%d \n",name,marks);
}

fclose(fptr);
return 0;
}

You might also like