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

C Program Assignment Q

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

C Program Assignment Q

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

C program to find factorial of a number

#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number
doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}
C Program to swap two bits

#include<stdio.h>
int swapBits(unsigned int n, unsigned int p1, unsigned int p2)
{
unsigned int bit1 = (n >> p1) & 1;
unsigned int bit2 = (n >> p2) & 1;
unsigned int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
unsigned int result = n ^ x;
}
int main()
{
int res = swapBits(28, 0, 3);
printf("Result = %d ", res);
return 0;
}
C Program to swap two words/nibbles of a byte

#include <stdio.h>
unsigned char swapTwoNibbles(unsigned char n)
{
unsigned char num;
num = ((n & 0x0F) << 4 | (n & 0xF0) >> 4);
return num;
}
int main()
{
unsigned char number;
unsigned char revNumber;
printf("Enter an integer number (One byte number):");
scanf("%u", &number);
revNumber = swapTwoNibbles(number);
printf("\nEntered Number was : %u \nNumber after swapping
nibbles : %u", number, revNumber);
return 0;
}
C Program to swap two numbers without using third variable

#include <stdio.h>
int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("\nEnter Value of y ");
scanf("%d", &y);
x = x + y;
y = x - y;
x = x - y;
printf("\nAfter Swapping: x = %d, y = %d", x, y);
return 0;
}
C Program to check whether number is palindrome or not

#include <stdio.h>
int main()
{
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
C program to whether number is perfect square or not

#include <stdio.h>
#include <math.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
double sqrt_num = sqrt(num);
if (sqrt_num == (int)sqrt_num) {
printf("%d is a perfect square.\n", num);
} else {
printf("%d is not a perfect square.\n", num);
}
return 0;
}
C program to print “Hello World” without using semicolon

#include<stdio.h>
void main() {
//Method One
if(printf("Hello world"))
{
}
//Method Two
while(!printf("Hello world"))
{
}
//Method Three
switch(printf("Hello world"))
{
}
}
C program to check whether number is prime or not

#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
C program to check whether a number is Armstrong or not

#include <math.h>
int main() {
int num, temp, originalNum, remainder, sum = 0, n = 0;
printf("Input a number: ");
scanf("%d", &num);
originalNum = num;
for (temp = num; temp != 0; n++) {
temp /= 10;
}
for (temp = num; temp != 0; temp /= 10)
{
remainder = temp % 10;
sum += pow(remainder, n);
}
if (sum == originalNum) {
printf("%d is an Armstrong number.\n", originalNum);
}
Else {
printf("%d is not an Armstrong number.\n", originalNum);
}
return 0;
}
C program to check whether a number if even or odd

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0){
printf("%d is even.", num);
}
else {
printf("%d is odd.", num);
}
return 0;
}

You might also like