[go: up one dir, main page]

0% found this document useful (0 votes)
4 views27 pages

III B.com-C Programming Lab Record

The document provides a comprehensive index of C programming examples, including programs for calculating averages, finding the largest number, performing arithmetic operations, and implementing various control structures like loops and conditionals. It also covers array manipulations, matrix operations, recursion, and string handling. Each program includes source code, input, and output examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

III B.com-C Programming Lab Record

The document provides a comprehensive index of C programming examples, including programs for calculating averages, finding the largest number, performing arithmetic operations, and implementing various control structures like loops and conditionals. It also covers array manipulations, matrix operations, recursion, and string handling. Each program includes source code, input, and output examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Programming in C

Index
1. Program for average of three numbers

2. Program on if-else - find biggest of three numbers

3. Program for arithmetic operators.

4. Program on else if ladder – calculating total, average, grade of the marks

5. Program on switch case-manipulation of arithmetic operations

6. Program on while loop - print 1 to 10 numbers.

7. Program on do-while loop- print 10 to 1 numbers .

8. Program on for loop-print 1 to 10 numbers .

9. Program if-else statement- find out given number is even or odd.

10.Program on while loop - print reverse of a given number.

11.Program on while loop - check the given number is armstrong or not

12.Program on while loop - print fibonacci series.

13.Program on one dimensional arrays-print the series of elements .

14.Program on two dimensional array- addition of two matrices.

15.Program on two dimensional array- subtraction of two matrices.

16. Program on two dimensional array- multiplication of two matrices.

17.Program on call by value – swapping of two elements

18.Program on call by reference – swapping of two elements

19.Program on recursion-factorial of a given number

20. Program on String Operations


1
1. Aim: Program for average of three numbers
Source code:
#include<stdio.h>
main()
{
int a,b,c;
printf(“enter a,b,c values”);
scanf(“%d%d%d”,&a,&b,&c);
avg=(a+b+c)/3;
printf(“the average = %d”,avg);
}
Input:
enter a,b,c values
20
30
40
Output:
average = 30

2
2. Aim: Program on if-else - find biggest of three numbers
Source code:
#include<stdio.h>
main()
{
int a,b,c;
printf(“enter a,b,c values”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“a is big number”);
else
if(b>c)
printf(“b is big number”);
else
printf(“c is big number”);
}
Input:
enter a,b,c values
10
20
30
Output:
c is big number

3
3. Aim: Program for arithmetic operators.
Source code:
#include<stdio.h>
main()
{
int a,b;
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
printf(“\n addition is :%d”,a+b);
printf(“\n subtraction is:%d”,a-b);
printf(“\n multiplication is:%d”,a*b);
printf(“\n division is:%f”,a/b);
printf(“\n modulo division is : %d”,a%b);
}
Input:
enter a,b values
8
4
Output:
addition is :12
subtraction is: 4
multiplication is: 32
division is:2
modulo division is : 0

4
4. Aim: Program on else if ladder – calculating total, average, grade of
the student marks
Source code:
#include<stdio.h>
main()
{
int m1,m2,m3,tot,avg;
char grade;
printf(“enter marks in 3 subjects”);
scanf(“%d%d%d”,&m1,&m2,&m3);
tot=m1+m2+m3;
avg=tot/3;
if(avg>=60)
grade=’a’;
else if(avg>=50)
grade=’b’;
else if(avg>=40)
grade=’c’;
else
grade=’f’;
printf(“grade is%c”,grade);
}
Input:
enter marks in 3 subjects
50
60
70
Output:
grade is a

5
5. Aim: Program on switch case-manipulation of arithmetic operations
Source code:
#include<stdio.h>
main()
{
int a,b,c,ch;
print f(“enter a,b values”);
scanf(“%d%d”,&a,&b);
printf(“enter your choice:)”;
scanf(“%d”,&ch);
switch(ch)
{
case 1: c=a+b;
printf(“addition :%d”, c);
break;
case 2: c=a-b;
printf(“subtraction :%d”, c);
break;
case 3: c=a*b;
printf(“multiplication:%d”, c);
break;
case 4: c=a/b;
printf(“division :%d”, c);
break;
default:
print f(“out of choice”);
break;
}
}
6
Input:
enter a,b values
6
3
enter your choice:
3
Output:
multiplication : 18

