0% found this document useful (0 votes)
21 views

C and CPP Record II Sem CA

Uploaded by

vikas goud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C and CPP Record II Sem CA

Uploaded by

vikas goud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

1.

Write a C Program to Add Two Integers

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{

int number1,number2,sum;
clrscr();
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2; // calculating sum
printf("Addition of two integers = %d",sum);
getch();
}

Output :-
Enter two integers: 12
11
Addition of two integers =
23
2. Write a C Program to Print an Integer (Entered by the User).

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf("Enter an integer: ");
scanf("%d",&number); // reads and stores input
printf("You entered: %d", number); // displays output

getch();
}

1
Output:-
Enter an integer: 25
You entered: 25

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

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
double a, b, product;
clrscr();
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
product = a * b;// Calculating product
// Result up to 2 decimal point is displayed using %.2lf
printf("Product = %.2lf", product);
getch();
}

Output:-
Enter two numbers: 2.4

1.12

Product=2.69

4. Write a C Program to Find ASCII Value of a Character.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char c;

2
clrscr();
printf("Enter a character: ");
scanf("%c",&c);

// %d displays the integer value of a character


// %c displays the actual character

printf("ASCII value of %c = %d", c, c);


getch();
}

Output:-
Enter a character: G
ASCII value of G = 71

5. Write a C Program to Compute Quotient and Remainder.


/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int dividend, divisor, quotient, remainder;
clrscr();
printf("Enter dividend: ");
scanf("%d",&dividend);
printf("Enter divisor: ");
scanf("%d",&divisor);

// Computes quotient
quotient = dividend / divisor;

// Computes remainder
remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);
getch();
}

Output:-
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1

3
6. Write a C Program to Find the Size of int, float, double and char.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int intType;
float floatType;
double doubleType;
char charType;
clrscr();
// sizeof() evaluates the size of a variable
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: 2 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

7. Write a C Program to Swap Two Numbers Using Temporary Variable.


/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int first, second, temp;
clrscr();
printf("Enter first number: ");
scanf("%d",&first);
printf("Enter second number: ");
scanf("%d",&second);
printf("\nBefore swapping, firstNumber = %d secondNumber = %d
\n",first,second);

4
// Value of first is assigned to temp
temp = first;

// Value of second is assigned to first


first = second;

// Value of temp (initial value of first) is assigned to second


second = temp;

printf("\nAfter swapping, firstNumber = %d secondNumber = %d


\n",first,second);
getch();
}

Output:-
Enter first number: 20
Enter second number: 45

Before swapping, firstNumber = 20 secondNumber = 45


After swapping, firstNumber = 45 secondNumber = 20
8.Write a C Program to Check Whether a Number is Even or Odd

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter an integer: ");
scanf("%d",&num);

// True if num is perfectly divisible by 2


if(num %2==0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

getch();
}

Output:-
Enter an integer: 7
7 is odd.

5
9. Write a C Program to Check Odd or Even Using the Ternary Operator.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter an integer: ");
scanf("%d",&num);

//Using Ternary Operator


(num %2==0)? printf("%d is even.", num): printf("%d is odd.", num);

getch();
}

OUTPUT:-

Enter an integer: 33
33 is odd.
10. Write a C Program to Check Whether a Character is a Vowel or Consonant.
/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf ("Please Enter an alphabet: \n");
scanf(" %c", &ch);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||


ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
printf("\n %c is a VOWEL.", ch);
}
else
{
printf("\n %c is a CONSONANT.", ch);
}

6
getch();
}

OUTPUT:-

Enter an alphabet: G
G is a consonant.

