Sei sulla pagina 1di 76

1. Write a program to print Welcome to the world of programming.

#include <stdio.h>

#include <conio.h>

int main()

clrscr();

printf(Welcome to the world of programming);

return 0;

Output

Welcome to the World of Programming

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

2. Write a program to calculate the sum of two given numbers.

#include <stdio.h>

#include <conio.h>

int main()

int num1= 3, num2= 4, sum=0;

clrscr();

sum = num1 + num2;

printf(\n Sum = %d, sum);

return 0;

}
Output

Sum = 7

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

3. Write a program to perform addition, subtraction, division,


integer division, multiplication, and modulo division on two integer
numbers.

#include <stdio.h>

#include <conio.h>

int main()

int num1, num2;

int add_res=0, sub_res=0, mul_res=0, idiv_res=0, modiv_res=0;

float fdiv_res=0.0;

clrscr();

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

add_res = num1+num2;

sub_res = num1num2;

mul_res = num1*num2;

idiv_res = num1/num2;

modiv_res = num1%num2;

fdiv_res = (float)num1/num2;
printf(\n %d + %d = %d, num1, num2, add_res);

printf(\n %d - %d = %d, num1, num2, sub_res);

printf(\n %d %d = %d, num1, num2, mul_res);

printf(\n %d / %d = %d (Integer Division), num1, num2,


idiv_res);

printf(\n %d % %d = %d (Moduluo Division), num1, num2,


modiv_res);

printf(\n %d / %d = %.2f (Normal Division), num1, num2,


fdiv_res);

return 0;

Output

Enter the first number : 10

Enter the first number : 5

10 + 5 = 15

10 5 = 5

10 5 = 50

10 / 5 = 2 (Integer Division)

10 % 5 = 0 (Modulo Division)

10 / 5 = 2.00 (Normal Division)

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

4. Write a program to print the ASCII value of a character.

#include <stdio.h>

#include <conio.h>

int main()

{
char ch;

clrscr();

printf(\n Enter any character : );

scanf(%c, &ch);

printf(\n The ASCII value of %c is : %d, ch, ch);

return 0;

Output

Enter any character : a

The ASCII value of a is : 97

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

5. Write a program to read a character in upper case and then print


it in lower case.

#include <stdio.h>

#include <conio.h>

int main()

char ch;

clrscr();

printf(\n Enter any character in upper case : );

scanf(%c, &ch);

printf(\n The character in lower case is : %c, ch+32);


return 0;

Output
Enter any character in upper case : Z
The character in lower case is : z

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

6. Write a program to swap two numbers using a temporary variable.

#include <stdio.h>

#include <conio.h>

int main()

int num1, num2, temp;

clrscr();

printf(\n Enter the first number : );

scanf(%d,&num1);

printf(\n Enter the second number : );

scanf(%d,&num2);

temp = num1;

num1=num2;

num2=temp;

printf(\n The first number is %d, num1);

printf(\n The second number is %d, num2);

return 0;
}

Output

Enter the first number : 5

Enter the second number : 3

The first number is 3

The second number is 5

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

7. Write a program to swap two numbers without using a temporary


variable.

#include <stdio.h>

#include <conio.h>

int main()

int num1, num2;

clrscr();

printf(\n Enter the first number : );

scanf(%d,&num1);

printf(\n Enter the second number : );

scanf(%d,&num2);

num1 = num1 + num2;

num2= num1 num2;

num1 = num1 num2;


printf(\n The first number is %d, num1);

printf(\n The second number is %d, num2);

return 0;

Output

Enter the first number : 5

Enter the second number : 3

The first number is 3

The second number is 5

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

8. Write a program that displays the size of every data type.

#include <stdio.h>

#include <conio.h>

int main()

clrscr();

printf(\n The size of short integer is : %d, sizeof(short


int));

printf(\n The size of unsigned integer is : %d,


sizeof(unsigned int));

printf(\n The size of signed integer is : %d, sizeof(signed


int));

printf(\n The size of integer is : %d, sizeof(int));

printf(\n The size of long integer is : %d, sizeof(long


int));
printf(\n The size of character is : %d, sizeof(char));

printf(\n The size of unsigned character is : %d,


sizeof(unsigned char));

printf(\n The size of signed character is : %d,


sizeof(signed char));

printf(\n The size of floating point number is : %d,


sizeof(float));

printf(\n The size of double number is : %d,


sizeof(double));

return 0;

Output

The size of short integer is : 2

The size of unsigned integer is : 2

The size of signed integer is : 2

The size of integer is : 2

The size of long integer is : 4

The size of character is : 1

The size of unsigned character is : 1

The size of signed character is : 1

The size of floating point number is : 4

The size of double number is : 8

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

9. Write a program to convert a floating point number into the


corresponding integer.

#include <stdio.h>
#include <conio.h>

int main()

float f_num;

int i_num;

clrscr();

printf(\n Enter any floating point number: );

scanf(%f, &f_num);

i_num = (int)f_num;

printf(\n The integer variant of %f is = %d, f_num, i_num);

return 0;

Output

Enter any floating point number: 3.14

The integer variant of 3.14 is = 3

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

10. Write a program to test whether a number is positive, negative


or equal to zero.

#include <stdio.h>

#include <conio.h>

int main()

int num;

clrscr();

printf(\n Enter any number : );

