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

Kashish c File (3)

Uploaded by

Lavanya Rawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Kashish c File (3)

Uploaded by

Lavanya Rawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 234

LAB FILE

Introduction to C –
Programming

Batch:(2023 – 26)
BCA 1st YEAR

SUBMITTED BY:
SUBMITTED TO:
Name –Lavanya Rawal MR.RISHI KUMAR
Student ID – 231522736. ASSISTANT
PROFESSOR
CSIT, GEU,
DEHRADU
INDEX
1. WAP for hello world or this is my first C Program.
2. WAP to add two numbers.
3. WAP to find area of circle.
4. WAP to divide two numbers.
5. WAP to print ASCII value.
6. WAP to multiply floating point numbers.
7. WAP to SWAP two vairables number by using third
variable.
8. WAP to SWAP two vairables number without using third
variable.
9. WAP to SWAP three vairable numbers without using
third variables.
10. Wap to find the area of rectangle.
11. WAP to find area of square
12. wap to find area of right angle triangle, isosceles
triangle, any triangle with 3 sides.
13. wap to find Area and Volume of Cube.
14. wap to find area and volume of cuboid.
15. WAP to find the largest number using the Logical AND
operator.
16. WAP to validate the username and password entered
by the user is correct or not using
the predefined username and password.
17. WAP to input the positive number from the user to
perform the Left shift operator.
18. WAP to input the positive number from the user to
perform the Right shift operator.
19. WAP to perform the pre increment and pre decrement
operator on two integers and
print both original value and updated value.
20. WAP to perform the post increment and post
decrement operator on two integers and
print both original value and updated value.
21. WAP for an integer number and to check whether it is
divisible by 9 or 7 using OR
logical operator.
22. WAP to identify gender in single character and print
full gender (Ex: if input is 'M' or
'm' – it should print "Male").
23. Write a C program to print all natural numbers in
reverse (from n to 1).
24. Write a C program to print all alphabets from a to z.
25. Write a CWrite a C program to print all natural
numbers from 1 to n.
26. program to print all even numbers between 1 to 100.
27. Write a C program to print all odd number between 1
to 100.
28. Write a C program to find sum of all natural numbers
between 1 to n.
29. Write a C program to find sum of all even numbers
between 1 to n.
30. Write a C program to find sum of all odd numbers
between 1 to n.
31. Write a C program to print multiplication table of any
number.
32. Write a C program to count number of digits in a
number.
33. Write a C program to find first and last digit of a
number.
34. Write a C program to find sum of first and last digit of
a number.
35. Write a C program to swap first and last digits of a
number.
36. Write a C program to calculate sum of digits of a
number.
37. Write a C program to calculate product of digits of a
number.
38. Write a C program to enter a number and print its
reverse.
39. Write a C program to check whether a number is
palindrome or not.
40. Write a C program to find frequency of each digit in a
given integer.
41. Write a C program to enter a number and print it in
words.
42. Write a C program to print all ASCII character with
their values.
43. Write a C program to find power of a number using for
loop.
44. Write a C program to find all factors of a number.
45. Write a C program to calculate factorial of a number.
46. Write a C program to find HCF (GCD) of two numbers.
47. Write a C program to find LCM of two numbers.
48. Write a C program to check whether a number is Prime
number or not.
49. Write a C program to print all Prime numbers between
1 to n.
50. Write a C program to find sum of all prime numbers
between 1 to n.
51. Write a C program to find all prime factors of a
number.
52. Write a C program to check whether a number is
Armstrong number or not.
53. Write a C program to print all Armstrong numbers
between 1 to n.
54. Write a C program to check whether a number is
Perfect number or not.
55. Write a C program to print all Perfect numbers
between 1 to n.
56. Write a C program to check whether a number is
Strong number or not.
57. Write a C program to print all Strong numbers
between 1 to n.
58. Write a C program to print Fibonacci series up to n
terms.
59. Write a C program to find one's complement of a
binary number.
60. Write a C program to find two's complement of a
binary number.
61. Write a C program to convert Binary to Octal number
system.
62. Write a C program to convert Binary to Decimal
number system.
63. Write a C program to convert Binary to Hexadecimal
number system.
64. Write a C program to convert Octal to Binary number
system.
65. Write a C program to convert Octal to Decimal number
system.
66. Write a C program to convert Octal to Hexadecimal
number system.
67. Write a C program to convert Decimal to Binary
number system.
68. Write a C program to convert Decimal to Octal number
system.
69. Write a C program to convert Decimal to Hexadecimal
number system.
70. Write a C program to convert Hexadecimal to Binary
number system.
71. Write a C program to convert Hexadecimal to Octal
number system.
72. Write a C program to convert Hexadecimal to Decimal
number system.
Pattern Exercises
1. Star pattern programs - Write a C program to print the
given star patterns.
*
***
*****
*******
*********
Pyramid Star Pattern
*
**
**
**
*********
Hollow Pyramid Star Pattern
*********
*******
*****
***
*
Inverted Pyramid Star Pattern
*********
**
**
**
*
Hollow Inverted Pyramid Star Pattern
*
**
***
****
*****
****
***
**
*
Half Diamond Star Pattern
*
**
***
****
*****
****
***
**
*
Mirrored Half Diamond Star Pattern
2. Number pattern programs - Write a C program to print
the given number patterns
Square number patterns
11111
11111
11111
11111
11111
Number pattern 1
11111
00000
11111
00000
11111
Number pattern 2
01010
01010
01010
01010
01010
Number pattern 3
11111
10001
10001
10001
11111
Number pattern 4
11111
11111
11011
11111
11111
Number pattern 5
10101
01010
10101
01010
10101
If…Else Exercises
1. Write a C program to find maximum between two
numbers.
2. Write a C program to find maximum between three
numbers.
3. Write a C program to check whether a number is
negative, positive or zero.
4. Write a C program to check whether a number is
divisible by 5 and 11 or not.
5. Write a C program to check whether a number is even
or odd.
6. Write a C program to check whether a year is leap year
or not.
7. Write a C program to check whether a character is
alphabet or not.
8. Write a C program to input any alphabet and check
whether it is vowel or consonant.
9. Write a C program to input any character and check
whether it is alphabet, digit or special
character.
10. Write a C program to check whether a character is
uppercase or lowercase alphabet.
11. Write a C program to input week number and print
week day.
12. Write a C program to input month number and print
number of days in that month.
13. Write a C program to count total number of notes in
given amount.
14. Write a C program to input month number and print
number of days in that month.
15. Write a C program to count total number of notes in
given amount.
16. Write a C program to input angles of a triangle and
check whether triangle is valid or not.
17. Write a C program to input all sides of a triangle and
check whether triangle is valid or not.
18. Write a C program to check whether the triangle is
equilateral, isosceles or scalene triangle.
19. Write a C program to find all roots of a quadratic
equation.
20. Write a C program to calculate profit or loss.
21. Write a C program to input marks of five subjects
Physics, Chemistry, Biology, Mathematics
and Computer. Calculate percentage and grade according
to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
22. Write a C program to input basic salary of an employee
and calculate its Gross salary according
to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary >20000 : HRA = 30%, DA = 95%
23. Write a C program to input electricity unit charges and
calculate total electricity bill according
to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
24. Write a C program to convert specified days into years,
weeks and days.
1. Write a program to print
“Hello World”.

#include <stdio.h>
int main() {

printf ("Hello World");


return 0;
}
OUTPUT:

2. Write a program to add two


numbers.

#include <stdio.h>
int main() {
int num1, num2, sum;
printf ("Enter two integers");
scanf ("%d %d", &num1, &num2);
//calculate the sum
sum = num1 + num2;
printf ("\nSum of the numbers =%d + %d = %d", num1,
num2, sum);
return 0;
}

OUTPUT:

3. Write a program to find area


