Computer Assignment Abhishek
Computer Assignment Abhishek
Introduction to C – Programming
Batch:(2023 – 26)
BCA 1st YEAR
SUBMITTED BY: SUBMITTED TO:
Name –ABHISHEK SHARMA MS.POOJA CHAHAR
Student ID -2316012165 ASSISTANT PROFESSOR
CSIT, GRAPHIC ERA, DDN
#include <stdio.h>
int main() {
printf ("Hello World");
return 0;
}
OUTPUT:
#include <stdio.h>
intmain() {
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:
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>
intmain() {
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:
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:
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("ABHISHEK SHARMA\n");
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid OUTPUT. 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;
}
OUTPUT:
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[] = "manas";
char correctPassword[] = "123456789";
char enteredUsername[50];
char enteredPassword[50];
printf("ABHISHEK SHARMA\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 OUTPUT the positive number from the user to perform the left shift operator.
#include <stdio.h>
int main() {
int num, shift;
{
printf("ABHISHEK SHARMA\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;
}
Output:
18. WAP to OUTPUT the positive number from the user to perform the right shift operator.
#include <stdio.h>
int main() {
int num, shift;
{
printf("ABHISHEK SHARMA\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("ABHISHEK SHARMA\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;
}
Output:
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("ABHISHEK SHARMA\n");
printf("Enter the first integer: ");
scanf("%d", &num1);
return 0;
}
Output:
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("ABHISHEK SHARMA\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;
}
Output:
22. WAP to identify gender in single character and print full gender (Ex: if OUTPUT is 'M' or 'm' – it should print
"Male").
#include <stdio.h>
int main() {
char gender;
printf("ABHISHEK SHARMA\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 OUTPUT\n");
}
return 0;
}
Output:
#include <stdio.h>
int main() {
printf("ABHISHEK SHARMA");
int n;
// OUTPUT the value of n
printf("Enter a natural number (n): ");
scanf("%d", &n);
if (n <= 0) {
printf("Please enter a positive natural number.\n");
} else {
// Print numbers from n down to 1
for (int i = n; i >= 1; i--) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
OUTPUT:
24.Write a C program to print all alphabets from a to z.
#include<stdio.h>
int main() {
int i;
printf("ABHISHEK SHARMA\n");
printf("\nAlphabets from (a to z) :\n");
}
printf("\n\nSum of all odd numbers from (1 to n) = %d", sum);
return 0;
}
OUTPUT:
#include<stdio.h>
int main()
{
printf("\nABHISHEK SHARMA");
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;
}
OUTPUT:
33. Write a C program to find first and last digit of a number
#include <stdio.h
int main() {
printf("ABHISHEK SHARMA");
int number, firstDigit, lastDigit;
return 0;
}
OUTPUT:
34. Write a C program to find sum of first and last digit of a number
#include <stdio.h>
int main() {
printf("ABHISHEK SHARMA");
int num, firstDigit, lastDigit, sum;
return 0;
}
OUTPUT:
35. Write a C program to swap first and last digits of a number.
#include <stdio.h>
int main() {
printf("ABHISHEK SHARMA");
int number, originalNumber, firstDigit, lastDigit, temp;
printf("Enter a number: ");
scanf("%d", &number);
originalNumber = number;
return 0;
}
OUTPUT:
36. Write a C program to calculate sum of digits of a number.
#include <stdio.h>
int main() {
printf("ABHISHEK SHARMA");
int num, sum = 0, digit;
int main() {
printf("ABHISHEK SHARMA");
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;
}
OUTPUT:
return 0;
}
OUTPUT:
int main() {
printf("ABHISHEK SHARMA");
int num, digit, count;
int frequency[10] = {0}; // Initialize an array to store the frequency of each digit
return 0;
}
OUTPUT:
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("ABHISHEK SHARMA");
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;
}
OUTPUT:
42. Write a C program to print all ASCII character with their values.
#include <stdio.h>
int main() {
printf("ABHISHEK SHARMA");
int i;
for (i = 0; i < 128; i++) {
printf("ASCII value %d represents character: %c\n", i, i);
}
return 0;
}
OUTPUT:
43. Write a C program to find power of a number using for loop.
#include <stdio.h
int main() {
printf("ABHISHEK SHARMA");
double base, exponent, result = 1;
// OUTPUT 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;
}
OUTPUT:
if (number <= 0) {
printf("Please enter a positive integer.\n");
return 1; // Exit with an error code
}
printf("Factors of %d are: ", number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 1; // Exit with an error code
}
return 0;
}
OUTPUT:
int main() {
printf("ABHISHEK SHARMA");
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;
}
OUTPUT:
return 1;
}
int main() {
printf("ABHISHEK SHARMA");
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("\n");
return 0;
}
OUTPUT:
return 1;
}
int main() {
printf("ABHISHEK SHARMA");
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("ABHISHEK SHARMA");
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Prime factors of %d are: ", n);
primeFactors(n);
return 0;
}
OUTPUT:
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("ABHISHEK SHARMA");
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:
#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("ABHISHEK SHARMA");
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("ABHISHEK SHARMA");
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:
int main() {
printf("ABHISHEK SHARMA");
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
int main() {
printf("ABHISHEK SHARMA");
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:
int main() {
printf("ABHISHEK SHARMA");
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
return 0;
}
OUTPUT:
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("ABHISHEK SHARMA");
char binary[32]; // Assuming a maximum of 32-bit binary number
int length, i;
return 0;
}
OUTPUT:
int main() {
printf ("ABHISHEK SHARMA");
char binary[32]; // Assuming a maximum of 32-bit binary number
int main() {
printf("ABHISHEK SHARMA");
char binary[32]; // Assuming a maximum of 32-bit binary number
return decimal;
}
int main() {
printf("ABHISHEK SHARMA");
char binary[32]; // Assuming a maximum of 32-bit binary number
return 0;
}
OUTPUT:
63. Write a C program to convert Binary to Hexadecimal number system.
#include <stdio.h>
#include <string.h>
// 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');
}
int main() {
printf("ABHISHEK SHARMA");
char binary[32]; // Assuming a maximum of 32-bit binary number
return 0;
}
OUTPUT:
int main() {
printf("ABHISHEK SHARMA");
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;
return decimal;
}
int main() {
printf("ABHISHEK SHARMA");
char octal[32]; // Assuming a maximum of 32-bit octal number
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>
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;
}
int main() {
char octal[100];
decimalToHexadecimal(decimal);
return 0;
}
OUTPUT:
int binary[32];
int i = 0;
printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}
int main() {
printf("ABHISHEK SHARMA ");
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToBinary(decimal);
return 0;
}
OUTPUT:
int main() {
printf("ABHISHEK SHARMA ");
int decimal, remainder, i = 0;
char hexadecimal[100];
printf("Enter a decimal number: ");
scanf("%d", &decimal);
return 0;
}
OUTPUT:
int main() {
printf("ABHISHEK SHARMA ");
char hexadecimal[100];
printf("Enter a hexadecimal number: ");
scanf("%s", hexadecimal);
printf("\n");
return 0;
}
OUTPUT:
return 0;
}
Pattern Exercises
1. Star pattern programs - Write a C program to print the given star patterns.
*
***
*****
*******
*********
Pyramid Star Pattern
#include <stdio.h>
void printStarPattern(int n) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= 2*i-1; j++) {
printf("*");
}
printf("\n");
}
}
int main() {
int n = 5; // Change this value to adjust the number of rows
printStarPattern(n);
return 0;
}
2. *
**
**
**
*********
Hollow Pyramid Star Pattern
#include <stdio.h>
void printHollowPyramid(int n) {
int i, j;
// Print upper part of the pyramid
for(i = 1; i <= n; i++) {
for(j = i; j < n; j++) {
printf(" ");
}
for(j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1 || i == n) {
printf("*");
}
else {
printf(" ");
}
}
printf("\n");
}
}
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
if(n <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
printHollowPyramid(n);
return 0;
}
*********
*******
*****
***
*
3.Inverted Pyramid Star Pattern
#include <stdio.h>
int main() {
int i, j, rows;
return 0;
}
*********
**
**
**
*
4.Hollow Inverted Pyramid Star Pattern
#include <stdio.h>
int main() {
int rows, i, j;
printf("*");
}
printf("\n");
}
return 0;
}
*****
*
***
****
****
***
**
*
5.Half Diamond Star Pattern
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Print upper half of the diamond
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
// Print lower half of the diamond
for(i = rows-1; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
*
**
***
****
*****
****
***
**
*
6.Mirrored Half Diamond Star Pattern
#include <stdio.h>
void printHalfDiamond(int n) {
int i, j;
// Upper half of the pattern
for(i = 1; i <= n; i++) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
// Lower half of the pattern
for(i = n-1; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
}
int main() {
int n;
// Get user input for the number of rows
printf("Enter the number of rows: ");
scanf("%d", &n);
// Call the function to print the pattern
printHalfDiamond(n);
return 0;
}
2. Number pattern programs - Write a C program to print the given number patterns
Square number patterns
11111
11111
11111
11111
11111
#include <stdio.h>
int main() {
int i, j;
return 0;
}
Number pattern 1
11111
00000
11111
00000
11111
#include <stdio.h>
int main() {
int i, j;
return 0;
}
Number pattern 2
01010
01010
01010
01010
01010
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= 5; j++) {
if(j % 2 == 0) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}
return 0;
}
Number pattern 3
11111
10001
10001
10001
11111
#include <stdio.h>
int main() {
int n = 5; // Number of rows and columns
Number pattern 4
11111
11111
11011
11111
11111
#include <stdio.h>
int main() {
int n = 5; // Size of the pattern (5x5 in this case)
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if (i == n/2 && j == n/2) { // If we're at the center, print 0
printf("0");
} else {
printf("1");
}
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Number pattern 5
10101
01010
10101
01010
10101
#include <stdio.h>
int main() {
int rows = 5;
int cols = 5;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if((i+j) % 2 == 0) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}
return 0;
}
If…Else Exercises
int main() {
int num1, num2;
int main() {
int num;
if (num > 0) {
printf("%d is positive.\n", num);
} else if (num < 0) {
printf("%d is negative.\n", num);
} else {
printf("%d is zero.\n", num);
}
return 0;
}
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;
return 0;
}
5. Write a C program to check whether a number is even or odd.
#include <stdio.h>
int main() {
int num;
// Prompt the user to enter a number
printf("Enter an integer: ");
scanf("%d", &num);
// Check if the number is even or odd
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
6. Write a C program to check whether a year is leap year or not.
#include <stdio.h>
int main() {
int year;
return 0;
}
THANK YOU