11. Write a C Program to Find the Largest Number Among Three Numbers.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, n3;
clrscr();
printf("Enter three numbers: ");
scanf("%d %d %d ",&n1,&n2,&n3);
if(n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
else if(n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
getch();
}

OUTPUT:-

Enter three numbers: 4


3
5
5 is the largest number.

12. Write a C Program to Check Leap Year.

There are two conditions for leap year: 1- If year is divisible by 400 (for Century years), 2- If year is
divisible by 4 and must not be divisible by 100 (for Non Century years).

For example,

1999 is not a leap year


2000 is a leap year
2004 is a leap year

7
/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year: ");
scanf("%d", &year);
if((year % 4 == 0 && year % 100!= 0 ) || (year % 400 == 0) )
{
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.

OUTPUT:-

Enter a year: 2012


2012 is a leap year.

13. Write a C Program to Check Whether a Character is an Alphabet or not.

Within this C Program to check the Character is Alphabet or Not example, the first printf
statement will ask the user to enter any character. Next, the character will assign to variable ch.

if( (ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’))

Within the If statement, the First condition (ch >= ‘a’ && ch <= ‘z’) will check whether the
character entered by the user is between a and z. The second condition (ch >= ‘A’ && ch <= ‘Z’)
will check whether the character entered by the user is between A and B.

 If one of those conditions is TRUE, the character entered by the user is an Alphabet.
 If both of the above conditions is FALSE, the character entered by the user is Not an
Alphabet.

8
/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
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

This C program check whether the input character is Alphabet or not using one of the built-in
function isapha()

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\n Please Enter any character \n");
scanf("%c", &ch);

if(isalpha(ch) )
printf("\n%c is an Alphabet", ch);
else
printf("\n %c is not an Alphabet", ch);
getch();
}

9
OUTPUT:-

Please enter any character: *


* is not an alphabet

ANALYSIS

In this C Program to check whether the Character is Alphabet or Not example

 isalpha(ch) function check whether the given character (ch) is Alphabet or not.
 If the condition isalpha(ch) is TRUE, the character is an Alphabet.
 If the condition isalpha(ch) is FALSE, the character is Not an Alphabet.

14. Write a C Program to Calculate the Sum of first ‘N’ Natural Numbers.

The positive numbers 1, 2, 3...are known as natural numbers. The sum of natural numbers up to 5
is:

Sum = 1 + 2 + 3 + 4 + 5

Sum of Natural Numbers Using for Loop


/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);

for (i = 1; i <= n; ++i)


{
sum += i;
}

printf("Sum = %d", sum);


getch ();
}

10
OUTPUT:-
Enter a positive integer:5

Sum=15

Sum of Natural Numbers Using while Loop

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum =0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
i =1;
while(i <= n)
{
sum+= i;
++i;
}

printf("Sum = %d", sum);


getch();
}

OUTPUT:-

Enter a positive integer: 100


Sum = 5050

Read Input until a Positive Integer is Entered


/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum =0;
clrscr();

11
do{
printf("Enter a positive integer: ");
scanf("%d",&n);
}while(n <=0);

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


sum+= i;
}

printf("Sum = %d", sum);


getch();
}

OUTPUT:-

Enter a positive integer:5

Sum=15

15. Write a C Program to Find Factorial of a Number

The factorial of a positive number n is given by:

1. Factorial of n: (n!)=1*2*3*4....n

The factorial of a negative number doesn't exist. And, the factorial of 0 is 1.

This program takes a positive integer from the user and computes the factorial using for loop.

Since the factorial of a number may be very large, the type of factorial variable is declared as
unsigned long long.

If the user enters a negative number, the program displays a custom error message.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
unsigned long long fact =1;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);

// shows error if the user enters a negative integer


if(n <0)

12
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i =1; i <= n;++i)
{
fact*= i;
}
printf("Factorial of %d = %llu", n, fact);
}

getch();
}

OUTPUT:-

Enter an integer: 10
Factorial of 10 = 3628800

16. Write a C Program to Generate Multiplication Table of a given number.

The program below takes an integer input from the user and generates the multiplication tables
up to 10.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
getch();
}

13
OUTPUT:-

Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

17. Write a C Program to Display Fibonacci sequence up to ‘n’ numbers.

The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms.
The first two terms of the Fibonacci sequence are 0 followed by 1.

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, t1 =0, t2 =1,nextTerm;
clrscr();
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: ");

for(i =1; i <= n;++i)


{
printf("%d, ", t1);
nextTerm= t1 + t2;
t1 = t2;
t2 =nextTerm;
}
getch();
}

14
OUTPUT:-

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