of circle.
#include <stdio.h>
#define pie 3.14
int main() {

float radius, area;


printf ("Enter the radius of the circle : ");
scanf ("%f", &radius);
area = pie * radius * radius;
printf ("Area of the given circle = %.4f", area);
return 0;
}

OUTPUT:

4. Write a program to divide two


numbers.

#include <stdio.h>
int main() {
float a, b;
float div;
printf ("Enter 1st number : ");
scanf ("%f", &a);
printf ("Enter 2nd number : ");
scanf ("%f", &b);
div = a / b;
printf ("\nDivision of the given numbers = %.4f", div);
return 0;
}
OUTPUT:

5. Write a program to print ASCII


value.
#include <stdio.h>
int main() {

char c;
printf ("Enter a character : ");
scanf ("%c", &c);
//%d display the integer value of a character
//%c display the actual character
printf ("\nASCII value of %c = %d", c, c);
return 0;
}
OUTPUT:

6. Write a program to multiply


floating numbers.
#include <stdio.h>
int main() {

double a, b, product;
printf ("Enter 1st number : ");
scanf ("%lf", &a);
printf ("Enter 2nd number : ");
scanf ("%lf", &b);
product = a * b;
printf ("\nProduct of the given numbers = %.4f", product);
return 0;
}
OUTPUT:

7. Write a program to SWAP two


variables by using third
variable.
#include <stdio.h>
int main() {
int a, b, c;
printf ("Enter first number: ");
scanf ("%d", &a);
printf ("Enter second number: ");
scanf ("%d", &b);
c = a;
a = b;
b = c;
printf ("\nSwap numbers are a = %d and b = %d", a, b);
return 0;
}
OUTPUT:

8. Write a program to SWAP two


variables without using third
variable.
#include <stdio.h>
int main () {
int a, b;
printf ("Enter 1st number :- ");
scanf ("%d", &a);
printf ("Enter 2nd number :- ");
scanf ("%d", &b);
a = a + b; /*a = %d+%d = %d*/ printf ("\na = %d", a);
b = a - b; /*b = %d-%d = %d*/ printf (" b = %d", b);
a = a - b; /*a = %d-%d = %d*/ printf (" a = %d\n", a);
printf ("\nSwap numbers are :\na = %d \nb = %d", a, b);
return 0;
}
OUTPUT:
9. Write a program to SWAP three
variables without using fourth
variable.

#include <stdio.h>
int main() {
int a, b, c;
printf ("Enter 1st number: ");
scanf ("%d", &a);
printf ("Enter 2nd number: ");
scanf ("%d", &b);
printf ("Enter 3rd number: ");
scanf ("%d", &c);

a = a+b; printf("\na=%d",a);
b = b+c; printf(" b=%d",b);
c = c+a; printf(" c=%d",c);
a = c-a; printf(" a=%d",a);
b = c-b; printf(" b=%d",b);
c = c-(a+b); printf(" c=%d\n",c);
printf("\nSwap numbers are a = %d, b = %d & c = %d", a, b, c);
return 0;
}

OUTPUT:
10. Write a program to find area
of rectangle.
#include <stdio.h>
int main() {

int l, b, area;
printf ("Length of the rectangle : ");
scanf ("%d", &l);
printf ("Breadth of the rectangle : ");
scanf ("%d", &b);
area = l * b;
printf ("\nArea of the rectangle = %d * %d = %d", l, b, area);
return 0;
}
OUTPUT:
11. Write a program to find area
of square.
#include <stdio.h>
int main() {

int a, area;
printf ("Enter the side of the square (a) : ");
scanf ("%d", &a);

area = a * a;
printf ("\nArea of the square = %d * %d = %d", a, a, area);
return 0;
}

OUTPUT:
12. Write a program to find area of
right-angle triangle, isosceles
triangle, any triangle with three
sides.
AREA OF ISOSCELES TRIANGLE:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("Enter the three sides of the triangle: ");
scanf("%f %f %f", &a, &b, &c);
s = (a+b+c) / 2;
printf("\nSemi-perimeter = %.2f\n", s);
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea of the given triangle = %.4f", area);
return 0;
}
OUTPUT:
AREA OF RIGHT-ANGLED TRIANGLE:
#include <stdio.h>
int main() {

int b, h, area;
printf ("Enter the base of the triangle: ");
scanf ("%d", &b);
printf ("Enter the height of the triangle: ");
scanf ("%d", &h);
area = 1/2 * b * h;
printf ("Area of the triangle = %d", area);
return 0;
}
OUTPUT:
13. rite a program to find area
and volume of a cube

AREA OF CUBE
#include <stdio.h>
int main() {

int a, area;
printf ("Enter the side of a cube (a) : ");
scanf ("%d", &a);

area = 6 * (a * a);
printf ("\nArea of cube = %d * (%d * %d) = %d", 6, a, a, area);
return 0;
}
OUTPUT:
VOLUME OF CUBE
#include <stdio.h>
int main() {

int a, volume;
printf ("Enter the side of a cube (a) : ");
scanf ("%d", &a);

volume = a * a * a;
printf ("\nVolume of cube = %d * %d * %d = %d", a, a, a,
volume);
return 0;
}

OUTPUT:
14. Write a program to find area
and volume of a cuboid
AREA OF CUBOID
#include <stdio.h>
int main() {
int l, b, h, area;
printf ("Length of the cuboid : ");
scanf ("%d", &l);
printf ("Breadth of the cuboid : ");
scanf ("%d", &b);
printf ("Height of the cuboid : ");
scanf ("%d", &h);
area = 2 * (l*b + b*h + h*l);
printf ("\nArea of cuboid = %d * (%d*%d + %d*%d + %d*%d)
= %d", 2,l,b,b,h,h,l, area);
return 0;
}
OUTPUT:
VOLUME OF CUBOID
#include <stdio.h>
int main() {
int l, b, h, volume;
printf ("Length of the cuboid : ");
scanf ("%d", &l);
printf ("Breadth of the cuboid : ");
scanf ("%d", &b);
printf ("Height of the cuboid : ");
scanf ("%d", &h);
volume = l * b * h;
printf ("\nVolume of cuboid = %d * %d * %d = %d", l, b, h,
volume);
return 0;
}
OUTPUT:
15. WAP to find the largest
number using the logical AND
operator.

#include <stdio.h>

int main() {
int n;
printf("Kashish\n");
printf("Enter the number of elements: ");
scanf("%d", &n);

if (n <= 0) {
printf("Invalid input. Please enter a positive number of
elements.\n");
return 1;
}

int largest;
int first = 1;
for (int i = 0; i < n; ++i) {
int num;
printf("Enter number %d: ", i + 1);
scanf("%d", &num);
if (first || (num > largest)) {
largest = num;
first = 0; // Set the flag to 0 after the first number is
entered
}
}

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

return 0;
}
16. WAP to validate the username and
password entered by the user is
correct or not using the predefined
username and password.

#include <stdio.h>
#include <string.h>
int main() {
char correctUsername[] = "Kashish";
char correctPassword[] = "123456789";

char enteredUsername[50];
char enteredPassword[50];
printf("Kashish\n");

printf("Enter username: ");


scanf("%s", enteredUsername);

printf("Enter password: ");


scanf("%s", enteredPassword);

if (strcmp(enteredUsername, correctUsername) == 0
&& strcmp(enteredPassword, correctPassword) == 0) {
printf("Login successful!\n");
} else {
printf("Login failed. Please check your username and
password.\n");
}

return 0;
}
17. WAP to input the positive
number from the user to
perform the left shift operator.

#include <stdio.h>

int main() {
int num, shift;
{
printf("KASHISH\n");
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 0) {
printf("Please enter a positive integer.\n");
}
} while (num <= 0);
printf("Enter the number of positions to shift left: ");
scanf("%d", &shift);
int result = num << shift;

printf("Result of left shift: %d << %d = %d\n", num, shift,


result);

return 0;
}
18. WAP to input the positive
number from the user to perform
the right shift operator.

#include <stdio.h>

int main() {
int num, shift;
{
printf("KASHISH\n");
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 0) {
printf("Please enter a positive integer.\n");
}
} while (num <= 0);
printf("Enter the number of positions to shift right: ");
scanf("%d", &shift);

int result = num >> shift;

printf("Result of right shift: %d >> %d = %d\n", num, shift,


result);

return 0;
}
19. WAP to perform the pre
increment and pre decrement
operator on two integers and print
both original value and updated
value.

#include <stdio.h>

int main() {
int num1, num2;

printf("KASHISH\n");
printf("Enter the first integer: ");
scanf("%d", &num1);

printf("Enter the second integer: ");


scanf("%d", &num2);

int preIncNum1 = ++num1;


int preDecNum2 = --num2;

printf("Original value of num1: %d\n", num1);


printf("Updated value of num1 (after pre-increment):
%d\n", preIncNum1);

printf("Original value of num2: %d\n", num2);


printf("Updated value of num2 (after pre-decrement):
%d\n", preDecNum2);

return 0;
}

20. WAP to perform the post


increment and post decrement
operator on two integers and print
both original value and updated
value.

#include <stdio.h>
int main() {
int num1, num2;
printf("KASHISH\n");
printf("Enter the first integer: ");
scanf("%d", &num1);

printf("Enter the second integer: ");


scanf("%d", &num2);

int postIncNum1 = num1++;


int postDecNum2 = num2--;

printf("Original value of num1: %d\n", num1);


printf("Updated value of num1 (after post-increment):
%d\n", postIncNum1);

printf("Original value of num2: %d\n", num2);


printf("Updated value of num2 (after post-decrement):
%d\n", postDecNum2);
return 0;
}

21. WAP for an integer number and to


check whether it is divisible by 9 or 7
using OR logical operator.

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

if (num % 9 == 0 || num % 7 == 0) {
printf("%d is divisible by 9 or 7.\n", num);
} else {
printf("%d is not divisible by 9 or 7.\n", num);
}

return 0;
}
22. WAP to identify gender in single
character and print full gender (Ex:
if input is 'M' or 'm' – it
should print "Male").

#include <stdio.h>
int main() {
char gender;

printf("KASHISH \n");
printf("Enter gender (M/F): ");
scanf(" %c", &gender);

switch (gender) {
case 'M':
case 'm':
printf("Male\n");
break;
case 'F':
case 'f':
printf("Female\n");
break;
default:
printf("Invalid gender input\n");
}

return 0;
}

23. Write a C program to print all natural numbers


in reverse (from n to 1)

#include<stdio.h>
int main() {
int i, n;
printf("Kashish \n");
printf("\nEnter 'n': ");
scanf("%d", &n);
printf("\nNumbers in reverse order (from n to 1) =\n");

for (i = n; i >= 1; i--) {


printf("%d ", i);
}
return 0;
}
INPUT:
24.Write a C program to print all alphabets from a
to z.

#include<stdio.h>
int main() {
int i;
printf("Kashish \n");
printf("\nAlphabets from (a to z) :\n");
for (i = 97; i <= 122; i++) { //ASCII value of a = 97
printf("%c ", i);
}
return 0;
}
INPUT:

25. Write a C program to print all natural


numbers from 1 to n.
#include<stdio.h>
int main() {
int i, n;
printf("Kashish \n");
printf("\nEnter 'n': ");
scanf("%d", &n);
printf("\nNumbers from 1 to n =\n");

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


printf("%d ", i);
}
return 0;
}
INPUT:
26. Write a program to print all even numbers
between 1 to 100.
#include<stdio.h>
int main() {
int i;
printf("Kashish \n");
printf("\nEven numbers from 1 to 100 :\n");
for (i = 1; i <= 100; i++) {
if (i % 2 != 0) {
continue;
}
printf("%d ", i);
}
return 0;
}
INPUT:

27. Write a C program to print all odd number


between 1 to 100.
#include<stdio.h>
int main() {
int i;
printf("Kashish \n");
printf("\nOdd numbers from 1 to 100 :\n");
for (i = 1; i <= 100; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
return 0;
}
INPUT:
28. Write a C program to find sum of all natural
numbers between 1 to n.
#include<stdio.h>
int main()
{
printf("Kashish ");
int i, n, sum = 0;
printf("\nEnter any number: ");
scanf("%d", &n);

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


printf("%d ", i);
sum = sum + i;
}
printf("\n\nSum of natural numbers from (1 to n) =
%d", sum);
return 0;
}
INPUT:
29. Write a C program to find sum of all even
numbers between 1 to n.
#include<stdio.h>
int main()
{
printf("Kashish ");
int i, n, sum = 0;
printf("\nEnter any number: ");
scanf("%d", &n);

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


if (i % 2 != 0) {
continue;
}
printf("%d ", i);
sum = sum + i;
}
printf("\n\nSum of all even numbers from (1 to n) =
%d", sum);
return 0;
}
INPUT:
30.Write a C program to find sum of all odd
numbers between 1 to n.

#include<stdio.h>
int main()
{
printf("Kashish ");
int i, n, sum = 0;
printf("\nEnter any number: ");
scanf("%d", &n);

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


if (i % 2 == 0) {
continue;
}
printf("%d ", i);
sum = sum + i;
}
printf("\n\nSum of all odd numbers from (1 to n) =
%d", sum);
return 0;
}
INPUT:
31. Write a C program to print multiplication table
of any number.
#include<stdio.h>
int main()
{
printf("Kashish ");
int i, n, product;
printf("\nEnter any number: ");
scanf("%d", &n);

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


product = n * i;
printf("%d * %d = %d\n", n, i, product);
}
return 0;
}

INPUT:
32. Write a C program to count number of digits in
a number.

#include<stdio.h>
int main()
{
printf("\nKashish ");
int n, count = 0;
printf("\nEnter any number: ");
scanf("%d", &n);

while (n > 0) {
n = n / 10;
count++;
}
printf("\nNumber of digits = %d", count);
return 0;
}
INPUT:
33. Write a C program to find first and last digit of
a number
#include <stdio.h>

int main() {
printf("Kashish ");
int number, firstDigit, lastDigit;

printf("Enter a number: ");


scanf("%d", &number);

// Find the last digit


lastDigit = number % 10;

// Find the first digit


while (number >= 10) {
number /= 10;
}
firstDigit = number;

printf("First digit: %d\n", firstDigit);


printf("Last digit: %d\n", lastDigit);

return 0;
}
INPUT:
34. Write a C program to find sum of first and last
digit of a number
#include <stdio.h>

int main() {
int num, firstDigit, lastDigit, sum;

// Input a number from the user


printf("Enter a number: ");
scanf("%d", &num);

// Find the last digit


lastDigit = num % 10;

// Find the first digit


while (num >= 10) {
num /= 10;
}
firstDigit = num;

// Calculate the sum


sum = firstDigit + lastDigit;

// Display the result


printf("The sum of the first and last digits of the
number is: %d\n", sum);

return 0;
}

INPUT:
35. Write a C program to swap first and last
digits of a number.
#include <stdio.h>

int main() {
printf("Kashish ");
int number, originalNumber, firstDigit, lastDigit, temp;

printf("Enter a number: ");


scanf("%d", &number);

originalNumber = number;
// Get the last digit
lastDigit = number % 10;

// Find the number of digits in the given number


while (number >= 10) {
number /= 10;
}

// The first digit is now the remaining number


firstDigit = number;

// Swap the first and last digits


temp = firstDigit;
firstDigit = lastDigit;
lastDigit = temp;

// Reconstruct the new number


int swappedNumber = 0;
number = originalNumber;
while (number >= 10) {
swappedNumber = swappedNumber * 10 + (number
% 10);
number /= 10;
}
swappedNumber = swappedNumber * 10 + number;

printf("Number after swapping: %d\n",


swappedNumber);

return 0;
}
INPUT:
36. Write a C program to calculate sum of digits of
a number.
#include <stdio.h>

int main() {
printf("Kashish ");
int num, sum = 0, digit;

printf("Enter an integer: ");


scanf("%d", &num);
while (num > 0) {
digit = num % 10; // Get the last digit
sum += digit; // Add it to the sum
num = num / 10; // Remove the last digit
}

printf("The sum of the digits is: %d\n", sum);

return 0;
}
INPUT:
37. Write a C program to calculate product of digits of a
number
#include <stdio.h>

int main() {
printf("Kashish ");
int num, digit, product = 1;

/* Input number from the user */


printf("Enter a number: ");
scanf("%d", &num);

while (num != 0) {
digit = num % 10; // Get the last digit
product *= digit; // Multiply it with the product
num = num / 10; // Remove the last digit
}

printf("Product of digits = %d\n", product);

return 0;
}
INPUT:
38. Write a C program to enter a number and print its
reverse.
#include <stdio.h>

int main() {
printf("Kashish ");
int num, reversed = 0;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}

printf("Reversed number: %d\n", reversed);


return 0;
}
INPUT:
39. Write a C program to check whether a number is
palindrome or not.
#include <stdio.h>
int main() {
printf("Kashish ");
int num, originalNum, reversed = 0;

printf("Enter a number: ");


scanf("%d", &num);

originalNum = num; // Store the original number

// Reverse the number


while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}

// Check if the reversed number is the same as the


original number
if (originalNum == reversed) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}

return 0;
}

INPUT:

40. Write a C program to find frequency of each digit in


a given integer.
#include <stdio.h>

int main() {
printf("Kashish ");
int num, digit, count;
int frequency[10] = {0}; // Initialize an array to store
the frequency of each digit

printf("Enter an integer: ");


scanf("%d", &num);

while (num != 0) {
digit = num % 10; // Get the last digit
frequency[digit]++; // Increment the count for that
digit
num /= 10; // Remove the last digit
}

printf("Digit Frequency:\n");
for (digit = 0; digit <= 9; digit++) {
if (frequency[digit] > 0) {
printf("%d: %d times\n", digit, frequency[digit]);
}
}

return 0;
}

INPUT:
41. Write a C program to enter a number and print it in
words.
#include <stdio.h>

// Function to print a number in words for single-digit numbers


void printDigitInWords(int digit) {
switch (digit) {
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}
}

int main() {
printf("Kashish ");
int num, digit, reversed = 0;

printf("Enter a number: ");


scanf("%d", &num);

if (num == 0) {
printf("Zero\n");
return 0;
}

// Reverse the number to print it correctly


while (num != 0) {
digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}

// Print the number in words


while (reversed != 0) {
digit = reversed % 10;
printDigitInWords(digit);
reversed /= 10;
}

printf("\n");

return 0;
}

INPUT:
42. Write a C program to print all ASCII character with
their values.
#include <stdio.h>

int main() {
printf("Kashish ");
int i;
for (i = 0; i < 128; i++) {
printf("ASCII value %d represents character: %c\n",
i, i);
}
return 0;
}

INPUT:
43. Write a C program to find power of a number using
for loop.
#include <stdio.h>

int main() {
printf("Kashish ");
double base, exponent, result = 1;

// Input the base and exponent


printf("Enter the base: ");
scanf("%lf", &base);
printf("Enter the exponent: ");
scanf("%lf", &exponent);

// Calculate the power using a for loop


for (int i = 1; i <= exponent; i++) {
result *= base;
}

// Print the result


printf("%.2lf^%.2lf = %.2lf\n", base, exponent, result);
return 0;
}
INPUT:
44. Write a C program to find all factors of a number.
#include <stdio.h>

int main() {
printf("Kashish ");
int number;

// Input the number


printf("Enter a positive integer: ");
scanf("%d", &number);

if (number <= 0) {
printf("Please enter a positive integer.\n");
return 1; // Exit with an error code
}
printf("Factors of %d are: ", number);
// Use a for loop to find and print factors
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
printf("%d ", i);
}
}