scanf(%d,&num);
if(num>0)

printf(\n %d is positive, num);

else

if(num<0)

printf(\n %d is negative, num);

else

printf(\n The number is equal to zero);

return 0;

Output

Enter any number : -5

-5 is negative

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

11. Write a program to find whether a given year is leap year or


not.

#include <stdio.h>

#include <conio.h>

int main()

int year;

clrscr();

printf(\n Enter any year : );

scanf(%d, &year);
if(year%4 == 0)

printf(\n Leap Year);

else

printf(\n Not A Leap Year);

return 0;

Output

Enter any year : 2008

Leap Year

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

12. Write a program to find the greatest number from three numbers.

#include <stdio.h>

#include <conio.h>

int main()

int num1, num2, num3, big=0;

clrscr();

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

printf(\n Enter the third number : );

scanf(%d, &num3);

if(num1>num2)
{

if(num1>num3)

printf(\n %d is greater than %d and %d, num1,


num2, num3);

else

printf(\n %d is greater than %d and %d, num3,


num1, num2);

else if(num2>num3)

printf(\n %d is greater than %d and %d, num2, num1,


num3);

else

printf(\n %d is greater than %d and %d, num3, num1,


num2);

return 0;

Output

Enter the first number : 5

Enter the second number : 15

Enter the third number : 10

15 is greater than 5 and 10

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

13. Write a program to input three numbers and then find largest of
them using && operator.

#include <stdio.h>

#include <conio.h>
int main()

int num1, num2, num3;

clrscr();

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

printf(\n Enter the third number : );

scanf(%d, &num3);

if(num1>num2 && num1>num3)

printf(\n %d is the largest number, num1);

if(num2>num1 && num2>num3)

printf(\n %d is the largest number, num2);

else

printf(\n %d is the largest number, num3);

getch();

return 0;

Output

Enter the first number : 5

Enter the second number : 15

Enter the third number : 10

15 is the largest number

********************************************************************
*****
14. Write a program to find largest of two numbers using ternary
operator.

#include <stdio.h>

#include <conio.h>

int main()

int num1, num2, large;

clrscr();

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

large = num1>num2?num1:num2;

printf(\n The largest number is : %d, large);

return 0;

Output

Enter the first number : 5

Enter the second number : 15

The largest number is : 15

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

15. Write a program to find largest of three numbers using ternary


operator.
#include<stdio.h>

#include<conio.h>

int main()

int num1, num2, num3, large;

clrscr();

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

printf(\n Enter the third number : );

scanf(%d, &num3);

large = num1>num2?(num1>num3?num1:num3):(num2>num3?num2:num3);

printf(\n The largest number is : %d, large);

return 0;

Output

Enter the first number : 5

Enter the second number : 15

Enter the third number : 10

The largest number is : 15

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

16. Write a program to take input from the user and then check
whether it is a number or a character. If it is a character,
determine whether it is in upper case or lower case.

#include <stdio.h>
#include <conio.h>

int main()

char ch;

clrscr();

printf(\n Enter any character : );

scanf(%c, &ch);

if(ch >=A && ch<=Z)

printf(\n Upper case character was entered);

if(ch >=a && ch<=z)

printf(\n Lower case character was entered);

else if(ch>=0 && ch<=9)

printf(\n You entered a number);

return 0;

Output

Enter any character : g

Lower case character was entered

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

17. Write a program to enter any character. If the entered character


is in lower case then convert it into upper case and if it is a
lower case character then convert it into upper case.

#include <stdio.h>
#include <conio.h>

int main()

char ch;

clrscr();

printf(\n Enter any character : );

scanf(%c, &ch);

if(ch >=A && ch<=Z)

printf(\n The entered character was in upper case. In


lower case it is : %c, (ch+32));

else

printf(\n The entered character was in lower case. In


upper case it is : %c, (ch-32));

return 0;

Output

Enter any character : g

The entered character was in lower case. In upper case it is : G

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

18. Write a program to enter the marks of a student in four


subjects. Then calculate the total, aggregate and display the grade
obtained by the student. If the student scores aggregate is greater
than 75%, then the grade is Distinction. If aggregate is 60>= and
<75, then the grade is First Division. If aggregate is 50>= and <60,
then the grade is Second Division. If aggregate is 40>= and <50,
then the grade is Third Division. Else the grade is Fail.
#include <stdio.h>

#include <conio.h>

int marks1, marks2, marks3, marks4, total = 0;

float avg =0.0;

clrscr();

printf(\n Enter the marks in Mathematics : );

scanf(%d, &marks1);

printf(\n Enter the marks in Science : );

scanf(%d, &marks2);

printf(\n Enter the marks in Social Science : );

scanf(%d, &marks3);

printf(\n Enter the marks in Computers : );

scanf(%d, &marks4);

total = marks1 + marks2 + marks3 + marks4;

avg = total/4;

printf(\n Total = %d, total);

printf(\n Aggregate = %f, avg);

if(avg >= 75)

printf(\n DISTINCTION);

if(avg>=60 && avg<75)

printf(\n FIRST DIVISION);

if(avg>=50 && avg<60)

printf(\n SECOND DIVISION);

if(avg>=40 && avg<50)

printf(\n THIRD DIVISION);

else
printf(\n FAIL);

return 0;

Output

Enter the marks in Mathematics : 85

Enter the marks in Science : 90

Enter the marks in Social Science : 75

Enter the marks in Computers : 100

Total = 350

Aggregate = 87.5

DISTINCTION

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

19. Write a program to enter a number from 1-7 and display the
corresponding day of the week using switch case statement.

#include <stdio.h>

#include <conio.h>

int main()

int day;

clrscr();

printf(\n Enter any number from 1 to 7 : );

scanf(%d, &day);

switch(day)

case 1: printf(\n SUNDAY);


break;

case 2 : printf(\n MONDAY);

break;

case 3 : printf(\n TUESDAY);

break;

case 4 : printf(\n WEDNESDAY);

break;

case 5 : printf(\n THURSDAY);

break;

case 6 : printf(\n FRIDAY);

break;

case 7 : printf(\n SATURDAY);

break;

default: printf(\n Wrong Number);

return 0;

Output

Enter any number from 1 to 7 : 5

THURSDAY

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

20. Write a program to display the largest of 10 numbers using


ternary operator.

#include <stdio.h>

#include <conio.h>

int main()

{
int i=0, large = -1, num;

clrscr();

while(i<10)

printf(\n Enter the number : );

scanf(%d,&num);

large = num>large ? num:large;

i++;

printf(\n The largest of ten numbers entered is : %d,


large);

return 0;

Output

Enter the number : 10

Enter the number : 1

Enter the number : 9

Enter the number : 7

Enter the number : 8

Enter the number : 6

Enter the number : 4

Enter the number : 5

Enter the number : 3

Enter the number : 2

The largest of ten numbers entered is : 10

********************************************************************
*****
21. Write a program to read the numbers until -1 is encountered.
Also count the negative, positive and zeroes entered by the user.

#include <stdio.h>

#include <conio.h>

int main()

int num;

int negatives=0, positives=0, zeroes=0;

clrscr();

printf(\n Enter -1 to exit.);

printf(\n\n Enter any number : );

scanf(%d, &num);

while(num != -1)

if(num>0)

positives++;

else if(num<0)

negatives++;

else

zeroes++;

printf(\n\n Enter any number : );

scanf(%d, &num);

printf(\n Count of positive numbers entered = %d,


positives);
printf(\n Count of negative numbers entered = %d,
negatives);

printf(\n Count of zeroes entered = %d, zeroes);

getch();

return 0;

Output

Enter -1 to exit.

Enter any number : 10

Enter any number : -10

Enter any number : 5

Enter any number : 0

Enter any number : -1

Count of positive numbers entered = 2

Count of negative numbers entered = 1

Count of zeroes entered = 1

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

22. Write a program using do-while loop to display the square and
cube of first n natural numbers.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

int i, n;
clrscr();

printf(\n Enter the value of n : );

scanf(%d, &n);

printf(\n
-----------------------------------------------------------);

i=1;

do

printf(\n | \t %d \t | \t %d \t | \t %ld \t |, i,
pow(i,2), pow(i,3));

i++;

} while(i<=n);

printf(\n
-----------------------------------------------------------);

return 0;

Output

Enter the value of n : 2

-----------------------------------------------------------

| 1 | 1| 1 |

| 2 | 4| 8 |

-----------------------------------------------------------

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

23. Write a program to list all the leap years from 1900 to 2100.

#include <stdio.h>
#include <conio.h>

int main()

int m=1900, n=2100;

clrscr();

do

if(m%4 == 0)

printf(\n %d is a leap year, m);

else

printf(\n %d is not a leap year, m);

m = m+1;

}while(m<=n);