18. Write a C Program to Count Number of Digits in an Integer.

This program takes an integer from the user and calculates the number of digits. For example: If
the user enters 2319, the output of the program will be 4.

The integer entered by the user is stored in variable n. Then the while loop is iterated until the
test expression n! = 0 is evaluated to 0 (false).

 After the first iteration, the value of n will be 345 and the count is incremented to 1.
 After the second iteration, the value of n will be 34 and the count is incremented to 2.
 After the third iteration, the value of n will be 3 and the count is incremented to 3.
 After the fourth iteration, the value of n will be 0 and the count is incremented to 4.

Then the test expression of the loop is evaluated to false and the loop terminates.

Program to Count the Number of Digits


/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
long long n;
int count =0;
clrscr();
printf("Enter an integer: ");
scanf("%lld",&n);
while(n!=0)
{
n/=10;// n = n/10
++count;
}
printf("Number of digits: %d", count);
getch();
}

15
OUTPUT:-

Enter an integer: 3452


Number of digits: 4

19. Write a C Program to Reverse a Number.

This program takes an integer input from the user. Then the while loop is used until n!= 0 is
false (0).
In each iteration of the loop, the remainder when n is divided by 10 is calculated and the value of
n is reduced by 10 times.

Inside the loop, the reversed number is computed using:

rev= rev*10+ remainder;

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev =0, remainder;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);
while(n !=0)
{
remainder= n %10;
rev= rev *10+ remainder;
n/=10;
}
printf("Reversed number = %d", rev);
getch();
}

OUTPUT:-

Enter an integer: 2345


Reversed number = 5432

16
20. Write a C Program to Check Whether a Number is Palindrome or Not.

An integer is a palindrome if the reverse of that number is equal to the original number.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, reversedN = 0, remainder, originalN;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;

// reversed integer is stored in reversedN


while (n != 0)
{
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}

// palindrome if orignalN and reversedN are equal


if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);

getch();
}

OUTPUT:-

Enter an integer: 1001


1001 is a palindrome.

17
21. Write a C Program to Check Whether a Number is Prime or Not.

A prime number is a positive integer that is divisible only by 1 and itself. For example: 2, 3, 5, 7,
11, 13, 17

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, flag = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);

for (i = 2; i <= n / 2; ++i)


{
// condition for non-prime
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.

18
In the program, a for loop is iterated from i = 2 to i < n/2.

In each iteration, whether n is perfectly divisible by i is checked using:

1. if(n % i ==0){
2.
3. }

If n is perfectly divisible by i, n is not a prime number. In this case, flag is set to 1, and the loop
is terminated using the break statement.

After the loop, if n is a prime number, flag will still be 0. However, if n is a non-prime number,
flag will be 1.

22. Write a C Program to Check whether the given number is an Armstrong


Number or not.
A number that is the sum of its own digits each raised to the power of the
number of digits.
Ex. 153=13+53+33

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num,originalNum, remainder, result =0;
clrscr();
printf("Enter a three-digit integer: ");
scanf("%d",&num);
originalNum=num;
while(originalNum!=0)
{
remainder=originalNum%10;
result+= remainder * remainder * remainder;
originalNum/=10;
}

if(result ==num)
printf("%d is an Armstrong number.",num);
else
printf("%d is not an Armstrong number.",num);

getch();
}

19
OUTPUT:-

Enter a three-digit integer: 371


371 is an Armstrong number.

23. Write a C Program to Make a Simple Calculator Using switch...case.

This program takes an arithmetic operator +, -, *, / and two operands from the user. Then, it
performs the calculation on the two operands depending upon the operator entered by the user.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char operator;
clrscr();
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

getch();
}

20
OUTPUT:-

Enter an operator (+, -, *,): *


Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8

24. Write a C Programming Code To Create Pyramid and Pattern.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, space, rows, k=0;
clrscr();
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:-

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

21
27. Write a C Program to Convert Binary Number to Decimal and vice-versa.