printf("\n");

return 0;
}

INPUT:
45. Write a C program to calculate factorial of a
number.
#include <stdio.h>
int main() {
printf("Kashish ");
int number;
unsigned long long factorial = 1;

// Input the number


printf("Enter a non-negative integer: ");
scanf("%d", &number);

if (number < 0) {
printf("Factorial is not defined for negative
numbers.\n");
return 1; // Exit with an error code
}

// Calculate the factorial


for (int i = 1; i <= number; i++) {
factorial *= i;
}

// Print the result


printf("Factorial of %d is %llu\n", number, factorial);

return 0;
}
INPUT:

46. Write a C program to find HCF (GCD) of two


numbers.
#include <stdio.h>
// Function to find the HCF/GCD of two numbers
int findGCD(int a, int b) {
if (b == 0) {
return a;
} else {
return findGCD(b, a % b);
}
}
int main() {
printf("Kashish ");
int num1, num2;

printf("Enter two numbers: ");


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

int gcd = findGCD(num1, num2);

printf("The GCD of %d and %d is %d\n", num1,


num2, gcd);

return 0;
}
INPUT:

47. Write a C program to find LCM of two numbers.


#include <stdio.h>

// Function to find the GCD (HCF) of two numbers


int findGCD(int a, int b) {
if (b == 0) {
return a;
} else {
return findGCD(b, a % b);
}
}

// Function to find the LCM of two numbers


int findLCM(int a, int b) {
int gcd = findGCD(a, b);
int lcm = (a * b) / gcd;
return lcm;
}

int main() {
printf("Kashish ");
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);
int lcm = findLCM(num1, num2);

printf("The LCM of %d and %d is %d\n", num1, num2,


lcm);

return 0;
}
INPUT:

48. Write a C program to check whether a number is


Prime number or not.
#include <stdio.h>
#include <stdbool.h>
// Function to check if a number is prime
bool isPrime(int n) {
if (n <= 1) {
return false; // 0 and 1 are not prime numbers
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false; // n is divisible by i, so it's not prime
}
}
return true; // If no divisors are found, it's a prime
number
}

int main() {
printf("Kashish ");
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}
INPUT:

49. Write a C program to print all Prime numbers


between 1 to n.
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
if (num <= 3) return 1;
if (num % 2 == 0 || num % 3 == 0) return 0;

for (int i = 5; i * i <= num; i += 6) {


if (num % i == 0 || num % (i + 2) == 0) {
return 0;
}
}
return 1;
}

int main() {
printf("Kashish ");
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Prime numbers between 1 and %d are:\n", n);

for (int i = 2; i <= n; i++) {


if (isPrime(i)) {
printf("%d ", i);
}
}

printf("\n");
return 0;
}
INPUT:

50. Write a C program to find sum of all prime numbers


between 1 to n.
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
if (num <= 3) return 1;
if (num % 2 == 0 || num % 3 == 0) return 0;

for (int i = 5; i * i <= num; i += 6) {


if (num % i == 0 || num % (i + 2) == 0) {
return 0;
}
}

return 1;
}
int main() {
printf("Kashish ");
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int sum = 0;
printf("Prime numbers between 1 and %d are:\n", n);

for (int i = 2; i <= n; i++) {


if (isPrime(i)) {
printf("%d ", i);
sum += i;
}
}

printf("\nSum of prime numbers between 1 and %d is:


%d\n", n, sum);
return 0;
}
INPUT:
51. Write a C program to find all prime factors of a
number.
#include <stdio.h>
// Function to find and print all prime factors of a number
void primeFactors(int n) {
// Print the number of 2s that divide n
while (n % 2 == 0) {
printf("2 ");
n = n / 2;
}

// n must be odd at this point, so a skip of 2 ( i = i + 2)


can be used
for (int i = 3; i * i <= n; i = i + 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
printf("%d ", i);
n = n / i;
}
}

// If n is a prime greater than 2


if (n > 2) {
printf("%d ", n);
}
}

