100% found this document useful (1 vote)
407 views

C Lab Programming

The document contains 22 code snippets with C programs demonstrating basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, and more. Each code snippet includes the program code, its aim, and sample input/output. The programs cover topics such as adding/printing integers, data types, operators, if/else conditions, loops, strings, functions, and checking properties of numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
407 views

C Lab Programming

The document contains 22 code snippets with C programs demonstrating basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, and more. Each code snippet includes the program code, its aim, and sample input/output. The programs cover topics such as adding/printing integers, data types, operators, if/else conditions, loops, strings, functions, and checking properties of numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

1

(1)

Aim:- Write a C Program to Add Two Integers.

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)

Aim:- Write a C Program to Print an Integer (Entered by the User).

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)

Aim:- Write a C Program to Multiply Two Floating-Point Numbers.

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)

Aim:- Write a C Program to Find ASCII Value of a Character.

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)

Aim:- Write a C Program to Compute Quotient and Remainder.

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)

Aim:- Write a C Program to Swap Two Numbers Using Temporary Variable.

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

printf("Before swapping a=%d b=%d\n\n",a,b);


temp=a;
a=b;
b=temp;
printf("After swapping a=%d b=%d\n\n",a,b);
getch();
}

Output:-
Enter a,b values
100
200
Before swapping a=100 b=200
After swapping a=200 b=100

(8)

Aim:- Write a C Program to Check Whether a Number is Even or Odd.

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)

Aim:- Write a C Program to Check Odd or Even Using the Ternary


Operator.

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)

Aim:- Write a C Program to Check Whether a Character is a Vowel or


Consonant.
7

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)

Aim:- Write a C Program to Find the Largest Number Among Three


Numbers.
8

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)

Aim:- Write a C Program to Check Leap Year.

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)

Aim:- Write a C Program to Check Whether a Character is an Alphabet or


not.

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)

Aim:- Write a C Program to Find Factorial of a Number.

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)

Aim:- Write a C Program to Generate Multiplication Table of a given


number.

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)

Aim:-Write a C Program to Display Fibonacci Sequence up to ‘n’ numbers.

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)

Aim:- Write a C Program to Count Number of Digits in an Integer.

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)

Aim:- Write a C Program to Reverse a Number.

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:

Enter a number 123

The reverse of the given number is 321

(20)

Aim:-Write a C Program to Check Whether a Number is Palindrome or Not.

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)

Aim:-Write a C Program to Check Whether a Number is Prime or Not.

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)

Aim:- Write a C Program to Check whether the given number is an


Armstrong Number or not.

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)

Aim:- Write a C Program to Make a Simple Calculator using switch...case.

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)

Aim:-Write a C program to reverse a Sentence Using Recursion.

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

long int bin=0;


clrscr();
printf("Enter the decimal number: \n");
scanf("%ld",&dec);
printf("Given decimal number is %ld\n",dec);
while(dec>0)
{
rem=dec%2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
printf("The Equivalent binary number is %ld",bin);
getch();
}

Output:

Enter the decimal number: 65


Given decimal number is 65
The Equivalent binary number is 1000001

(28)

Aim:-Write a C Program to Check Prime or Armstrong Number Using User-


defined Function.

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)

Aim:-Write a C program to calculate the power using recursion.

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

printf("Enter power number(positive integer): ");


scanf("%d", &a);
result = power(base, a);
printf("%d^%d = %d", base, a, result);
getch();
}
int power(int base, int a)
{
if (a != 0)
{
return (base * power(base, a - 1));
}
else
{
return 1;
}
}

Output:-

Enter base number: 3


Enter power number (positive integer): 4
3^4 = 81
(30)

Aim:-Write a C Program to Find G.C.D Using Recursion.

Program:-

#include <stdio.h>
int hcf(int n1, int n2);
void main()
{
int n1, n2;
printf("Enter two positive integers: ");
27

scanf("%d %d", &n1, &n2);


printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
getch();
}
int hcf(int n1, int n2)
{
if (n2 != 0)
{
return hcf(n2, n1 % n2);
}
else
{
return n1;
}
}

Output:-

Enter two positive integers: 366


60
G.C.D of 366 and 60 is 6.

(31)

Aim:-Write a C Program to Calculate Average Using Arrays.

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;

printf("The average is: %.2f ",avg);

getch();

Output:-

The average is: 62.125

(32)

Aim:-Write a C Program to Find Largest Element in an Array.

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:-

Enter Total Number of Elements in an Array up to 15: 5

Enter all the values till 5: 10 80 30 40 50

The largest Element in array is: 80

33. Write a C Program to Add Two Matrices Using Multi-dimensional


Arrays.
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r1,c1,a[10][10],b[10][10];
clrscr();
printf("Enter order of matrix A & B up to 10×10 :\n");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix of A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
30

}
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)

Aim:- Write a C Program to Find the Length of a String.

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)

Aim:-Write a C Program to Copy String without using strcpy().

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)

Aim:-Write a C Program to Count the Number of Vowels, Consonants

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)

Aim:-Write a C Program to Find the Frequency of Characters in a String.

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)

Aim:-Write a C Program to Access Array Elements Using Pointers.

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

Address of num: 2764564284


Using pointer variable:
Value of num: 100
Address of num: 2764564284

(41)

Aim:-Write a C program to swap two numbers using pointers

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

Enter value of num2: 200


38

Before Swapping: num1 is: 100, num2 is: 200

After Swapping: num1 is: 200, num2 is: 100

(42)

Aim:-Write a C program to count vowels and consonants in a string using


pointers.

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

Number of Vowels in String: 2

Number of Consonants in String: 2

(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

Enter information for 2nd distance


Enter feet: 34
Enter inch: 2.4
Sum of distances = 57'-11.0"

(45)

Aim:-Write a C Program to Store Information of Students Using Structure.

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)

Aim:-Write a C program to declare, initialize a union.

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)

Aim:-Write a C++ program to implement function overloading.

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

Enter base and height of triangle: 4 4

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:

void set_values (int,int);

int area (void)

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);

cout << "Area is : " << rect.area();

getch();

Output:-

Area is: 12

(49)

Aim:-Write a C++ program to add two numbers using data abstraction.

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

Enter two integers:


4
5
4+5=9

(50)
Aim:-Write a C++ program to overload binary operators
Program:-

#include <iostream.h>
#include <conio.h>
class Complex
{

int n1, n2;


public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";

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:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy