You are on page 1of 15

Programming in c I B.S.

C Language Lab manual

1)write a c program to calculate simple and compound interest

#include<stdio.h>
void main()
{
int p,r,t;

float i;
printf("Enter the Principal, Rate and Time\n");
scanf("%d %d %d",&p,&r,&t);
i=p*r*t/100;
printf("simple interest is : %f",i);

2)write a c program to interchange two numbers

#include<stdio.h>
void main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
}

1
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

3)find the biggest of three numbers in c

#include <stdio.h>
void main()
{
int A, B, C;

printf("Enter the numbers A, B and C: ");


scanf("%d %d %d", &A, &B, &C);

if (A >= B && A >= C)


printf("%d is the largest number.", A);

else if (B >= A && B >= C)


printf("%d is the largest number.", B);

else
printf("%d is the largest number.", C);
}

4)write a c program to find sum of individual digits

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

2
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

5)write a c program to find Fibonacci series starting with 0 and 1

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

6) write a c program to check whether a number is Armstrong or not

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

int main()
{
int originalNum, num, lastDigit, digits, sum;
printf("Enter any number to check Armstrong number: ");
scanf("%d", &num);

sum = 0;

originalNum = num;

3
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

digits = (int) log10(num) + 1;

while(num > 0)
{
lastDigit = num % 10;

sum = sum + round(pow(lastDigit, digits));

num = num / 10;


}

if(originalNum == sum)
{
printf("%d is ARMSTRONG NUMBER", originalNum);
}
else
{
printf("%d is NOT ARMSTRONG NUMBER", originalNum);
}

return 0;
}

7)wirte a c program to generate all the prime numbers between 1 and n

#include<stdio.h>

int main(){

int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);

for(num = 1;num<=n;num++){

count = 0;

for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}

4
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

if(count==0 && num!= 1)


printf("%d ",num);
}

return 0;
}

8) write c program to implement searching of given item In given list

#include <stdio.h>

int main()

int array[100], search, c, number;

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

scanf("%d",&number);

printf("Enter %d numbers\n", number);

for ( c = 0 ; c < number ; c++ )

scanf("%d",&array[c]);

printf("Enter the number to search\n");

scanf("%d",&search);

for ( c = 0 ; c < number ; c++ )

if ( array[c] == search {

printf("%d is present at location %d.\n", search, c+1);

break;

if ( c == number )

5
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

printf("%d is not present in array.\n", search);

return 0;

9) write a c program that uses functions to perform the addition of matrices

#include <stdio.h>

#include <conio.h>

void main()

int a[3][3], b[3][3], c[3][3], i, j;

clrscr();

printf("Enter the elements of 3*3 matrix a \n");

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

for(j = 0; j < 3; j++)

scanf("%d", &a[i][j]);

printf("Enter the elements of 3*3 matrix b \n");

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

6
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

for(j = 0; j < 3; j++)

scanf("%d", &b[i][j]);

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

for(j = 0; j < 3; j++)

c[i][j] = a[i][j] + b[i][j];

printf("The resultant 3*3 matrix c is \n");

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

for(j = 0; j < 3; j++)

printf("%d\t", c[i][j]);

printf("\n");

7
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

getch();

10) write a c program that uses functions to perform the multipliction of matrices

#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
clrscr();
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{

8
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

11)write a c program for concatenation of two strings

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[50], str2[50];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
strcat(str1, str2);
printf("\nString after concatenation is:\n%s", str1);
getch();
return 0;
}

12)write a c program for length of string with and without string handling functions

9
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

#include <stdio.h>
int main()
{

char str[100];
int i,length=0;

printf("Enter a string: \n");


scanf("%s",str);

.
for(i=0; str[i]!='\0'; i++)
{
length++;
}

printf("\nLength of input string: %d",length);

return 0;
}

13) write a c program to demonstrate call by value and call by reference mechanism
a) call by value:

#include <stdio.h>
void swap(int , int);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}

10
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

b)call by reference

#include <stdio.h>
void swap(int *, int *);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}

14) write a c program to find GCD of two numbers using recursion

#include<stdio.h>

int findGCD(int num1,int num2);


int main()
{
int num1,num2,gcd;
printf("\n\n Recursion : Find GCD of two numbers :\n");
printf("------------------------------------------\n");
Printf(" Input 1st number: ");
scanf("%d",&num1);
printf(" Input 2nd number: ");
scanf("%d",&num2);

gcd = findGCD(num1,num2);
printf("\n The GCD of %d and %d is: %d\n\n",num1,num2,gcd);
return 0;

11
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

int findGCD(int a,int b)


{
while(a!=b)
{
if(a>b)
return findGCD(a-b,b);
else
return findGCD(a,b-a);
}
return a;
}

15) write a c program to perform various operations using pointers

#include <stdio.h>

int main()

int a = 22;

int *p = &a;

printf("p = %u\n", p);

p++;

printf("p++ = %u\n", p);

p--;

printf("p-- = %u\n", p);

float b = 22.22;

12
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

float *q = &b;

printf("q = %u\n", q);

q++;

printf("q++ = %u\n", q);

q--;

printf("q-- = %u\n", q);

char c = 'a';

char *r = &c;

printf("r = %u\n", r);

r++;

printf("r++ = %u\n", r);

r--;

printf("r-- = %u\n", r);

return 0;

16) write a c program to demonstrate dynamic arrays using dynamic memory management
functions

13
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

#include <stdio.h>

#include <stdlib.h>

int main()

Int *ptr;

int size;

printf("Enter size of elements:");

scanf("%d", &size);

ptr = (int*)malloc(size * sizeof(int));

if (ptr == NULL) {

printf("Memory not allocated.\n");

else {

printf("Memory successfully allocated using malloc.\n");

for (int j = 0; j < size; ++j) {

ptr[j] = j + 1;

printf("The elements of the array are: ");

14
Department of Computer Science A.S.N Degree College, Tenali
Programming in c I B.S.C

for (int k = 0; k < size; ++k) {

printf("%d, ", ptr[k]);

return 0;

15
Department of Computer Science A.S.N Degree College, Tenali

You might also like