Kashish c File (3)
Kashish c File (3)
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() {
#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:
OUTPUT:
#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:
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:
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:
#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
}
}
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");
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;
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);
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);
return 0;
}
#include <stdio.h>
int main() {
int num1, num2;
printf("KASHISH\n");
printf("Enter the first integer: ");
scanf("%d", &num1);
#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;
}
#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");
#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:
#include<stdio.h>
int main()
{
printf("Kashish ");
int i, n, sum = 0;
printf("\nEnter any number: ");
scanf("%d", &n);
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;
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;
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;
originalNumber = number;
// Get the last digit
lastDigit = number % 10;
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;
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;
while (num != 0) {
digit = num % 10; // Get the last digit
product *= digit; // Multiply it with the product
num = num / 10; // Remove the last digit
}
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;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return 0;
}
INPUT:
int main() {
printf("Kashish ");
int num, digit, count;
int frequency[10] = {0}; // Initialize an array to store
the frequency of each digit
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>
int main() {
printf("Kashish ");
int num, digit, reversed = 0;
if (num == 0) {
printf("Zero\n");
return 0;
}
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;
int main() {
printf("Kashish ");
int 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;
if (number < 0) {
printf("Factorial is not defined for negative
numbers.\n");
return 1; // Exit with an error code
}
return 0;
}
INPUT:
return 0;
}
INPUT:
int main() {
printf("Kashish ");
int num1, num2;
return 0;
}
INPUT:
int main() {
printf("Kashish ");
int 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:
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);
printf("\n");
return 0;
}
INPUT:
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);
int main() {
printf("Kashish ");
int n;
printf("Enter a number: ");
scanf("%d", &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>
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;
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>
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;
return 0;
}
OUTPUT:
54. Write a C program to check whether a number
is Perfect number or not.
#include <stdio.h>
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);
return 0;
}
OUTPUT:
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;
}
int main() {
printf("Kashish ");
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
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("\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;
return 0;
}
OUTPUT:
int main() {
printf ("Kashish ');
char binary[32]; // Assuming a maximum of 32-bit
binary number
return 0;
}
OUTPUT:
int main() {
printf("Kashish ");
char binary[32]; // Assuming a maximum of 32-bit
binary number
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
return 0;
}
OUTPUT:
int main() {
printf("Kashish ");
char binary[32]; // Assuming a maximum of 32-bit
binary number
return 0;
}
OUTPUT:
int main() {
printf("Kashish ");
char octal[32]; // Assuming a maximum of 32-bit
octal number
char binary[128]; // To store the binary equivalent
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;
int main() {
printf("Kashish ");
char octal[32]; // Assuming a maximum of 32-bit octal
number
if (decimal != -1) {
printf("Decimal representation: %d\n", decimal);
}
return 0;
}
OUTPUT:
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;
}
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;
int main() {
printf("Kashish ");
int decimal;
decimalToBinary(decimal);
return 0;
}
OUTPUT:
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++;
}
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);
printf("\n");
return 0;
}
OUTPUT:
i = 1;
// Convert decimal to octal
while (dec != 0) {
oct += (dec % 8) * i;
dec /= 8;
i *= 10;
}
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);
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;
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) {
printf("1");
printf("\n");
printf("Kashish\n");
void printNumberPattern1(int n) {
if ((i + j) % 2 == 0) {
printf("1");
} else {
printf("0");
}
printf("\n");
printf("Kashish\n");
void printNumberPattern2(int n) {
if (j % 2 == 0) {
printf("1");
} else {
printf("0");
printf("\n");
printf("Kashish\n");
}
void printNumberPattern3(int n) {
if (i == 1 || i == n || j == 1 || j == n) {
printf("1");
} else {
printf("0");
printf("\n");
printf("Kashish\n");
void printNumberPattern4(int n) {
printf("0");
} else {
printf("1");
printf("\n");
printf("Kashish\n");
void printNumberPattern5(int n) {
if ((i + j) % 2 == 0) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
printf("Kashish\n");
int main() {
int n;
scanf("%d", &n);
printSquareNumberPattern(n);
printNumberPattern1(n);
printNumberPattern3(n);
printNumberPattern4(n);
printNumberPattern5(n);
return 0;
OUTPUT :
1. Write a C program to find maximum between two numbers.
#include <stdio.h>
int main() {
else
printf("Kashish");
return 0;
}
OUTPUT :
2. Write a C program to find maximum between three numbers.
#include <stdio.h>
int main() {
double max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
printf("Kashish");
return 0;
}
OUTPUT :
or zero.
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if (num > 0)
printf("Positive number\n");
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;
scanf("%d", &num);
else
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;
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;
scanf("%d", &year);
printf("Leap year\n");
else
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;
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;
ch = tolower(ch);
if (isalpha(ch)) {
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
character.
#include <stdio.h>
#include <ctype.h>
int main() {
char 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;
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;
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("Kashish");
return 0;
OUTPUT :
12. Write a C program to input month number and print number of
#include <stdio.h>
int main() {
int month;
scanf("%d", &month);
switch (month) {
printf("31 days\n");
break;
printf("30 days\n");
break;
case 2:
printf("28 or 29 days (leap year)\n");
break;
default:
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;
scanf("%d", &amount);
int i;
}
// Display the count of each denomination
printf("Notes count:\n");
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;
scanf("%d", &amount);
int i;
printf("Notes count:\n");
printf("Kashish");
return 0;
OUTPUT :
15. Write a C program to input angles of a triangle and check whether
#include <stdio.h>
int main() {
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
#include <stdio.h>
int main() {
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() {
printf("Equilateral triangle\n");
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() {
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
printf("Two distinct real roots: root1 = %.2lf, root2 = %.2lf\n", root1, root2);
} else if (discriminant == 0) {
} else {
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() {
if (profitLoss > 0)
else
printf("Kashish");
return 0;
OUTPUT :
21. Write a C program to input marks of five subjects Physics,
#include <stdio.h>
int main() {
double percentage;
printf("Grade A\n");
printf("Grade B\n");
printf("Grade C\n");
printf("Grade D\n");
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
to following:
#include <stdio.h>
int main() {
scanf("%lf", &basicSalary);
da = 0.8 * basicSalary;
} else {
da = 0.95 * basicSalary;
printf("Kashish");
return 0;
OUTPUT :
23. Write a C program to input electricity unit charges and calculate
#include <stdio.h>
int main() {
int units;
scanf("%d", &units);
else
totalBill += surcharge;
printf("Kashish");
return 0;
OUTPUT :
24. Write a C program to convert specified days into
years, weeks and days.
#include <stdio.h>
int main()
days = 1329;
years = days/365;
return 0;