return 0;

Output

1900 is a leap year

1901 is not a leap year

2100 is a leap year

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

24. Write a program to read a character until a * is encountered.


Also count the number of upper case, lower case characters and
numeric digits entered by the users.
#include <stdio.h>

#include <conio.h>

int main()

char ch;

int lowers = 0, uppers = 0, numbers = 0;

clrscr();

printf(\n Enter any character : );

scanf(%c, &ch);

do

if(ch >=A && ch<=Z)

uppers++;

if(ch >=a && ch<=z)

lowers++;

if(ch >=0 && ch<=9)

numbers++;

printf(\n Enter another character. Enter * to exit.);

scanf(%c, &ch);

} while(ch != *);

printf(\n Total count of lower case characters entered = %d,


lowers);

printf(\n Total count of upper case characters entered = %d,


uppers);

printf(\n Total count of numeric digits entered = %d,


numbers);
return 0;

Output

Enter any character : s

Enter another character. Enter * to exit. 2

Enter another character. Enter * to exit. Z

Enter another character. Enter * to exit. *

Total count of lower case characters entered = 1

Total count of upper case characters entered = 1

Total count of numeric digits entered = 1

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

25. Write a program to read the numbers until -1 is encountered.


Also calculate the sum and mean of all positive numbers entered and
the sum and mean of all negative numbers entered separately.

#include <stdio.h>

#include <conio.h>

int main()

int num;

int sum_negatives=0, sum_positives=0;

int positives = 0, negatives = 0;

float mean_positives = 0.0, mean_negatives = 0.0;

clrscr();

printf(\n Enter -1 to exit.);

printf(\n\n Enter any number : );


scanf(%d, &num);

do

if(num>0)

sum_positives += num;

positives++;

else if(num<0)

sum_negatives += num;

negatives++;

