b.com ii sem c lab manual
b.com ii sem c lab manual
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf9"enter two number to swap");
scanf(“%d%d”, &a,&b);
printf("\n Before swapping");
printf("\n a is=%d",a);
printf("\n b is=%d",b);
temp=a;
a=b;
b=temp;
printf("\n After swapping");
printf("\n a is=%d",a);
printf("\n b is=%d",b);
getch();
}
Output
Enter two numbers to swap
45
Before swapping
A is = 4
B is = 5
After swapping
A is = 5
B is = 4
2. Write a C Program to test arithmetic operators.
#include<stdio.h>
#include<conio.h>
Void main()
{
int num1,num2,num3;
clrscr();
printf(”Enter two numbers to be operated with arithmetic
operators:”);
scanf(“%d%d”,&num1,&num2);
num3=num1+num2;
printf(”Num1+Num2=%d”,num3);
num3=num1*num2;
printf(”Num1*Num2=%d”,num3);
num3=num1-num2;
printf(”Num1-Num2=%d”,num3);
if(num2!=0)
num3=num1/num2;
printf(”Num1/Num2=%d”,num3);
else
printf(”num2 is not non-zer. Division is not defined.”);
getch();
}
Output
Enter two numbers to be operated with arithmetic operators:
46
Num1+Num2= 10
Num1*Num2= 24
Num1-Num2= -2
Num1/Num2= 0
3. Write a C program to determine the largest of three numbers Using
If.. else statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf(" \n A is largest");
else
if(b>c)
printf("\n B is largest");
else
printf("\n C is largest");
getch();
}
Output
Enter three number
2
12
5
B is largest
4. Write a C Program to Print month name of given number Using switch statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int month;
clrscr();
printf("Enter month number");
scanf(“%d”,&month);
switch(month)
{
case 1: printf("month name = january");
break;
case 2: printf("month name = february");
break;
case 3: printf("month name = march");
break;
case 4: printf(“month name = april");
break;
case 5: printf("month name = may");
break;
case 6: printf("month name = june");
break;
case 7: printf("month name = july");
break;
case 8: printf("month name = august");
break;
case 9: printf("month name = september");
break;
case 10: printf("month name = october");
break;
case 11: printf("month name = november");
break;
case 12: printf("month name = december");
break;
default:
printf("Please enter correct month number between 1 to 12");
break;
}
getch();
}
Output
Enter month number 12
Monthname = December
Enter month number 13
Please enter correct month number between 1 to 12
5. Write a C program to find the sum of it’s digits Using While
statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,k;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(n>0)
{
k=n%10;
s=s+k;
n=n/10;
}
printf("\n sum of digits is%d", s);
getch();
}
Output
Enter the number 123
Sum of digits 6
6. Program to check whether the given number is palindrome or not Using do.. while
statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,digit,rev=0;
clrscr();
printf("Enter a positive number");
scanf(“%d”,&num);
n=num;
do
{
digit=num%10;
rev=(rev*10)+digit;
num=num/10;
} while(num!=0);
Printf("\n The reverse number is %d”,rev);
if(n==rev)
printf("\n the number is a palindrome");
else
printf("\n the number is not a palindrome");
getch();
}
Output
Enter a positive number
12321
The reverse number is 12321
The number is a palindrome
Enter a positive number
12331
The reverse number is 13321
The number is not a palindrome
7. Write a C program to print 1 to 10 multiplication tables.
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,j;
clrscr();
for (i=1;i<=10;i++)
{
for (j=1;j,=10;j++)
{
printf(“ %d *%d = %d”,I,j,i*j);
}
printf(“\n”);
}
getch();
}
Output
1 * 1 =1
1 * 2 =2
1 * 3 =3
1 * 4 =4
1 * 5 =5
1 * 6 =6
1 * 7 =7
1 * 8 =8
1 * 9 =9
1 * 10 =10
2 * 1 =2
2 * 2 =4
2 * 3 =6
2 * 4 =8
2 * 5 =10
2 * 6 =12
2 * 7 =14
2 * 8 =16
2 * 9 =18
2 * 10 =20
8. Write a C program to read elements into an Array and display the
Array elements.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a[5],i;
Clrscr();
// Reading elements into an Array
Printf(“enter 5 elements”);
For(i=0;i<5;i++)
{
Scanf(“%d”,&a[i]);
}
// Display the Array elements
Printf(“\n Array elements are”);
For(i=0;i<5;i++)
{
Printf(“%d\n ”,a[i]);
}
Getch();
}
Output
Enter 5 elements
11
12
13
14
15
Array elements are
11
12
13
14
15
9. Write a C program to read elements into Matrix and display the
matrix.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a[2][2],I,j;
Clrscr();
// Reading elements into an Matrix
Printf(“enter matrix elements”);
For(i=0;i<2;i++)
{
For(j=0;j<2;j++)
{
Scanf(“%d”,&a[i][j]);
}
}
// Display the Matrix elements
Printf(“\n Martrix elements are”);
For(i=0;i<2;i++)
{
For(j=0;j,2;j++)
{
Printf(“%d\n ”,a[i][j]);
}
Printf(“\n”);
}
Getch();
}
Output
Enter Matrix elements
11
12
13
14
Matrix elements are
11 12
13 14
10. Write a C program to find the largest and smallest elements in the
Array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,small,large,a[20];
clrscr();
printf("enter the size of array");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
large=a[0];
small=a[0];
for(i=1;i<n;i++)
{
if(a[i]>large)
large=a[i];
else
if(a[i]<small)
small=a[i];
}
printf("\n The largest element is%d",large);
printf("\n The smallest element is %d",small);
getch();
}
Output
Enter the size of Array 5
Enter the elements
3
11
16
78
The largest element is 16
The smallest element is 3
11. Write a C program to accept your name in lower case.
a. Display it in uppercase
b. Display it’s length
c. Display it reverse using string functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
clrscr();
printf("\n enter your name in lowercase");
scanf("%s",name);
printf("\n your name in uppercase:%s",strupr(name));
printf("\n length of your name is %d",strlen(name));
printf("\n Your name in reverse order %s",strrev(name));
getch();
}
Output
Enter your name in lower case
Sridhar
Your name in uppercase: SRIDHAR
Length of your name is: 7
Your name in reverse order : RAHDIRS
12. Write a C program to check given string is Palindrome or not Using
String functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[10],str2[10];
clrscr();
printf("\n enter the string");
scanf("%s",str1);
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0)
printf("\n given string is palindrome");
else
printf("\n given string is not palindrome");
getch();
}
Output
Enter the string
Liril
Given string is palindrome
Enter the string
Soap
Given string is not palindrome
13. Write a C program to find to add two numbers Using Functions.
#include<stdio.h>
#include<conio.h>
Void add(int a,int b);
Void main()
{
Int x,y,sum;
Clrscr();
Printf(“enter two numbers”);
Scanf(“%d%d”,&x,&y);
Sum=add(x,y);
Printf(“The sum is%d”,sum);
Getch();
}
Void add(int a, int b)
{
Int result;
Result=a+b;
Return result;
}
Output
Enter two numbers
10
20
The sum is 30
14. Write a C program to print student details Using structure.
#include<stdio.h>
struct student
{
int rno;
char name[10];
int marks;
};
main()
{
struct student s={111,"sridhar",76};
printf("\n the details of students is\n");
printf("\n Rno\t Name \t Marks \n");
printf("\n %d\t%s\t%d",s.rno,s.name,s.marks);
getch();
}
Output
The details of students is
Rno Name Marks
111 sridhar 76
15. Write a C program to illustrate the Unions.
#include<stdio.h>
#include<conio.h>
union item
{
int var1;
long var2;
};
main()
{
union item code={10};
clrscr();
printf("%f",code.var1);
getch();
}
Output
10
16. Write a C program to swap two numbers Using Pointers.
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a=100,b=200;
clrscr();
printf("before swapping");
printf("\n a is =%d",a);
printf("\n b is =%d",b);
swap(&a,&b);
printf("\n after swapping");
printf("\n a is =%d",a);
printf("\n b is =%d",b);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output
Before swapping
A is = 100
B is = 200
After swapping
A is = 200
B is =100