int main() {
printf("Kashish ");
int n;
printf("Enter a number: ");
scanf("%d", &n);

printf("Prime factors of %d are: ", n);


primeFactors(n);

return 0;
}
INPUT:
52. Write a C program to check whether a
number is Armstrong number or not.
#include <stdio.h>
#include <math.h>

int isArmstrong(int num) {


int originalNum, remainder, n = 0, result = 0;

originalNum = num;

while (originalNum != 0) {
originalNum /= 10;
++n;
}

originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if (result == num)
return 1; // It's an Armstrong number
else
return 0; // It's not an Armstrong number
}

int main() {
printf("Kashish ");
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (isArmstrong(num))
printf("%d is an Armstrong number.\n");
else
printf("%d is not an Armstrong number.\n");

return 0;
}

OUTPUT:
53. Write a C program to print all Armstrong
numbers between 1 to n.

#include <stdio.h>
#include <math.h>

int isArmstrong(int num) {


int originalNum, remainder, result = 0, n = 0;

originalNum = num;

while (originalNum != 0) {
originalNum /= 10;
++n;
}

originalNum = num;

while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if (result == num)
return 1;
else
return 0;
}

int main() {
printf("Kashish ");
int n, i;

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


scanf("%d", &n);

printf("Armstrong numbers between 1 and %d are:\n", n);

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


if (isArmstrong(i))
printf("%d\n", i);
}

return 0;
}

OUTPUT:
54. Write a C program to check whether a number
is Perfect number or not.
#include <stdio.h>

int isPerfect(int num) {


int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return (sum == num);
}

int main() {
printf("Kashish ");
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (isPerfect(num)) {
printf("%d is a perfect number.\n", num);
} else {
printf("%d is not a perfect number.\n", num);
}

return 0;
}

OUTPUT:
55. Write a C program to print all Perfect numbers
between 1 to n.
#include <stdio.h>
int isPerfect(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return (sum == num);
}

int main() {
printf("Kashish ");
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);

printf("Perfect numbers between 1 and %d are: \n", n);

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


if (isPerfect(i)) {
printf("%d\n", i);
}
}

return 0;
}
OUTPUT:

56. Write a C program to check whether a number


is Strong number or not.
#include <stdio.h>
// Function to calculate the factorial of a number
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
// Function to check if a number is a strong number
int isStrongNumber(int num) {
int originalNum = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}
return (sum == originalNum);
}

int main() {
printf("Kashish ");
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);

if (isStrongNumber(n)) {
printf("%d is a Strong number.\n");
} else {
printf("%d is not a Strong number.\n");
}

return 0;
}
OUTPUT:
57. Write a C program to print all Strong numbers
between 1 to n.
#include <stdio.h>
// Function to calculate the factorial of a number
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}

// Function to check if a number is a strong number


int isStrongNumber(int num) {
int originalNum = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}
return (sum == originalNum);
}

int main() {
printf("Kashish ");
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);

printf("Strong numbers between 1 and %d are: \n", n);

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


if (isStrongNumber(i)) {
printf("%d\n", i);
}
}

return 0;
}
OUTPUT:
58. Write a C program to print Fibonacci series up to n
terms.
#include <stdio.h>
int main() {
printf("Kashish ");
int n, first = 0, second = 1, next;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series up to %d terms: \n", n);

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


if (i == 1) {
printf("%d, ", first);
} else if (i == 2) {
printf("%d, ", second);
} else {
next = first + second;
printf("%d, ", next);
first = second;
second = next;
}
}

printf("\n");

return 0;
}

OUTPUT:
59. Write a C program to find one's complement of
a binary number.
#include <stdio.h>
int main() {
printf("Kashish ");
char binary[32]; // Assuming a maximum of 32-bit
binary number
int length, i;

printf("Enter a binary number: ");


scanf("%s", binary);
// Find the length of the binary number
length = strlen(binary);

// Perform one's complement


for (i = 0; i < length; i++) {
if (binary[i] == '0') {
binary[i] = '1';
} else if (binary[i] == '1') {
binary[i] = '0';
}
}

printf("One's complement: %s\n", binary);

return 0;
}
OUTPUT:

60. Write a C program to find two's complement of a


binary number.
#include <stdio.h>
#include <string.h>

// Function to reverse a binary string


void reverseString(char str[]) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}
// Function to add 1 to a binary string
void addOne(char binary[]) {
int length = strlen(binary);
int carry = 1;

for (int i = 0; i < length; i++) {


if (binary[i] == '0' && carry == 1) {
binary[i] = '1';
carry = 0;
} else if (binary[i] == '1' && carry == 1) {
binary[i] = '0';
}
}
}

int main() {
printf ("Kashish ');
char binary[32]; // Assuming a maximum of 32-bit
binary number

printf("Enter a binary number: ");


scanf("%s", binary);

// Reverse the binary string


reverseString(binary);

// Add 1 to the reversed binary string


addOne(binary);

// Reverse the result to get the two's complement


reverseString(binary);

printf("Two's complement: %s\n", binary);

return 0;
}
OUTPUT:

61. Write a C program to convert Binary to Octal


#include <stdio.h>
#include <string.h>

// Function to convert a binary number to an octal


number
void binaryToOctal(char binary[]) {
number system.
int length = strlen(binary);
// Pad the binary number with leading zeros if needed
to make the length a multiple of 3
int padding = (3 - (length % 3)) % 3;
for (int i = 0; i < padding; i++) {
printf("0");
}

// Iterate through the binary number in groups of 3


and convert to octal
for (int i = padding; i < length; i += 3) {
int octalDigit = (binary[i] - '0') * 4 + (binary[i + 1] - '0')
* 2 + (binary[i + 2] - '0');
printf("%d", octalDigit);
}
printf("\n");
}

int main() {
printf("Kashish ");
char binary[32]; // Assuming a maximum of 32-bit
binary number

printf("Enter a binary number: ");


scanf("%s", binary);

printf("Octal representation: ");


binaryToOctal(binary);

return 0;
}
OUTPUT:
62. Write a C program to convert Binary to Decimal
number system.
#include <stdio.h>
#include <string.h>
// Function to convert a binary number to decimal
int binaryToDecimal(char binary[]) {
int length = strlen(binary);
int decimal = 0;
int base = 1;
for (int i = length - 1; i >= 0; i--) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}

return decimal;
}

int main() {
printf("Kashish ");
char binary[32]; // Assuming a maximum of 32-bit
binary number

printf("Enter a binary number: ");


scanf("%s", binary);
int decimal = binaryToDecimal(binary);

printf("Decimal representation: %d\n", decimal);

return 0;
}
OUTPUT:

63. Write a C program to convert Binary to


Hexadecimal number system.
#include <stdio.h>
#include <string.h>

// Function to convert a binary number to hexadecimal


void binaryToHexadecimal(char binary[]) {
int length = strlen(binary);

// Pad the binary number with leading zeros if needed


to make the length a multiple of 4
int padding = (4 - (length % 4)) % 4;
for (int i = 0; i < padding; i++) {
printf("0");
}

// Iterate through the binary number in groups of 4


and convert to hexadecimal
for (int i = padding; i < length; i += 4) {
int hexDigit = 0;
for (int j = 0; j < 4; j++) {
hexDigit = (hexDigit << 1) | (binary[i + j] - '0');
}

if (hexDigit < 10) {


printf("%d", hexDigit);
} else {
printf("%c", 'A' + hexDigit - 10);
}
}
printf("\n");
}