We can obtain a decimal number by multiplying each digit of binary number with power of 2
and adding each multiplication result. The power starts from 0 and goes to n-1 where n is
the total number of digits in binary number.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
long int i,n,x=0,a;
clrscr();
printf("Enter any binary number: ");
scanf("%ld",&n);
printf("\nThe decimal conversion of %ld is ",n);
for(i=0;n!=0;++i)
{
a=n%10;
x=(a)*(pow(2,i))+x;
n=n/10;
}
printf("%ld",x);
getch();
}

OUTPUT:-

Enter any binary number: 111


The decimal conversion of 111 is 7

22
28. Write a C Program to Check Prime or Armstrong Number Using
User-defined Function.

The checkPrimeNumber() function returns 1 if the number entered by the user is a prime number.
Similarly, checkArmstrongNumber() function also returns 1 if the number entered by the user is an
Armstrong number.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<math.h>
#include<stdio.h>
#include<conio.h>
int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);
void main()
{
int n, flag;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);

// check prime number


flag= checkPrimeNumber(n);
if(flag ==1)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);

// check Armstrong number


flag= checkArmstrongNumber(n);
if(flag ==1)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number.", n);
getch();
}

int checkPrimeNumber(int n)
{
int i, flag =1;
for(i =2; i <= n /2;++i){
// condition for non-prime number
if(n % i ==0){
flag=0;
break;
}
}
return flag;
}

23
int checkArmstrongNumber(intnum)
{
int original, rem, result =0, n =0, flag;
original=num;
while(original !=0){
original/=10;
++n;
}
original=num;
while(original !=0)
{
rem= original %10;
result+=pow(rem, n);
original/=10;
}
// condition for Armstrong number
if(result ==num)
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. Write a C program to calculate the power using recursion.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
int power(int n1,int n2);
void main()
{
int base, a, result;
clrscr();
printf("Enter base number: ");
scanf("%d",&base);
printf("Enter power number(positive integer): ");
scanf("%d",&a);
result= power(base, a);
printf("%d^%d = %d",base, a, result);
getch();
}

24
int power(intbase,int a)
{
if(a !=0)
return(base* power(base, a -1));
else
return1;
}

OUTPUT:-

Enter base number: 3


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

30. Write a C Program to Find G.C.D Using Recursion.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
int hcf(int n1,int n2);
void main()
{
int n1, n2;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
printf("G.C.D of %d and %d is %d.", n1, n2,hcf(n1, n2));
getch();
}
inthcf(int n1,int n2)
{
if(n2 !=0)
returnhcf(n2, n1 % n2);
else
return n1;
}

OUTPUT:-

Enter two positive integers: 366


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

25
31. Write a C Program to Calculate Average Using Arrays.
/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
float num[100], sum =0.0,avg;
clrscr();
printf("Enter the numbers of elements: ");
scanf("%d",&n);

while(n >100|| n <1)


{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d",&n);
}

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


{
printf("%d. Enter number: ", i +1);
scanf("%f",&num[i]);
sum+=num[i];
}

avg= sum / n;
printf("Average = %.2f",avg);
getch();
}

OUTPUT:-

Enter the numbers of elements: 6


1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

26
32. Write a C Program to Find Largest Element in an Array.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float arr[100];
clrscr();
printf("Enter the number of elements (1 to 100): ");
scanf("%d",&n);
for(i =0; i < n;++i)
{
printf("Enter number%d: ", i +1);
scanf("%f",&arr[i]);
}
// storing the largest number to arr[0]
for(i =1; i < n;++i)
{
if(arr[0]<arr[i])
arr[0]=arr[i];
}

printf("Largest element = %.2f",arr[0]);


getch();
}

OUTPUT:-

Enter the number of elements (1 to 100): 5


Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70

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

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
clrscr();
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d",&r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d",&c);

printf("\nEnter elements of 1st matrix:\n");