7
6. Aim: Program on while loop - print 1 to 10 numbers.
Source code:
#include<stdio.h>
main()
{
int n;
n=1;
printf(“the numbers from 1 to 10:“);
while(n<=10)
{
print f(“%d\n”,n);
n++;
}
}
Output:
the numbers from 1 to 10:
1
2
3
4
5
6
7
8
9
10

8
7. Aim: Program on do-while loop- print 10 to 1 numbers .
Source code:
#include<stdio.h>
main()
{
int n;
n=10;
printf(“the numbers from 10 to 1:”);
do
{
print f(“%d\n”,n);
n--;
}while (n>0);
}
Output:
the numbers from 10 to 1:
10
9
8
7
6
5
4
3
2
1

9
8. Aim: Program on for loop-print 1 to 10 numbers .
Source code:
#include<stdio.h>
main()
{
int n;
printf(“the numbers from 1 to 10:“);
for(n=1;n<=10;n++)
print f(“%d\n”,n);
}
Output:
the numbers from 1 to 10:
1
2
3
4
5
6
7
8
9
10

10
9. Aim: Program if-else statement- find out given number is even or
odd.
Source code:
#include<stdio.h>
main()
{
int a;
printf(“enter any number:”);
scanf(“%d”,&a);
if(a%2==0)
printf(“the given number is even”);
else
printf(“the given number is odd”);
}
Input:
enter any number:
7
Output:
the given number is odd

11
10. Aim: Program on while loop - print reverse of a given number.
Source code:
#include<stdio.h>
main()
{
int n,rem,rev=0;
printf(“enter any number”);
scanf(“%d”,&n);
while(n>o0)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
printf(“reverse of the given number :%d”,rev);
}
Input:
enter any number
342
Output:
reverse of the given number : 243

12
11. Aim: Program on while loop - check the given number is armstrong
or not
source code:
#include<stdio.h>
main()
{
int n,m ,s,arm,sum=0;
printf(“enter n value”);
scanf(“%d”,&n);
m=n;
while(i>0)
{
s=n%10;
sum=sum+(s*s*s);
n=n/10;
}
if(sum==m)
printf(“the given number is armstrong”);
else
printf(“the given number is not arm strong”);
}
Input:
enter n value
153
Output:
the given number is armstrong

13
12. Aim: Program on while loop - print fibonacci series.
source code:
#include<stdio.h>
main()
{
int a=0,b=1,c,n;
printf(“enter n value”);
scanf(“%d”,&n);
printf(“the fibonacci series :\n”);
printf(“%d\t%d\t”,a,b);
n=n-2;
while(n!=0)
{
c=a+b;
a=b;
b=c;
printf(“%d\t”,c);
n--;
}
}
Input:
enter n value
7
Output:
the fibonacci series:
0 1 1 2 3 5 8