int main() {
printf("Kashish ");
char binary[32]; // Assuming a maximum of 32-bit
binary number

printf("Enter a binary number: ");


scanf("%s", binary);

printf("Hexadecimal representation: ");


binaryToHexadecimal(binary);

return 0;
}

OUTPUT:

64. Write a C program to convert Octal to Binary


number system.
#include <stdio.h>
#include <string.h>
// Function to convert an octal digit to a binary
string
char *octalToBinary(char octalDigit) {
switch (octalDigit) {
case '0': return "000";
case '1': return "001";
case '2': return "010";
case '3': return "011";
case '4': return "100";
case '5': return "101";
case '6': return "110";
case '7': return "111";
default: return "Invalid";
}
}

int main() {
printf("Kashish ");
char octal[32]; // Assuming a maximum of 32-bit
octal number
char binary[128]; // To store the binary equivalent

printf("Enter an octal number: ");


scanf("%s", octal);

int length = strlen(octal);


int binaryIndex = 0;

// Convert each octal digit to binary and


concatenate
for (int i = 0; i < length; i++) {
char *binaryDigit = octalToBinary(octal[i]);

// Skip the first digit if it results in leading zeros


if (i == 0 &&binaryDigit[0] == '0') {
binaryDigit += 1;
}

// Copy the binary digits to the binary string


strcpy(binary + binaryIndex, binaryDigit);
binaryIndex += strlen(binaryDigit);
}

printf("Binary representation: %s\n", binary);

return 0;
}
OUTPUT:
65. Write a C program to convert Octal to Decimal
number system.
#include <stdio.h>
#include <math.h>
int octalToDecimal(char octal[]) {
int length = 0;
while (octal[length] != '\0') {
length++;
}

int decimal = 0;
int base = 1;

for (int i = length - 1; i >= 0; i--) {


if (octal[i] < '0' || octal[i] > '7') {
printf("Invalid octal digit: %c\n", octal[i]);
return -1;
}

int octalDigit = octal[i] - '0';


decimal += octalDigit * base;
base *= 8;
}
return decimal;
}

int main() {
printf("Kashish ");
char octal[32]; // Assuming a maximum of 32-bit octal
number

printf("Enter an octal number: ");


scanf("%s", octal);

int decimal = octalToDecimal(octal);

if (decimal != -1) {
printf("Decimal representation: %d\n", decimal);
}

return 0;
}
OUTPUT:

66. Write a C program to convert Octal to


Hexadecimal number system.
#include <stdio.h>
#include <string.h>

// Function to convert octal to decimal


int octalToDecimal(char octal[]) {
int decimal = 0, i = 0, len = strlen(octal);

while (len--) {
decimal += (octal[len] - '0') * (1 << (3 * i));
i++;
}

return decimal;
}
// Function to convert decimal to hexadecimal
void decimalToHexadecimal(int decimal) {
char hexadecimal[100];
int i = 0;
while (decimal > 0) {
int remainder = decimal % 16;
if (remainder < 10)
hexadecimal[i++] = remainder + '0';
else
hexadecimal[i++] = remainder + 'A' - 10;

decimal /= 16;
}

printf("Hexadecimal equivalent: ");


for (int j = i - 1; j >= 0; j--) {
printf("%c", hexadecimal[j]);
}
}
int main() {
char octal[100];

printf("Enter an octal number: ");


scanf("%s", octal);

int decimal = octalToDecimal(octal);

decimalToHexadecimal(decimal);

return 0;
}
OUTPUT:
67. Write a C program to convert Decimal to Binary
number system.
#include <stdio.h>
void decimalToBinary(int decimal) {
if (decimal == 0) {
printf("Binary: 0\n");
return;
}

int binary[32];
int i = 0;

while (decimal > 0) {


binary[i] = decimal % 2;
decimal /= 2;
i++;
}
printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

int main() {
printf("Kashish ");
int decimal;

printf("Enter a decimal number: ");


scanf("%d", &decimal);

decimalToBinary(decimal);

return 0;
}
OUTPUT:

68. Write a C program to convert Decimal to Octal


number system.
#include <stdio.h>
int main() {
printf("Kashish ");
int decimal, octal[100], i = 0;

printf("Enter a decimal number: ");


scanf("%d", &decimal);

while (decimal > 0) {


octal[i] = decimal % 8;
decimal /= 8;
i++;
}

printf("Octal equivalent: ");


for (int j = i - 1; j >= 0; j--) {
printf("%d", octal[j]);
}

return 0;
OUTPUT:
69. Write a C program to convert Decimal to
Hexadecimal number system.> 0) {
remainder = decimal % 16;
if (remainder < 10) {
hexadecimal[i] = remainder + '0';
} else {
#
hexadecimal[i] = remainder - 10 + 'A';
}
decimal /= 16;
i++;
}

printf("Hexadecimal equivalent: 0x");


for (int j = i - 1; j >= 0; j--) {
printf("%c", hexadecimal[j]);
}

return 0;
}

OUTPUT:
70. Write a C program to convert Hexadecimal to
Binary number system.
#include <stdio.h>
#include <string.h>
int main() {
printf("Kashish ");
char hexadecimal[100];
printf("Enter a hexadecimal number: ");
scanf("%s", hexadecimal);

int length = strlen(hexadecimal);


printf("Binary equivalent: ");

for (int i = 0; i < length; i++) {


char hexDigit = hexadecimal[i];
int decimalValue;

if (hexDigit >= '0' && hexDigit <= '9') {


decimalValue = hexDigit - '0';
} else if (hexDigit >= 'A' && hexDigit <= 'F') {
decimalValue = hexDigit - 'A' + 10;
} else if (hexDigit >= 'a' && hexDigit <= 'f') {
decimalValue = hexDigit - 'a' + 10;
} else {
printf("Invalid hexadecimal input.\n");
return 1;
}

for (int j = 3; j >= 0; j--) {


if (decimalValue & (1 << j)) {
printf("1");
} else {
printf("0");
}
}
}

printf("\n");
return 0;
}
OUTPUT:

71. Write a C program to convert Hexadecimal


to Octal number system
#include <stdio.h>
#include <string.h>
int main() {
printf("Kashish ");
char hex[20];
printf("Enter a hexadecimal number: ");
scanf("%s", hex);

long int dec = 0, oct = 0, i = 0;

// Convert hexadecimal to decimal


for (int j = strlen(hex) - 1; j >= 0; j--) {
int digit;
if (hex[j] >= '0' && hex[j] <= '9')
digit = hex[j] - '0';
else if (hex[j] >= 'A' && hex[j] <= 'F')
digit = hex[j] - 'A' + 10;
else if (hex[j] >= 'a' && hex[j] <= 'f')
digit = hex[j] - 'a' + 10;
dec += digit * (1 << (4 * i));
i++;
}

i = 1;
// Convert decimal to octal
while (dec != 0) {
oct += (dec % 8) * i;
dec /= 8;
i *= 10;
}

printf("The octal equivalent is: %lo\n", oct);

return 0;
}
OUTPUT:
72. Write a C program to convert Hexadecimal to
Decimal number system.
#include <stdio.h>
#include <math.h>
int main() {
printf("Kashish ");
char hexNum[10];
int decimalNum = 0;
int i, j, len;
printf("Enter a hexadecimal number: ");
scanf("%s", hexNum);

// Find the length of the hexadecimal number


len = strlen(hexNum);

// Iterate through the hexadecimal number in reverse


order
for (i = 0; hexNum[i] != '\0'; i++) {
len--;
if (hexNum[i] >= '0' && hexNum[i] <= '9')
j = hexNum[i] - '0';
else if (hexNum[i] >= 'a' && hexNum[i] <= 'f')
j = hexNum[i] - 'a' + 10;
else if (hexNum[i] >= 'A' && hexNum[i] <= 'F')
j = hexNum[i] - 'A' + 10;

decimalNum += j * pow(16, len);


}
printf("Decimal equivalent: %d\n", decimalNum);

return 0;
}
OUTPUT:
Pattern Exercises
1. Star pattern programs - Write a C program to print the given star
patterns.
*
***
*****
*******
*********
Pyramid Star Pattern
*
**
**
**
*********
Hollow Pyramid Star Pattern
*********
*******
*****
***
*
Inverted Pyramid Star Pattern
*********
**
**
**
*
Hollow Inverted Pyramid Star Pattern
*
**
***
****
*****
****
***
**
*
Half Diamond Star Pattern
*
**
***
****
*****
****
***
**
*
Mirrored Half Diamond Star Pattern
#include <stdio.h>

void printPyramidStarPattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
printf("Kashish\n");
}

void printHollowPyramidStarPattern(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
printf("Kashish\n");
}
void printInvertedPyramidStarPattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
printf("Kashish\n");
}

void printHollowInvertedPyramidStarPattern(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
if (i == 1 || i == n || j == 1 || j == i) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
printf("Kashish\n");
}
void printHalfDiamondStarPattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
printf("Kashish\n");
}

void printMirroredHalfDiamondStarPattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
printf("Kashish\n");
}

int main() {
int n;

printf("Enter the number of rows: ");


scanf("%d", &n);

printf("Pyramid Star Pattern:\n");


printPyramidStarPattern(n);

printf("Hollow Pyramid Star Pattern:\n");


printHollowPyramidStarPattern(n);

printf("Inverted Pyramid Star Pattern:\n");


printInvertedPyramidStarPattern(n);

printf("Hollow Inverted Pyramid Star Pattern:\n");


printHollowInvertedPyramidStarPattern(n);

printf("Half Diamond Star Pattern:\n");


printHalfDiamondStarPattern(n);

printf("Mirrored Half Diamond Star Pattern:\n");


printMirroredHalfDiamondStarPattern(n);

return 0;
}
OUTPUT:
2. Number pattern programs - Write a C program to print the given
number patterns
Square number patterns
11111
11111
11111
11111
11111
Number pattern 1
11111
00000
11111
00000
11111
Number pattern 2
01010
01010
01010
01010
01010
Number pattern 3
11111
10001
10001
10001
11111
Number pattern 4
11111
11111
11011
11111
11111
Number pattern 5
10101
01010
10101
01010
10101Number pattern programs - Write a C program to print the given
number patterns Square number patterns 11111
11111
11111
11111
11111
#include <stdio.h>