printf(\n\n Enter any number : );

scanf(%d,&num);

} while(num != -1);

mean_positives = sum_positives/positives;

mean_negatives = mean_neagtives/negatives;

printf(\n Sum of all positive numbers entered = %d,


sum_positives);

printf(\n Mean of all positive numbers entered = %d,


mean_positives);

printf(\n Sum of all negative numbers entered = %d,


sum_negatives);
printf(\n Mean of all negative numbers entered = %d,
mean_negatives);

return 0;

Output

Enter -1 to exit.

Enter any number : 89

Enter any number : 99

Enter any number : -1

Sum of all positive numbers entered = 188

Mean of all positive numbers entered = 94

Sum of all negative numbers entered = 0

Mean of all negative numbers entered = 0

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

26. Write a program using for loop to print all the numbers from m
to n and classifying them as even or odd.

#include <stdio.h>

#include <conio.h>

int main()

int i, m, n;

clrscr();

printf(\n Enter the value of m : );

scanf(%d, &m);

printf(\n Enter the value n : );


scanf(%d, &n);

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

if(i%2 == 0)

printf(\n %d is even, i);

else

printf(\n %d is odd, i);

return 0;

Output

Enter the value of m : 3

Enter the value n : 5

3 is odd

4 is even

5 is odd

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

27. Write a program using for loop to calculate the factorial of a


number.

#include <stdio.h>

#include <conio.h>

int main()

int fact = 1, num;

clrscr();
printf(\n Enter the number : );

scanf(%d, &num);

if(num == 0)

fact = 1;

else

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

fact = fact * num;

printf(\n Factorial of %d is %d : , num, fact);

return 0;

Output

Enter the number : 5

Factorial of 5 is 120

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

28. Write a program using while loop to read the numbers until -1 is
encountered. Also classify the entered number as positive, negative
or zero.

#include <stdio.h>

#include <conio.h>

int main()

int num;

clrscr();
printf(\n Enter -1 to exit.);

printf(\n\n Enter any number : );

scanf(%d,&num);

while(num != -1)

if(num>0)

printf(\n %d is POSITIVE,num);

else if(num<0)

printf(\n %d is NEGATIVE, num);

else

printf(\n ZERO is neither positive nor negative);

printf(\n\n Enter any number : );

scanf(%d, &num);

return 0;

Output

Enter -1 to exit.

Enter any number : 98

98 is POSITIVE

Enter any number : -1

********************************************************************
*****
29. Write a program using do-while loop to read the numbers until -1
is encountered. Also count the number of prime numbers and composite
numbers entered by the user.

#include <stdio.h>

#include <conio.h>

int main()

int num, i;

int primes=0, composites=0, flag=0;

clrscr();

printf(\n Enter -1 to exit.);

printf(\n\n Enter any number : );

scanf(%d,&num);

do

for(i=2;i<=num%2;i++)

if(num%i==0)

flag=1;

break;

if(flag==0)

primes++;

else

composites++:
flag=0;

printf(\n\n Enter any number : );

scanf(%d, &num);

} while(num != -1);

printf(\n Count of prime numbers entered = %d, primes);

printf(\n Count of composite numbers entered = %d,


composites);

return 0;

Output

Enter -1 to exit.

Enter any number : 67

Enter any number : 99

Enter any number : -1

Count of prime numbers entered = 1

Count of composite numbers entered = 1

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

30. Write a program to calculate pow(x,n).

#include <stdio.h>

#include <conio.h>

int main()

int i, num, n;

long int result =1;

clrscr();
printf(\n Enter the number : );

scanf(%d, &num);

printf(\n Till which power to calculate : );

scanf(%d, &n);

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

result = result * num;

printf(\n pow(%d, %d) = %ld, num, n, result);

return 0;

Output

Enter the number : 2

Till which power to calculate : 5

pow(2, 5) = 32

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

31. Write a program to print the reverse of a number.

#include <stdio.h>

#include <conio.h>

int main()

int num, temp;

clrscr();
printf(\n Enter the number : );

scanf(%d, &num);

printf(\n The reversed number is : );

while(num != 0)

temp = num%10;

printf(%d,temp);

num = num/10;

return 0;

Output

Enter the number : 256

The reversed number is : 652

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

32. Write a program to enter a number and then calculate the sum of
its digits.

#include <stdio.h>

#include <conio.h>

int main()

int num, temp, sumofdigits = 0;

clrscr();

printf(\n Enter the number : );

scanf(%d, &num);
while(num != 0)

temp = num%10;

sumofdigits += temp;

num = num/10;

printf(\n The sum of digits = %d, sumofdigits);

return 0;

Output

Enter the number: 524

The sum of digits = 11

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

33. Write a program to enter a decimal number. Calculate and display


the binary equivalent of this number

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

int decimal_num, remainder, binary_num = 0, i =0;

clrscr();

printf(\n Enter the decimal number : );


scanf(%d, &decimal_num);

while(decimal_num != 0)

remainder = decimal_num%2;

binary_num += remainder*pow(10,i);

decimal_num = decimal_num/2;

i++;

printf(\n The binary equivalent of %d is = %d, decimal_num,


binary_num);

return 0;

Output

Enter the decimal number : 10

The binary equivalent of 10 is = 1010

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

34. Write a program to enter a decimal number. Calculate and display


the octal equivalent of this number.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

