C Lab Programming
C Lab Programming
(1)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf(”Enter two numbers:”);
scanf(“%d %d”,&a,&b);
c=a+b;
printf(”Sum= %d”,c);
getch();
}
Output:-
Enter two numbers: 20 30
Sum=50
(2)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(”Enter any number:”);
scanf(“%d”,&a);
printf(”you entered %d”,a);
getch();
2
}
Output:-
Enter any number: 10
you entered 10
(3)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
printf(”Enter two float numbers:”);
scanf(“%f %f”,&a,&b);
c=a*b;
printf(”product= %f”,c);
getch();
}
Output:-
Enter two float numbers: 2.5 3.5
Product=8.75
(4)
Program:-
#include <stdio.h>
void main()
{
char c;
printf("Enter a character: ");
3
scanf("%c", &c);
printf("ASCII value of %c = %d", c, c);
getch();
}
Output:-
Enter a character: G
ASCII value of G = 71
(5)
Program:-
#include <stdio.h>
void main()
{
int a, b, quotient = 0, remainder = 0;
printf("Enter two numbers A and B : \n");
scanf("%d %d", &a, &b);
quotient = a / b;
remainder = a % b;
printf("Quotient when A/B is: %d\n", quotient);
printf("Remainder when A/B is: %d", remainder);
getch();
}
Output:-
Enter two numbers A and B : 17 5
Quotient when A/B is: 3
Remainder when A/B is: 2
(6)
Aim:- Write a C Program to Find the Size of int, float, double and char.
4
Program:-
#include<stdio.h>
void main()
{
int intType;
float floatType;
double doubleType;
char charType;
printf("Size of int: %ld bytes\n", sizeof(intType));
printf("Size of float: %ld bytes\n", sizeof(floatType));
printf("Size of double: %ld bytes\n", sizeof(doubleType));
printf("Size of char: %ld byte\n", sizeof(charType));
getch();
}
Output:-
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
(7)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("Enter a,b values\n");
scanf("%d %d",&a,&b);
5
Output:-
Enter a,b values
100
200
Before swapping a=100 b=200
After swapping a=200 b=100
(8)
Program:-
#include <stdio.h>
void main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
getch();
}
Output:-
6
Enter an integer: 7
7 is odd.
(9)
Program:-
#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");
getch();
}
Output:-
Enter an integer 4
Even number
(10)
Program:-
#include <stdio.h>
void main()
{
char c;
int lowercase, uppercase;
clrscr();
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase || uppercase)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
getch();
}
Output:-
Enter an alphabet: G
G is a consonant.
(11)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter a,b,c values\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("a is greater\n");
}
else if(b>a && b>c)
{
printf("b is greater\n");
}
else if(c>a && c>b)
{
printf("c is greater\n");
}
getch();
}
Output:
Enter a,b,c values
100
300
9
500
C is greater
(12)
Program:-
#include <stdio.h>
void main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year);
}
else
printf("%d is not a leap year.", year);
getch();
}
Output:-
Enter a year: 1900
1900 is not a leap year.
10
(13)
Program:-
#include <stdio.h>
void main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.", c);
getch();
}
Output:-
Enter a character: *
* is not an alphabet
(14)
Aim:- Write a C Program to Calculate the Sum of first ‘N’ Natural Numbers.
Program:-
#include <stdio.h>
11
void main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i)
{
sum += i;
}
printf("Sum = %d", sum);
getch();
}
Output:-
Enter a positive integer: 10
Sum = 55
(15)
Program:-
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,f;
clrscr();
printf("Enter n value\n");
scanf("%d",&n);
f=fact(n);
printf("The factorial of the given number is %d\n",f);
getch();
}
int fact(int n)
12
{
if(n>1)
{
return(n*fact(n-1));
}
else
{
return n;
}
}
Output:-
Enter n value
5
The factorial of the given number is 120
(16)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter a number:\n");
scanf("%d",&n);
printf("\nMultiplication Table of %d is\n",n);
for(i=1;i<=10;i++)
{
printf("\n %2d * %2d=%3d",n,i,n*i);
}
13
getch();
}
Output:-
Enter a number:
10
Multiplication Table of 10 is
10 * 1= 10
10 * 2= 20
10 * 3= 30
10 * 4= 40
10 * 5= 50
10 * 6= 60
10 * 7= 70
10 * 8= 80
10 * 9= 90
10 *10=100
(17)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("Enter n value\n");
scanf("%d",&n);
printf("The Fibonacci series is\n");
printf("%d",a);
printf(" %d",b);
14
for(i=0;i<=n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}
Output:-
Enter n value
5
The Fibonacci series is
0 1 1 2 3 5
(18)
Program:-
#include <stdio.h>
void main() {
int n,count = 0;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
n /= 10;
++count;
}
printf("Number of digits: %d", count);
}
Output:
Enter an integer: 3452
15
Number of digits: 4
(19)
Program:-
#include<stdio.h>
#include<conio.h>
void main ()
{
int num, rev=0,r,;
clrscr ();
printf("Enter a number\n");
scanf("%d",&num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("The reverse of the given number is %d", rev);
getch ();
}
Output:
(20)
Program:-
#include<stdio.h>
#include<conio.h>
void main ()
{
int num, rev=0,r,k;
clrscr ();
16
printf("Enter a number\n");
scanf("%d",&num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("The reverse of the given number is %d", rev);
if(rev==k)
{
printf("The given number is palindrome\n ",k);
}
else
{
printf("The given number is not palindrome\n ",k);
}
getch ();
}
Output:
Enter a number 123
The reverse of the given number is 321
The given number is not palindrome
(21)
Program:-
#include <stdio.h>
void main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i)
{
17
if (n % i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
getch();
}
Output:
Enter a positive integer: 29
29 is a prime number.
(22)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
18
int n,b=0,t=n,a;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
while(n>0)
{
a=n%10;
b=b+(a*a*a);
n=n/10;
}
if(b==t)
{
printf("Armstrong number\n");
}
else
{
printf("Not an Armstrong number\n");
}
getch();
}
Output:-
Enter the number 153
Armstrong number
(23)
Program:-
#include <stdio.h>
void main()
{
19
char operator;
float first, second;
printf("Enter any operator (+, -, *,/,%));
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%f %f", &first, &second);
switch (operator)
{
case '+ ':
printf("%f + %f = %f", first, second, first + second);
break;
case '-' :
printf("%f - %f = %f", first, second, first - second);
break;
case '*' :
printf("%f * %f = %f", first, second, first * second);
break;
case '/' :
printf("%f / %f = %f", first, second, first / second);
break;
default :
printf("Error! Operator is not correct");
}
getch();
}
Output:-
Enter any operator (+, -, *,/,%)
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
(24)
Aim:-Write a C Programming Code To Create Pyramid and Pattern.
20
Program:-
#include<stdio.h>
int main() {
int i, space, rows, k=0;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i=1; i<=rows; ++i,k=0)
{
for (space=1; space<=rows-i; ++space)
{
printf(" ");
}
while (k!=2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
getch();
}
Output:-
*
***
*****
*******
*********
(25)
Program:-
#include <stdio.h>
21
void reverse();
void main()
{
printf("Please enter a sentence: ");
reverse();
}
void reverse()
{
char c;
scanf("%c", &c);
if (c != '\n') {
reverse();
printf("%c", c);
}
}
Output:-
Please enter a sentence:
HAI
IAH
(26)
Aim:-Write a C Program to Display Prime Numbers Between Intervals
Using Function.
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,i,j,c;
clrscr();
printf("Enter the starting number and ending Number: ");
scanf("%d %d",&n1,&n2);
22
for(i=n1;i<=n2;i++)
{
c=0;
for(j=n1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\t",i);
}
}
getch();
}
Output:-
Enter the starting number and ending Number:
2
10
2 3 5 7
(27)
Aim:-Write a C Program to Convert Binary Number to Decimal and
Vice-versa.
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
long int dec,rem,i=1;
23
Output:
(28)
Program:-
#include <stdio.h>
#include <math.h>
int Prime(int n);
int Armstrong(int n);
void main()
{
int n, flag;
printf("Enter a positive integer: ");
scanf("%d", &n);
flag =Prime(n);
24
if (flag == 1)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
flag = Armstrong(n);
if (flag == 1)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number",n);
return 0;
}
int Prime(int n)
{
int i, flag = 1;
for(i=2; i<=n/2; ++i)
{
if(n%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
int Armstrong(int number)
{
int originalNumber, remainder, result = 0, n = 0, flag;
originalNumber = number;
while (originalNumber != 0)
{
originalNumber /= 10;
++n;
25
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, n);
originalNumber /= 10;
}
if(result == number)
flag = 1;
else
flag = 0;
return flag;
}
Output:-
Enter a positive integer: 407
407 is not a prime number.
407 is an Armstrong number.
(29)
Program:-
#include <stdio.h>
int power(int n1, int n2);
void main()
{
int base, a, result;
clrscr();
printf("Enter base number: ");
scanf("%d", &base);
26
Output:-
Program:-
#include <stdio.h>
int hcf(int n1, int n2);
void main()
{
int n1, n2;
printf("Enter two positive integers: ");
27
Output:-
(31)
Program:-
#include <stdio.h>
#include <stdlib.h>
void main()
int arr[]={56,78,94,56,73,42,31,67};
28
int total=0,i;
clrscr();
for(i=0; i<=7; i++)
total=total+arr[i];
double avg=total/i;
getch();
Output:-
(32)
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[15],i,n,largest;
clrscr();
printf("\n Enter Total Number of Elements in an Array up to 15 :");
scanf("%d",&n);
printf("\n Enter all the values till %d: ",n);
for(i=0; i < n; i++)
scanf("%d",&a[i]);
largest = a[0];
for(i = 0 ; i < n ; i++)
29
{
if ( a[i] > largest )
largest = a[i];
}
printf("\n The largest Element in array is: %d",largest);
getch();
}
Output:-
}
printf("Enter elements of matrix of B:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n matrix addition \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%3d",a[i][j]+b[i][j]);
}
printf("\n");
}
getch();
}
Output:-
Enter order of matrix A & B up to 10×10:
3 3
Enter elements of matrix of A:
111
111
111
Enter elements of matrix of B:
222
222
222
31
Matrix addition
3 3 3
3 3 3
3 3 3
(34)
Program:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf("Enter the string for finding length\n");
gets(str1);
printf("string length=%d\n",strlen(str1));
getch();
}
Output:-
Enter the string for finding length
Geethanjali
String length=11
(35)
Aim:- Write a C Program to Concatenate Two Strings
Program:-
#include<stdio.h>
#include<conio.h>
32
#include<string.h>
void main()
{
char S1[30],S2[30];
clrscr();
printf("Enter two Sings for concatenation\n");
gets(S1);
gets(S2);
strcat(S1,S2);
puts(S1);
getch();
}
Output:-
Enter two strings for concatenation
Sachin
Tendulker
Sachin Tendulker
(36)
Program:-
#include <stdio.h>
void main()
{
char s1[100], s2[100], i;
clrscr();
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
for (i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
33
}
s2[i] = '\0';
printf("String s2: %s", s2);
getch();
}
Output:-
Enter string s1: programmer.
String s2: programmer.
(37)
and so on.
Program:-
#include <stdio.h>
void main()
{
char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
clrscr();
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
for (int i = 0; line[i] != '\0'; ++i) {
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||
line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||
line[i] == 'U')
{
++vowels;
}
else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))
{
34
++consonant;
}
else if (line[i] >= '0' && line[i] <= '9')
{
++digit;
}
else if (line[i] == ' ')
{
++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
getch();
}
Output:-
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2
(38)
Program:-
#include <stdio.h>
void main()
{
char str[20], ch;
35
int count = 0;
clrscr();
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i)
{
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, freq);
getch();
}
Output:-
Enter a string: This website is awesome.
Enter a character to find its frequency: e
Frequency of e = 4
(39)
Program:-
#include <stdio.h>
void main() {
int data[5];
printf("Enter elements: ");
for (int i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: \n");
for (int i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
36
getch();
}
Output:-
Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4
(40)
Aim:-Write a C program to create, initialize, assign and access a pointer
variable.
Program:-
#include <stdio.h>
void main()
{
int num;
int *pNum;
pNum=& num;
num=100;
clrscr();
printf("Using variable num:\n");
printf("value of num: %d\n address of num: %u\n",num,&num);
printf("Using pointer variable:\n");
printf("value of num: %d\n address of num: %u\n",*pNum,pNum);
getch();
}
Output:-
Using variable num:
Value of num: 100
37
(41)
Program:-
#include <stdio.h>
void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
void main()
{
int n1,n2;
printf("Enter value of n1: ");
scanf("%d",&n1);
printf("Enter value of n2: ");
scanf("%d",&n2);
printf("Before Swapping: n1 is: %d, n2 is: %d\n",n1,n2);
swap(&n1,&n2);
printf("After Swapping: n1 is: %d, n2 is: %d\n",n1,n2);
getch();
}
Output:-
Enter value of num1: 100
(42)
Program:-
#include <stdio.h>
void main()
{
char str[100];
char *p;
int vCount=0,cCount=0;
clrscr();
printf("Enter any string: ");
fgets(str, 100, stdin);
p=str;
while(*p!='\0')
{
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCount++;
else
cCount++;
p++;
}
printf("Number of Vowels in String: %d\n",vCount);
printf("Number of Consonants in String: %d",cCount);
getch();
}
Output:-
Enter any string: book
39
(44)
Aim:- Write a C Program to Add Two Distances (in inch-feet system) using
Structures.
Program:-
#include<stdio.h>
struct Distance
{
int feet;
float inch;
} d1, d2, sumOfDistances;
void main()
{
clrscr();
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
sumOfDistances.feet=d1.feet+d2.feet;
sumOfDistances.inch=d1.inch+d2.inch;
if (sumOfDistances.inch>12.0)
{
sumOfDistances.inch = sumOfDistances.inch-12.0;
++sumOfDistances.feet;
40
}
printf("\nSum of distances = %d\'-%.1f\"", sumOfDistances.feet,
sumOfDistances.inch);
getch();
}
Output:-
Enter information for 1st distance
Enter feet: 23
Enter inch: 8.6
(45)
Program:-
#include<stdio.h>
#include<conio.h>
void main ()
{
struct student
{
int sid;
char sname[20];
float fees;
}s1;
clrscr();
41
printf("Enter sid,sname,fees\n");
scanf("%d%s%f",&s1.sid,&s1.sname,&s1.fees);
printf("\nsid=%d\nsname=%s\nfees=%f\n", s1.sid,s1.sname,s1.fees );
getch();
}
Output:-
Enter sid,sname,fees
100
Praveen
15000
sid=100
sname=Praveen
fees=15000.00000
(46)
Program:-
#include<stdio.h>
#include<conio.h>
void main ()
{
union emp
{
int eid;
char ename[20];
float esalary;
}e1;
clrscr();
printf("Enter eid, ename,esalary\n ");
42
scanf("%d%s%f",&e1.eid,&e1.ename,&e1.esalary);
printf("\neid=%d\n ename=%s\n esalary=%f\n", e1.eid,e1.ename,e1.esalary );
getch();
}
Output:-
Enter eid,ename,esalary
1111
Praveen
20000
eid=1111
ename=Praveen
esalary=20000.00
(47)
Program:-
#include <iostream.h>
#include <conio.h>
int area(int);
int area(int,int);
float area(float);
float area(float,float);
void main()
{
int s,l,b;
float r,b,h;
clrscr();
cout<<"Enter side of a square: ";
43
cin>>s;
cout<<"Enter length and breadth of rectangle: ";
cin>>l>>b;
cout<<"Enter radius of circle: ";
cin>>r;
cout<<"Enter base and height of triangle: ";
cin>>b>>h;
cout<<"Area of square is "<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(b,h);
}
int area(int s)
{
return (s*s);
}
int area(int l,int b)
{
return (l*b);
}
float area(float r)
{
return (3.14*r*r);
}
float area(float b,float h)
{
return ((b*h)/2);
}
Output:-
Enter side of a square: 2
Enter length and breadth of rectangle: 3 6
Enter radius of circle: 3
44
Area of square is 4
Area of rectangle is 18
Area of circle is 28.26
Area of triangle is 8
48. Write a C++ program to calculate an area of rectangle using
encapsulation.
Program:-
#include <iostream.h>
#include<conio.h>
class Rectangle
int x, y;
public:
return (x*y);
};
void Rectangle::set_values (int a, int b)
x=a;
y= b;
void main ()
45
clrscr();
Rectangle rect;
rect.set_values (3,4);
getch();
Output:-
Area is: 12
(49)
Program:-
#include <iostream.h>
#include <conio.h>
void main()
{
int n1, n2, Sum;
clrscr();
cout << "Enter two integers: ";
cin >> n1 >> n2;
Sum = n1 + n2;
cout << n1 << " + " <<n2 << " = " << Sum;
getch();
}
Output:-
46
(50)
Aim:-Write a C++ program to overload binary operators
Program:-
#include <iostream.h>
#include <conio.h>
class Complex
{
cin>>n1>>n2;
}
Complex operator+(Complex obj) //Overloading '+' operator
{
Complex c;
c.n1=n1+obj.n1;
c.n2=n2+obj.n2;
return(c);
}
void display()
{
cout<<n1<<"+"<<n2<<"i"<<"\n";
}
};
void main()
{
Complex c1, c2, sum;
clrscr();
c1.accept();
c2.accept();
47
sum = c1+c2;
cout<<"\n Entered Values:"<<endl;
cout<<"\t";
c1.display();
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers: "<<endl;
cout<<"\t";
sum.display();
getch();
}
Output:-