void printSquareNumberPattern(int n) {

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

for (int j = 1; j <= n; j++) {

printf("1");

printf("\n");

printf("Kashish\n");

void printNumberPattern1(int n) {

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

for (int j = 1; j <= n; j++) {

if ((i + j) % 2 == 0) {

printf("1");

} else {

printf("0");
}

printf("\n");

printf("Kashish\n");

void printNumberPattern2(int n) {

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

for (int j = 1; j <= n; j++) {

if (j % 2 == 0) {

printf("1");

} else {

printf("0");

printf("\n");

printf("Kashish\n");
}

void printNumberPattern3(int n) {

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

for (int j = 1; j <= n; j++) {

if (i == 1 || i == n || j == 1 || j == n) {

printf("1");

} else {

printf("0");

printf("\n");

printf("Kashish\n");

void printNumberPattern4(int n) {

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

for (int j = 1; j <= n; j++) {


if (i == n / 2 + 1 || j == n / 2 + 1) {

printf("0");

} else {

printf("1");

printf("\n");

printf("Kashish\n");

void printNumberPattern5(int n) {

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

for (int j = 1; j <= n; j++) {

if ((i + j) % 2 == 0) {

printf("1");

} else {

printf("0");

}
}

printf("\n");

printf("Kashish\n");

int main() {

int n;

printf("Enter the size of the pattern: ");

scanf("%d", &n);

printf("Square Number Pattern:\n");

printSquareNumberPattern(n);

printf("Number Pattern 1:\n");

printNumberPattern1(n);

printf("Number Pattern 2:\n");


printNumberPattern2(n);

printf("Number Pattern 3:\n");

printNumberPattern3(n);

printf("Number Pattern 4:\n");

printNumberPattern4(n);

printf("Number Pattern 5:\n");

printNumberPattern5(n);

return 0;

OUTPUT :
1. Write a C program to find maximum between two numbers.

#include <stdio.h>

int main() {

double num1, num2;

printf("Enter two numbers: ");

scanf("%lf %lf", &num1, &num2);

if (num1 > num2)

printf("Maximum number is %.2lf\n", num1);

else

printf("Maximum number is %.2lf\n", num2);

printf("Kashish");

return 0;

}
OUTPUT :
2. Write a C program to find maximum between three numbers.

#include <stdio.h>

int main() {

double num1, num2, num3;

printf("Enter three numbers: ");

scanf("%lf %lf %lf", &num1, &num2, &num3);

double max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);

printf("Maximum number is %.2lf\n", max);

printf("Kashish");

return 0;

}
OUTPUT :

3. Write a C program to check whether a number is negative, positive

or zero.

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0)

printf("Positive number\n");

else if (num < 0)

printf("Negative number\n");

else
printf("Zero\n");

printf("Kashish");

return 0;

OUTPUT :
4. Write a C program to check whether a number is divisible by 5 and

11 or not.

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 5 == 0 && num % 11 == 0)

printf("Divisible by 5 and 11\n");

else

printf("Not divisible by 5 and 11\n");

printf("Kashish");

return 0;

}
OUTPUT :
5. Write a C program to check whether a number is even or odd.

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0)

printf("Even number\n");

else

printf("Odd number\n");

printf("Kashish");

return 0;

}
OUTPUT :
6. Write a C program to check whether a year is leap year or not.

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("Leap year\n");

else

printf("Not a leap year\n");

printf("Kashish");

return 0;

}
OUTPUT :
7. Write a C program to check whether a character is alphabet or not.

#include <stdio.h>

#include <ctype.h>

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if (isalpha(ch))

printf("Alphabet\n");

else

printf("Not an alphabet\n");

printf("Kashish");
return 0;

OUTPUT :
8. Write a C program to input any alphabet and check whether it is

vowel or consonant.

#include <stdio.h>

#include <ctype.h>

int main() {

char ch;

printf("Enter an alphabet: ");

scanf(" %c", &ch);

ch = tolower(ch);

if (isalpha(ch)) {

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

printf("Vowel\n");

else

printf("Consonant\n");
} else

printf("Not an alphabet\n");

printf("Kashish");

return 0;

OUTPUT :
9. Write a C program to input any character and check whether it is

alphabet, digit or special

character.

#include <stdio.h>

#include <ctype.h>

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if (isalpha(ch))

printf("Alphabet\n");

else if (isdigit(ch))
printf("Digit\n");

else

printf("Special character\n");

printf("Kashish");

return 0;

OUTPUT :
10. Write a C program to check whether a character is uppercase or

lowercase alphabet.

#include <stdio.h>

#include <ctype.h>

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if (isalpha(ch)) {

if (islower(ch))

printf("Lowercase alphabet\n");

else

printf("Uppercase alphabet\n");

} else

printf("Not an alphabet\n");

printf("Kashish");
return 0;

OUTPUT :
11. Write a C program to input week number and print week day.

#include <stdio.h>

int main() {

int weekNumber;

printf("Enter a week number (1-7): ");

scanf("%d", &weekNumber);

switch (weekNumber) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");

break;

case 4:
printf("Thursday\n");

break;

case 5:

printf("Friday\n");

break;

case 6:

printf("Saturday\n");

break;

case 7:

printf("Sunday\n");

break;

default:

printf("Invalid week number\n");

printf("Kashish");

return 0;

OUTPUT :
12. Write a C program to input month number and print number of

days in that month.

#include <stdio.h>

int main() {

int month;

printf("Enter a month number (1-12): ");

scanf("%d", &month);

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

printf("31 days\n");

break;

case 4: case 6: case 9: case 11:

printf("30 days\n");

break;

case 2:
printf("28 or 29 days (leap year)\n");

break;

default:

printf("Invalid month number\n");

printf("Kashish");

return 0;

OUTPUT :
13. Write a C program to count total number of notes in given amount.

#include <stdio.h>

int main() {

int amount;

printf("Enter the amount: ");

scanf("%d", &amount);

// Define denominations of notes

int notes[] = {500, 100, 50, 20, 10, 5, 1};

int count[7] = {0}; // Initialize an array to count each denomination

int i;

for (i = 0; i < 7; i++) {

count[i] = amount / notes[i];

amount = amount % notes[i];

}
// Display the count of each denomination

printf("Notes count:\n");

for (i = 0; i < 7; i++) {

printf("%d notes of %d\n", count[i], notes[i]);

printf("Kashish");

return 0;

OUTPUT :
14. Write a C program to count total number of notes in given amount.

#include <stdio.h>

int main() {

int amount;

printf("Enter the amount: ");

scanf("%d", &amount);

// Define denominations of notes

int notes[] = {500, 100, 50, 20, 10, 5, 1};

int count[7] = {0}; // Initialize an array to count each denomination

int i;

for (i = 0; i < 7; i++) {

count[i] = amount / notes[i];

amount = amount % notes[i];


}

// Display the count of each denomination

printf("Notes count:\n");

for (i = 0; i < 7; i++) {

printf("%d notes of %d\n", count[i], notes[i]);

printf("Kashish");

return 0;

OUTPUT :
15. Write a C program to input angles of a triangle and check whether

triangle is valid or not.

#include <stdio.h>

int main() {

int angle1, angle2, angle3;

printf("Enter the three angles of the triangle: ");

scanf("%d %d %d", &angle1, &angle2, &angle3);

if (angle1 + angle2 + angle3 == 180 && angle1 > 0 && angle2 > 0 && angle3 > 0)

printf("Valid triangle\n");

else

printf("Invalid triangle\n");

printf("Kashish");

return 0;

}
OUTPUT :
17. Write a C program to input all sides of a triangle and check

whether

triangle is valid or not.

#include <stdio.h>

int main() {

int side1, side2, side3;

printf("Enter the three sides of the triangle: ");

scanf("%d %d %d", &side1, &side2, &side3);

if (side1 + side2 > side3 && side2 + side3 > side1 && side3 + side1 > side2)

printf("Valid triangle\n");

else

printf("Invalid triangle\n");
printf("Kashish");

return 0;

OUTPUT :
18. Write a C program to check whether the triangle is equilateral, isosceles or scalene triangle.

#include <stdio.h>

int main() {

int side1, side2, side3;

printf("Enter the three sides of the triangle: ");

scanf("%d %d %d", &side1, &side2, &side3);

if (side1 == side2 && side2 == side3)

printf("Equilateral triangle\n");

else if (side1 == side2 || side2 == side3 || side3 == side1)

printf("Isosceles triangle\n");

else

printf("Scalene triangle\n");
printf("Kashish");

return 0;

OUTPUT :
19. Write a C program to find all roots of a quadratic equation.

#include <stdio.h>

#include <math.h>

int main() {

double a, b, c, discriminant, root1, root2;

printf("Enter coefficients a, b, and c: ");

scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;
if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Two distinct real roots: root1 = %.2lf, root2 = %.2lf\n", root1, root2);

} else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("One real root: root1 = root2 = %.2lf\n", root1);

} else {

double realPart = -b / (2 * a);

double imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Complex roots: root1 = %.2lf + %.2lfi, root2 = %.2lf - %.2lfi\n", realPart, imaginaryPart, realPart,

imaginaryPart);

printf("Kashish");

return 0;

OUTPUT :
20. Write a C program to calculate profit or loss.

#include <stdio.h>

int main() {

double costPrice, sellingPrice, profitLoss;

printf("Enter the cost price and selling price: ");

scanf("%lf %lf", &costPrice, &sellingPrice);


profitLoss = sellingPrice - costPrice;

if (profitLoss > 0)

printf("Profit: %.2lf\n", profitLoss);

else if (profitLoss < 0)

printf("Loss: %.2lf\n", -profitLoss);

else

printf("No profit, no loss\n");

printf("Kashish");

return 0;

OUTPUT :
21. Write a C program to input marks of five subjects Physics,

Chemistry, Biology, Mathematics

and Computer. Calculate percentage and grade according to following:

Percentage >= 90% : Grade A

Percentage >= 80% : Grade B

Percentage >= 70% : Grade C

Percentage >= 60% : Grade D

Percentage >= 40% : Grade E

Percentage < 40% : Grade F

#include <stdio.h>

int main() {

int physics, chemistry, biology, mathematics, computer;

double percentage;

printf("Enter marks in Physics, Chemistry, Biology, Mathematics, and Computer: ");

scanf("%d %d %d %d %d", &physics, &chemistry, &biology, &mathematics, &computer);

percentage = (physics + chemistry + biology + mathematics + computer) / 5.0;


printf("Percentage: %.2lf%%\n", percentage);

if (percentage >= 90)

printf("Grade A\n");

else if (percentage >= 80)

printf("Grade B\n");

else if (percentage >= 70)

printf("Grade C\n");

else if (percentage >= 60)

printf("Grade D\n");

else if (percentage >= 40)

printf("Grade E\n");

else

printf("Grade F\n");

printf("Kashish");

return 0;

OUTPUT :
22. Write a C program to input basic salary of an employee and

calculate its Gross salary according

to following:

Basic Salary <= 10000 : HRA = 20%, DA = 80%

Basic Salary <= 20000 : HRA = 25%, DA = 90%

Basic Salary >20000 : HRA = 30%, DA = 95%

#include <stdio.h>

int main() {

double basicSalary, hra, da, grossSalary;

printf("Enter the basic salary: ");

scanf("%lf", &basicSalary);

if (basicSalary <= 10000) {

hra = 0.2 * basicSalary;

da = 0.8 * basicSalary;

} else if (basicSalary <= 20000) {

hra = 0.25 * basicSalary;


da = 0.9 * basicSalary;

} else {

hra = 0.3 * basicSalary;

da = 0.95 * basicSalary;

grossSalary = basicSalary + hra + da;

printf("Gross Salary: %.2lf\n", grossSalary);

printf("Kashish");

return 0;

OUTPUT :
23. Write a C program to input electricity unit charges and calculate

total electricity bill according

to the given condition:

For first 50 units Rs. 0.50/unit

For next 100 units Rs. 0.75/unit

For next 100 units Rs. 1.20/unit

For unit above 250 Rs. 1.50/unit

An additional surcharge of 20% is added to the bill

#include <stdio.h>

int main() {

int units;

float surcharge, totalBill = 0.0;

printf("Enter the electricity unit charges: ");

scanf("%d", &units);

if (units <= 50)

totalBill = units * 0.50;


else if (units <= 150)

totalBill = 50 * 0.50 + (units - 50) * 0.75;

else if (units <= 250)

totalBill = 50 * 0.50 + 100 * 0.75 + (units - 150) * 1.20;

else

totalBill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 250) * 1.50;

surcharge = totalBill * 0.20;

totalBill += surcharge;

printf("Total electricity bill: %.2f\n", totalBill);

printf("Kashish");

return 0;

OUTPUT :
24. Write a C program to convert specified days into
years, weeks and days.
#include <stdio.h>

int main()

int days, years, weeks;

days = 1329;

// Converts days to years, weeks and days

years = days/365;

weeks = (days % 365)/7;

days = days- ((years*365) + (weeks*7));

printf("Years: %d\n", years);

printf("Weeks: %d\n", weeks);

printf("Days: %d \n", days);

return 0;

You might also like