int decimal_num, remainder, octal_num= 0, i =0;

clrscr();
printf(\n Enter the decimal number : );

scanf(%d, &decimal_num);

while(decimal_num != 0)

remainder = decimal_num%8;

octal_num += remainder*pow(10,i);

decimal_num = decimal_num/8;

i++;

printf(\n The octal equivalent of %d is = %d, decimal_num,


octal_num);

return 0;

Output

Enter the decimal number : 10

The octal equivalent of 10 is = 12

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

35. Write a program to enter a decimal number. Calculate and display


the hexadecimal equivalent of this number

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{
int decimal_num, hex_num =0 , i =0, remainder;

clrscr();

printf(\n Enter the decimal number : );

scanf(%d, &decimal_num);

while(decimal_num != 0)

remainder = decimal_num%16;

hex_num += remainder*pow(10,i);

decimal_num = decimal_num/16;

i++;

printf(\n The hexadecimal equivalent of %d is = %d,


decimal_num, hex_num);

return 0;

Output

Enter the decimal number : 20

The hexadecimal equivalent of 20 is = 14

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

36. Write a program to enter a binary number. Calculate and display


the decimal equivalent of this number

#include <stdio.h>

#include <conio.h>

#include <math.h>
int main()

int decimal_num = 0, remainder, binary_num, i =0;

clrscr();

printf(\n Enter the binary number : );

scanf(%d, &binary_num);

while(binary_num != 0)

remainder = binary_num%10;

decimal_num += remainder*pow(2,i);

binary_num = binary_num/10;

i++;

printf(\n The decimal equivalent of %d is = %d, binary_num,


decimal_num);

return 0;

Output

Enter the binary number : 101

The decimal equivalent of 101 is = 5

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

37. Write a program to enter an octal number. Calculate and display


the decimal equivalent of this number.

#include <stdio.h>

#include <conio.h>
#include <math.h>

int main()

int decimal_num= 0, remainder, octal_num, i =0;

clrscr();

printf(\n Enter the octal number : );

scanf(%d, &octal_num);

while(octal_num != 0)

remainder = octal_num%10;

decimal_num += remainder*pow(8,i);

octal_num = octal_num/10;

i++;

printf(\n The decimal equivalent of %d is = %d, octal_num,


decimal_num);

return 0;

Output

Enter the octal number : 15

The decimal equivalent of 15 is = 13

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

38. Write a program to enter a hexadecimal number. Calculate and


display the decimal equivalent of this number.
#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

int decimal_num= 0, remainder, hex_num, i =0;

clrscr();

printf(\n Enter the hexadecimal number : );

scanf(%d, &hex_num);

while(hex_num != 0)

remainder = hex_num%10;

decimal_num += remainder*pow(16,i);

hex_num = hex_num/10;

i++;

printf(\n The decimal equivalent of %d is = %d, hex_num,


decimal_num);

return 0;

Output

Enter the hexadecimal number : 19

The decimal equivalent of 19 is = 25

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

39. Write a program to calculate GCD of two numbers.


#include <stdio.h>

#include <conio.h>

int main()

int num1, num2, temp;

int dividend, divisor, remainder;

clrscr();

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

if(num1>num2)

dividend = num1;

divisor = num2;

else

dividend = num2;

divisor = num1;

while(divisor)

remainder = dividend%divisor;

dividend = divisor;
divisor = remainder;

printf(\n GCD of %d and %d is = %d, num1, num2, dividend);

return 0;

Output

Enter the first number : 4

Enter the second number : 8

GCD of 4 and 8 is = 4

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

40. Write a program to calculate the sum of the series 1+ 1/2


+........ +1/n.

#include <stdio.h>

#include <conio.h>

int main()

int n;

float sum=0.0, a, i;

clrscr();

printf(\n Enter the value of n : );

scanf(%d, &n);

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

{ a=1/i;

sum = sum +a;


}

printf(\n The sum of series 1/1 + 1/2 + .... + 1/%d = %f, n,


sum);

return 0;

Output

Enter the value of n : 4

The sum of series 1/1 + 1/2 + .... + 1/4 = 2.08

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

41. Write a program to calculate the sum of the series 1/12 + 1/ 22 +


. 1/n2.

#include <stdio.h>

#include <math.h>

#include <conio.h>

int main()

int n;

float sum=0.0, a, i;

clrscr();

printf(\n Enter the value of n : );

scanf(%d, &n);

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

{ a=1/pow(i,2);

sum = sum +a;

}
printf(\n The sum of series 1/12 + 1/ 22 + . 1/%d2 = %f, n,
sum);

return 0;

Output

Enter the value of n : 4

The sum of series 1/12 + 1/ 22 + . 1/42 = 1.4236

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

42. Write a program to calculate the sum of the series 1/2 + 2/3 +
. n/(n+1).

#include <stdio.h>

#include <conio.h>

int main()

int n;

float sum=0.0, a, i;

clrscr();

printf(\n Enter the value of n : );

scanf(%d, &n);

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

{ a= i/(i+1);

sum = sum +a;

printf(\n The sum of series 1/2 + 2/3 + .... + %d/%d = %f,


n, n+1,sum);

return 0;
}

Output

Enter the value of n : 4

The sum of series 1/2 + 2/3 + .... + 4/5 = 2.71

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

43. Write a program to calculate sum of cubes of numbers from 1 to


n.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

int i, n;

int term, sum = 0;

clrscr();

printf(\n Enter the value of n : );

scanf(%d, &n);

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

term = pow(i,3);

sum += term;

printf(\n 13 + 23 + 33 + .... = %d, sum);

return 0;

Output
Enter the value of n : 4

13 + 23 + 33 + .... = 100

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

44. Write a program to calculate the sum of squares of even numbers.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

int i, n;

int term, sum = 0;

clrscr();

printf(\n Enter the value of n : );

scanf(%d, &n);

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

if(i%2 == 0)

term = pow(i,2);

sum += term;

printf(\n 22 + 42 + 62 + .... = %d, sum);

return 0;

}
Output