for(i =0; i < r;++i)
for(j =0; j < c;++j)
{
printf("Enter element a%d%d: ", i +1, j +1);
scanf("%d",&a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for(i =0; i < r;++i)
for(j =0; j < c;++j)
{
printf("Enter element a%d%d: ", i +1, j +1);
scanf("%d",&b[i][j]);
}

// adding two matrices


for(i =0; i < r;++i)
for(j =0; j < c;++j)
{
sum[i][j]= a[i][j]+ b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for(i =0; i < r;++i)
for(j =0; j < c;++j)
{
printf("%d ", sum[i][j]);
if(j == c -1)
{
printf("\n\n");
}
}
getch();
}

28
OUTPUT:-

Enter the number of rows (between 1 and 100): 2


Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element a11: -4
Enter element a12: 5
Enter element a13: 3
Enter element a21: 5
Enter element a22: 6
Enter element a23: 3

Sum of two matrices:


-2 8 7

10 8 6
34. Write a C Program to Find the Length of a String.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char s[]="Programming is fun";
int i;
crlscr();
for(i =0; s[i]!='\0';++i);

printf("Length of the string: %d", i);


getch();
}

OUTPUT:-

Length of the string: 18

29
// C program to find the length of
// string using strlen function

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
Char Str[1000];
int i;
clrscr();
printf("Enter the String: ");
scanf("%s", Str);
printf("Length of Str is %ld", strlen(Str));
getch();
}

OUTPUT:-

Enter the String: JDPC


Length of Str is 4

35. Write a C Program to Concatenate Two Strings.


/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[100], b[100];
clrscr();
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
}

30
OUTPUT:-

Enter the first string


Jagruti
Enter the second string
College
String obtained on concatenation is JagrutiCollege

Concatenate Two Strings Without Using strcat()


/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100]="programming ", s2[]="is awesome";
int i,j;
clrscr();
// length of s1 is stored in i
for(i =0; s1[i]!='\0';++i)
{
printf("i = %d\n", i);
}

// concatenating each character of s2 to s1


for(j =0; s2[j]!='\0';++j,++i)
{
s1[i]= s2[j];
}

// terminating s1 string
s1[i]='\0';

printf("After concatenation: ");


puts(s1);

getch();
}

OUTPUT:-

After concatenation: programming is awesome

31
36. Write a C Program to Copy String without usingstrcpy ().

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.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];
}
s2[i]='\0';
printf("String s2: %s", s2);
getch();
}

OUTPUT:-

Enter string s1: Hello this is my string.


String s2: Hello this is my string.
37. Write a C Program to Count the Number of Vowels, Consonants and so on.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char line[150];
clrscr();
int vowels, consonant, digit, space;

vowels= consonant = digit = space =0;

printf("Enter a line of string: ");


fgets(line,sizeof(line),stdin);

for(int i =0; line[i]!='\0';++i)


{

32
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'))
{
++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. Write a C Program to Find the Frequency of Characters in a String.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
char str[1000], ch;
int count =0;
clrscr();
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);

printf("Enter a character to find its frequency: ");


scanf("%c",&ch);

33
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 is jagruti degree and pg college.


Enter a character to find its frequency: e
Frequency of e = 5

39. Write a C Program to Access Array Elements Using Pointers.

/*
Name:
H.T No:
Section:
Aim:
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int data[5];
clrscr();
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));
getch();
}

OUTPUT:-

Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4

34
40. Write a C program to create, initialize, assign and access a pointer variable.

/*
Name:
H.T No:
Section:
Aim:
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num;/*declaration of integer variable*/
int *pNum;/*declaration of integer pointer*/
clrscr();
pNum=&num;/*assigning address of num*/
num=100;/*assigning 100 to variable num*/

//access value and address using variable num


printf("Using variable num:\n");
printf("value of num: %d\naddress of num: %u\n",num,&num);
//access value and address using pointer variable num
printf("Using pointer variable:\n");
printf("value of num: %d\naddress of num: %u\n",*pNum,pNum);

getch();
}

OUTPUT:-

Using variable num:


value of num: 100
address of num: 2764564284
Using pointer variable:
value of num: 100
address o fnum: 2764564284

35
41. Write a C program to swap two numbers using pointers
/*
Name:
H.T No:
Section:
Aim:
*/

#include <stdio.h>

// function to swap the two numbers


void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}

void main()
{
int num1,num2;

printf("Enter value of num1: ");


scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);

//displaying numbers before swapping


printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

//calling the user defined function swap()