14
13. Aim: Program on one dimensional arrays-print the series of
elements .
source code:
#include<stdio.h>
main()
{
int a[10],;
printf(“enter 10 elements”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
printf(“the elements are:\n”);
for(i=0;i<10;i++)
pritnf(“%d\t”,a[i]);
}
Input:
enter 10 elements
3
5
6
7
8
2
3
4
1
0
Output:
the elements are:
3 5 6 7 8 2 3 4 1 0

15
14. Aim: Program on two dimensional array- addition of two matrices.
Source code:
#include<stdio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf(“enter elements for a:”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&a[i][j]);
printf(“enter elements for b:”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&b[i][j]);
printf(“addition is:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}

16
Input:
enter elements for a
1
2
3
4
enter elements for b
4
3
2
1
Output:
addition is:
5 5
5 5

17
15. Aim: Program on two dimensional array- subtraction of two
matrices.
Source code:
#include<stdio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf(“enter elements for a:”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&a[i][j]);
printf(“enter elements for b:”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&b[i][j]);
printf(“subtraction is:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}

18
Input:
enter elements for a
8
7
6
5

enter elements for b


5
4
3
2
Output:
subtracton is:
3 3
3 3

19
16. Aim: Program on two dimensional array- multiplication of two matrices.

Source code:

#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("\n Enter the order of the matrix A :");
scanf("%d%d",&m,&n);
printf("\n Enter the order of the matrix B :");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix A & B is not multiplicable");
getch();
}
printf("\n 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("\n 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++)
{

20
for(j = 0 ; j < q ; j++)
{
c[i][j]=0;
for(k = 0 ; k < n ; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
printf("\n MATRIX A \n");
for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf(" %d \t", a[i][j]);
}
printf("\n");
}
printf("\n MATRIX B \n");
for(i = 0 ; i < p ; i++)
{
for(j = 0 ; j < q ; j++)
{
printf(" %d \t", b[i][j]);
}
printf("\n");
}
printf("\n MATRIX C \n");
for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j < q ; j++)
{
printf(" %d \t", c[i][j]);
}
printf("\n");

21
}
}

Sample Output 1:
Enter the order of the matrix A :2 2
Enter the order of the matrix B :2 2
Enter the elements of matrix A
1 2
3 4
Enter the elements of matrix B
2 3
4 5
MATRIX A
1 2
3 4
MATRIX B
2 3
4 5
MATRIX C
10 13
22 29

Output 2:
Enter the order of the matrix A :2 3
Enter the order of the matrix B :2 3
Matrix A & B is not multipliable.

22
17. Aim: Program on call by value – swapping of two elements
Source code:
#include<stdio.h>
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
main()
{
int a, b;
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
printf(“before swapping a=%d\t b=%d”,a,b);
swap(a,b);
printf(“after swapping a=%d\t b=%d”,a,b);
}
Input:
enter a, b values
5
3
Output:
before swapping a=5 b=3
after swapping a=5 b=3

23
18. Aim: Program on call by reference – swapping of two elements
Source code:
#include<stdio.h>
void swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
main()
{
int a, b;
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
printf(“before swapping a=%d\t b=%d”,a,b);
swap(&a,&b);
printf(“after swapping a=%d\t b=%d”,a,b);
}
Input:
enter a, b values
5
3
Output:
before swapping a=5 b=3
after swapping a=3 b=5

24
19. Aim: Program on recursion-factorial of a given number
Source code:
#include<stdio.h>
main()
{
int n;
printf(“enter the value for n”);
scanf(“%d”,&n);
printf(“the factorial =%d”,fact(n));
}
int fact(int x)
{
if(x==1)
return 1;
else
return(x*fact(x-1));
}
Input:
enter the value for n
5
Output:
the factorial = 120

25
20. Aim: Program on String Operations

Source code:

#include<stdio.h>
main()
{
char s1[20],s2[20],s3[20];
int x,l1,l2,l3;
printf(“Enter two strings\n”);
scanf(“%s%s”,s1,s2);
x=strcmp(s1,s2);//comparing two strings
if(x!=0)
{
printf(“Strings are not equal”);
strcat(s1,s2);//joining two strings
}
else
printf(‘Strings are equal”);
strcpy(s3,s1);//copying s1 to s3
//length of strings
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(s3);
//output
printf(“\n s1=%s\t length=%d characters\n”,s1,l1);
printf(“\n s2=%s\t length=%d characters\n”,s2,l2);
printf(“\n s3=%s\t length=%d characters\n”,s3,l3);
getch();
}

26
Input: 1.
Enter two Strings
New York

Output: 1.
Strings are not equal
s1=NewYork length=7 characters
s2=York length=7 characters
s3=NewYork length=7 characters

Input: 2.
Enter two Strings
London London

Output: 2.
Strings are equal
s1=London length=7 characters
s2=London length=7 characters
s3=London length=7 characters

27

You might also like