Enter the value of n : 7

22 + 42 + 62 + .... = 56

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

45. Write a program to find whether the given number is an Armstrong


number or not.

#include <stdio.h>

#include <conio.h>

#include <math.h>

main()

int num, sum=0, r, n;

clrscr();

printf(\n Enter the number : );

scanf(%d, &num);

n=num;

while(n>0)

r=n%10;

sum += pow(r,3);

n=n/10;

if(sum==num)

printf(\n %d is an Armstrong number, num);

else

printf(\n %d is not an Armstrong number, num);


return 0;

Output

Enter the number: 371

371 is an Armstrong number

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

46. Write a program to print the multiplication table.

#include <stdio.h>

#include <conio.h>

int main()

int i, j;

clrscr();

printf(\n\t\t Multiplication table);

printf(\n
*******************************************************);

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

for(j=1;j<=10;j++)

printf(\t %d, (i*j));

printf(\n);

getch();

return 0;

}
Output

Multiplication table

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

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

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

47. Write a program to add two integers using functions.

#include <stdio.h>

int sum(int a, int b);

int main()

int num1, num2, total = 0;

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

total = sum(num1, num2);

printf(\n Total = %d, total);

return 0;

int sum (int a, int b)

{
int result;

result = a + b;

return result;

Output

Enter the first number : 10

Enter the first number : 5

Total = 15

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

48. Write a program to find largest of three integers using


functions.

#include <stdio.h>

int greater(int a, int b, int c);

int main()

int num1, num2, num3, large;

printf(\n Enter the first number : );

scanf(%d, &num1);

printf(\n Enter the second number : );

scanf(%d, &num2);

printf(\n Enter the third number : );

scanf(%d, &num3);

large = greater(num1, num2, num3);


printf(\n Largest number = %d, large);

return 0;

int greater(int a, int b, int c)

if(a>b && a>c)

return a;

if(b>a && b>c)

return b;

else

return c;

Output

Enter the first number : 5

Enter the second number : 15

Enter the third number : 10

Largest number = 15

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

49. Write a program to calculate area of a circle using function.

#include <stdio.h>

float cal_area(float r);

int main()

{
float area, radius;

printf(\n Enter the radius of the circle : );

scanf(%f, &radius);

area = cal_area(radius);

printf(\n Area of the circle with radius %f = %f, radius,


area);

return 0;

float cal_area(float radius)

return (3.14 * radius * radius);

Output

Enter the radius of the circle : 5

Area of the circle with radius 5 = 78.5

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

50. Write a program to calculate pow(x,y) using a recursive


function.

#include <stdio.h>

int pow_rec(int, int);

int main()

int num1, num2, res;

printf(\n Enter the two numbers : );


scanf(%d %d, &num1, &num2);

res = pow_rec(num1, num2);

printf(Pow(%d, %d) = %d, num1, num2, res);

return 0;

int pow_rec(int x, int y)

if( y==0)

return 1;

else

return (pow_rec(x, y-1) * x);

Output

Enter the two numbers : 2 3

Pow(2, 3) = 8

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

51. Write a program to print a character. Also print its ASCII value
and rewrite the character in upper case.

#include <stdio.h>

int main()

int ch, *pch;

pch = &ch;

printf(\n Enter the character : );

scanf(%c, &ch);
printf(\n The character that was entered is : %c, *pch);

printf(\n The ASCII value of the character is : %d, *pch);

printf(\n The character in upper case is : %c, *pch - 32);

return 0;

Output

Enter the character : s

The character that was entered is : s

The ASCII value of the character is : 115

The character in upper case is : S

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

52. Write a program to convert a floating point number into an


integer.

#include <stdio.h>

int main()

float fnum, *pfnum;

int num, *pnum;

pfnum = &fnum;

pnum= &num;

printf(\n Enter the floating point number : );

scanf(%f, &fnum);

*pnum = (int)*pfnum;

printf(\n The integer equivalent of %f = %d, *pfnum, *pnum);


return 0;

Output

Enter the floating point number : 3.14

The integer equivalent of 3.14 = 3

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

53. Write a program to enter a character and then determine whether


it is vowel or not.

#include <stdio.h>

int main()

char ch, *pch;

pch = &ch;

printf(\n Enter any character : );

scanf(%c, pch);

if(*pch=='a' || *pch =='e' || *pch=='i' || *pch=='o' ||


*pch=='u' ||

*pch=='A' || *pch=='E' || *pch=='I' || *pch=='O' ||


*pch=='U' )

printf(\n %c is a vowel, ch);

else

printf(\n %c is not a vowel);

return 0;
}

Output

Enter any character : s

s is not a vowel

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

54. Write a program to find mean of n numbers using arrays.

#include <stdio.h>

int main()

int i, n, arr[20], sum =0;

int *pn, *psum;

float mean = 0.0, *pmean;

pn = &n;

psum = &sum;

pmean = &mean;

printf(\n Enter the number of elements in the array : );

scanf(%d, pn);

for(i=0; i < *pn; i++)

printf(\n Enter the number: );

scanf(%d, arr + i);

}
for(i=0; i < *pn; i++)