swap(&num1,&num2);

//displaying numbers after swapping


printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

return 0;
}

OUTPUT:

36
42.Write a C program to count vowels and consonants in a string using pointer?

#include <stdio.h>
void main()
{ char str[100];
char *p;
int vCount=0,cCount=0;

printf("Enter any string: ");


fgets(str, 100, stdin);

//assign base address of char array to pointer


p=str;
//'\0' signifies end of the string
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++;
//increase the pointer, to point next character
p++;
}

printf("Number of Vowels in String: %d\n",vCount);


printf("Number of Consonants in String: %d",cCount);
return 0;
}

Output:

37
43. Write a C Program to Store Information of a Student Using Structure?

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];

void main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output
Enter information of students:

For roll number1,


Enter name: Tom
Enter marks: 98

For roll number2,


Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:

Roll number: 1
Name: Tom
Marks: 98
.
.

38
44. Write a C Program to Add Two Distances (in inch-feet system) using Structures.?
#include <stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, result;

void main() {
// take first distance input
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);

// take second distance input


printf("\nEnter 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);

// adding distances
result.feet = d1.feet + d2.feet;
result.inch = d1.inch + d2.inch;

// convert inches to feet if greater than 12


while (result.inch >= 12.0) {
result.inch = result.inch - 12.0;
++result.feet;
}
printf("\nSum of distances = %d\'-%.1f\"", result.feet,
result.inch);
return 0;
}
Output
Enter 1st distance
Enter feet: 23
Enter inch: 8.6

Enter 2nd distance


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

39
45. Write a C Program to Store Information of Students Using Structure?
46. Write a C program to declare, initialize an union?
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;

void main() {
j.salary = 12.3;

// when j.workerNo is assigned a value,


// j.salary will no longer hold 12.3
j.workerNo = 100;

printf("Salary = %.1f\n", j.salary);


printf("Number of workers = %d", j.workerNo);
return 0;
}
Output
Salary = 0.0
Number of workers = 100

40
47. Write a C++ program to implement function overloading?
#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);

int mul(int a,int b)


{
return a*b;
}
float mul(double x, int y)
{
return x*y;
}
void main()
{
int r1 = mul(6,7);
float r2 = mul(0.2,3);
std::cout << "r1 is : " <<r1<< std::endl;
std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
}

Output:

r1 is : 42
r2 is : 0.6

41
48. Write a C++ program to calculate an area of rectangle using encapsulation?

#include <iostream>
using namespace std;

class Rectangle {
public:
// Variables required for area calculation
int length;
int breadth;

// Constructor to initialize variables


Rectangle(int len, int brth) : length(len), breadth(brth) {}

// Function to calculate area


int getArea() {
return length * breadth;
}
};

void main() {
// Create object of Rectangle class
Rectangle rect(8, 6);

// Call getArea() function


cout << "Area = " << rect.getArea();

return 0;
}

Output
Area = 48

42
49. Write a C++ program to add two numbers using data abstraction?
#include <iostream>
using namespace std;

void main() {

int first_number, second_number, sum;

cout << "Enter two integers: ";


cin >> first_number >> second_number;

// sum of two numbers in stored in variable sumOfTwoNumbers


sum = first_number + second_number;

// prints sum
cout << first_number << " + " << second_number << " = " <<
sum;

return 0;
}
Run Code
Output
Enter two integers: 4
5
4 + 5 = 9

43
50.Write a C++ program to overload binary operators?
// C++ program to overload the binary operator +
// This program adds two complex numbers

#include <iostream>
using namespace std;

class Complex {
private:
float real;
float imag;

public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}

void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}

// Overload the + operator


Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}

void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag <<
"i";
else
cout << "Output Complex number: " << real << "+" <<
imag << "i";
}
};

int main() {
Complex complex1, complex2, result;

cout << "Enter first complex number:\n";


complex1.input();

cout << "Enter second complex number:\n";


complex2.input();

44
// complex1 calls the operator function
// complex2 is passed as an argument to the function
result = complex1 + complex2;
result.output();

return 0;
}
Run Code
Output
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i

45

You might also like