*psum += *(arr + i);

*pmean = *psum / *pn;

printf(\n The numbers you entered are : );

for(i=0; i < *pn; i++)

printf(%d, *(arr + i));

printf(\n The sum of these numbers is : %d, *psum);

printf(\n The mean of these numbers is : %f, *pmean);

return 0;

Output

Enter the number of elements in the array : 5

Enter the number: 2

Enter the number: 3

Enter the number: 4

Enter the number: 5

Enter the number: 6

The numbers you entered are : 2 3 4 5 6

The sum of these numbers is : 20

The mean of these numbers is : 4

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

55. Write a program to read a text, delete all the semi-colons it


has and finally replace all , with a ..

#include <stdio.h>
int main()

char str[100], copy_str[100];

char *pstr, *pcopy_str;

pstr = str;

pcopy_str = copy_str;

printf(\n Enter the string : );

gets(str);

while( *pstr != \0)

if( *pstr = ;)

{ } // do nothing

if ( *pstr == ,)

*pcopy_str = .;

else

*pcopy_str = *pstr;

pstr++; pcopy_str++;

*pcopy_str = \0;

printf(\n The new text is : );

pcopy_str = copy_str;

while( *pcopy_str != \0)

printf(%c, *pcopy_str);

pcopy_str++;

}
return 0;

Output

Enter the string : hello; I am fine,

The new text is : hello I am fine.

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

56. Write a program to read and print an array of n numbers, then


find out the smallest number. Also print the position of the
smallest number

#include <stdio.h>

void read_array (int *arr, int n);

void find_small (int *arr, int n, int *small, int *pos);

int main()

int num[10], n, smallest=32767, pos;

printf(\n Enter the size of the array : );

scanf(%d, &n);

read_array(num, n);

find_small(num, n, &smallest, &pos);

printf(\n The smallest number in the array is = %d,


smallest);

printf(\n The position of the smallest number in the array is


= %d, pos);

return 0;

void read_array (int *arr, int n)


{

int i;

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

printf(\n array[%d] = , i);

scanf(%d, arr+i);

void find_small(int *arr, int n, int *small, int *pos)

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

if( *(arr+i)< *small)

*small = *(arr+i);

*pos = i;

Output

Enter the size of the array : 5

array[0] = 2

array[1] = 1

array[2] = 3

array[3] = 5

array[4] = 4

The smallest number in the array is = 1

The position of the smallest number in the array is = 1


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

57. Write a program to arrange the elements of an array in ascending


order.

#include <stdio.h>

void read_array (int *arr, int n);

void display_array (int *arr, int n);

void sort_array (int *arr, int n);

int main()

int num[10], n;

printf(\n Enter the size of the array : );

scanf(%d, &n);

read_array(num, n);

sort_array(num, n);

printf(\n The sorted array is:\n);

display_array(num, n);

return 0;

void read_array (int *arr, int n)

int i;

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

printf(\n array[%d] = , i);

scanf(%d, arr+i);

}
}

void display_array (int *arr, int n)

int i;

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

printf(\n array[%d] = %d, i, *(arr+i));

void sort_array(int *arr, int n)

int i, j, temp;

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

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

if( *(arr+i) > *(arr+j))

temp = *(arr+i);

*(arr+i) = *(arr+j);

*(arr+j) = temp;

Output

Enter the size of the array : 5

array[0] = 2

array[1] = 1
array[2] = 3

array[3] = 5

array[4] = 4

The sorted array is:

array[0] = 1

array[1] = 2

array[2] = 3

array[3] = 4

array[4] = 5

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

58. Write a menu driven program to read a string, display the


string, merge two strings, copy n characters from the mth position,
and calculate the length of the string.

#include <stdio.h>

void read_str( char *my_str);

void display( char *my_str);

void merge_str(char *my_str1, char *my_str2, char *my_str3);

void copy_str( char *my_str, char *copy_str, int m, int n);

int cal_len(char *my_str, int *len);

void count(char my_str[]);

void count_wlc(char *my_str);

void replace_str(char *my_str)

int main()

char str1[100], str2[100], merged_str[200], copy_str[100];

int option, m, n, length=0;


do

printf(\n 1. Enter the string);

printf(\n 2. Display the string);

printf(\n 3. Merge two strings);

printf(\n 4. Copy n characters from mth position);

printf(\n 5. Calculate length of the string);

printf(\n 6. Count the number of upper case, lower case,


numbers and special characters);

printf(\n 7. Count the number of words, lines and


characters);

printf(\n 8. Replace , with ;);

printf(\n 9. EXIT);

printf(\n\n Enter your option : );

scanf(%d, &option);

switch(option)

case 1:

printf(\n Enter the string : );

read_str(str1);

break;

case 2:

printf(\n The string is : );

display_str(str1);

break;

case 3:

printf(\n Enter the second string : );

read_str(str2);

merge_str(str1, str2, merged_str);


break;

case 4:

printf(\n Enter the position from which to


copy the text : );

scanf(%d, &m);

printf(\n Enter the number of characters to


be copied : );

scanf(%d, &n);

copy_str(str1, copy_str, m, n);

break;

case 5:

cal_len(str1, &length);

printf(\n The length of the string is : %d,


length);

break;

case 6:

count(str1);

break;

case 7:

count_wlc(str1);

break;

case 8:

replace_str(str1);

break;

}while (option != 9);

return 0;

void read_str( char *my_str)


{

printf(\n Enter the string : );

while(*my_str!= *)

scanf(%c, my_str);

my_str++;

*my_str = \0;

void display_str(char *my_str)

printf(\n The string is : );

while(*my_str != \0)

printf(%c, *my_str);

my_str++;

void merge_str(char *my_str1, char *my_str2, char *my_str3)

while(*my_str1 != \0)

*my_str3 = *my_str1;

my_str1++, my_str3++;

}
while(*my_str2 != \0)

*my_str3 = *my_str2;

my_str3++, my_str2++;

*my_str3 = \0;

display_str(my_str3);

void copy_str( char *my_str, char *copy_str, int m, int n)

int i =0;

my_str = my_str + m-1;

while (i<=n)

*copy_str = *my_str;

copy_str++, my_str++;

*copy_str[j] = \0;

display_str(copy_str);

int cal_len(char *my_str, int *len)

while(*my_str != \0)

*len++;

}
void count(char my_str[])

int upper_case = 0, lower_case = 0, numbers = 0, spcl_char =


0;

while ( *my_str != \0)

if ( *my_str >= A && *my_str <= Z)

upper_case++;

if ( *my_str >= a && *my_str <= z)

lower_case++;

if ( *my_str >= 0 && *my_str <= 9)

numbers++;

else

spcl_car++;

printf(\n Upper case character = %d, upper_case);

printf(\n Lower case character = %d, lower_case);

printf(\n Numbers = %d, numbers);

printf(\n Special characters = %d, spcl_char);

void count_wlc(char *my_str)

int words =0, lines =0, characters = 0;

while(*my_str != \0)

if (*my_str == \n)

lines++;
if (*my_str == && *(my_str+1) != )

words++;

characters++;

my_str++;

printf(\n Number of words = %d, words);

printf(\n Number of lines = %d, lines);

printf(\n Number of characters = %d, characters);

void replace_str(char *my_str)

while ( *my_str != \0)

if(*my_str == ,)

*my_str = ;;

my_str++;

display_str(my_str);

Output

1. Enter the string

2. Display the string

3. Merge two strings

4. Copy n characters from mth position

5. Calculate length of the string

6. Count the number of upper case, lower case, numbers and special
characters

7. Count the number of words, lines and characters


8. Replace , with ;

9. EXIT

Enter your option : 1

Enter the string : goransh

1. Enter the string

2. Display the string

3. Merge two strings

4. Copy n characters from mth position

5. Calculate length of the string

6. Count the number of upper case, lower case, numbers and special
characters

7. Count the number of words, lines and characters

8. Replace , with ;

9. EXIT

Enter your option : 2

The string is : goransh

1. Enter the string

2. Display the string

3. Merge two strings

4. Copy n characters from mth position

5. Calculate length of the string

6. Count the number of upper case, lower case, numbers and special
characters

7. Count the number of words, lines and characters

8. Replace , with ;

9. EXIT

Enter your option : 9


59. Write a program to copy the last n characters of a character
array in another character array. Also convert the lower case
letters into upper case letters while copying.

#include <stdio.h>

#include <string.h>

int main()

char str[100], copy_str[100];

char *pstr, *pcopy_str;

int i=0, n;

pstr = str;

pcopy_str = copy_str;

printf(\n Enter the string:);

gets(str);

printf(\n Enter the number of characters to be copied (from


the end): );

scanf(%d, &n);

pstr = pstr + strlen(str) n;

while(*pstr != \0)

*pcopy_str = *pstr - 32;

pstr++; pcopy_str++;

*pcopy_str = \0;

printf(\n The copied text is : );

puts(copy_str);
return 0;

Output

Enter the string: hello, how are you?

Enter the number of characters to be copied (from the end): 12

The copied text is : HOW ARE YOU?

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

60. Write a program to enter two points and then calculate the
distance between them.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

typedef struct

int x, y;

}point;

point p1, p2;

float distance;

clrscr();

printf(\n Enter the coordinates of the first point : );

scanf(%d %d, &p1.x, &p1.y);

printf(\n Enter the coordinates of the second point : );


scanf(%d %d, &p2.x, &p2.y);

distance = sqrt(pow((p2.x p1.x), 2) + pow((p2.y p1.y),


2));

printf(\n The coordinates of p1 are : %dx %dy, p1.x, p1.y);

printf(\n The coordinates of p2 are : %dx %dy, p2.x, p2.y);

printf(\n Distance between p1 and p2 = %.3f, distance);

getch();

return 0;

Output

Enter the coordinates of the first point : 2 3

Enter the coordinates of the second point : 4 5

The coordinates of p1 are : 2x 3y

The coordinates of p2 are : 4x 5y

Distance between p1 and p2 = 2.828

Potrebbero piacerti anche