0% found this document useful (0 votes)
108 views58 pages

C Programs for Basic Number Operations

The document contains C program code snippets to solve various problems related to printing numbers, calculating sums, checking properties of numbers like palindrome, prime, Armstrong etc. using loops and conditional statements. The problems include printing numbers in range, reversing numbers, calculating sum/product of digits, checking palindrome, prime, Armstrong properties and more. All programs take a number as input, perform some operation on it using loops and print the output.

Uploaded by

Anonymous YCXTAd
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)
108 views58 pages

C Programs for Basic Number Operations

The document contains C program code snippets to solve various problems related to printing numbers, calculating sums, checking properties of numbers like palindrome, prime, Armstrong etc. using loops and conditional statements. The problems include printing numbers in range, reversing numbers, calculating sum/product of digits, checking palindrome, prime, Armstrong properties and more. All programs take a number as input, perform some operation on it using loops and print the output.

Uploaded by

Anonymous YCXTAd
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

1.

Write a C program to print all natural numbers from 1 to n. - using while loop

# include<stdio.h>
int main ()
{
int i,n;
printf("print all the natural numbers from 1 to:");
scanf("%d",&n);
i=1;
while(i<=n)
{
printf("%d\n",i);
i++;
}
return 0;
}

1.

Write a C program to print all natural numbers in reverse (from n to 1). - using while
loop

#include <stdio.h>

int main()

{
int i, n;

printf("Enter value of n: ");


scanf("%d", &n);
for(i=n; i>=1; i--)
{
printf("%d\n", i);
}

return 0;
}

1.

Write a C program to print all alphabets from a to z. - using while loop

int main()
{
char c;
for(c='a'; c<='z'; ++c)
printf("%c ",c);
return 0;
}

Write a C program to print all even numbers between 1 to 100. - using while
loop

#include <stdio.h>

int main() {
int counter;
printf("Even numbers between 1 to 100\n");

for(counter = 1; counter <= 100; counter++) {

if(counter%2 == 0) {

printf("%d ", counter);


}
}

return 0;
}

1.

Write a C program to print all odd number between 1 to 100.

#include <stdio.h>

int main() {
int counter;
printf("odd numbers between 1 to 100\n");

for(counter = 1; counter <= 100; counter++) {

if(counter%2 == 1) {

printf("%d ", counter);


}
}

return 0;
}

Write a C program to print sum of all even numbers between 1 to n.


#include <stdio.h>

int main() {
int counter, N, sum = 0;
printf("Enter a Positive Number\n");
scanf("%d", &N);

for(counter = 1; counter <= N; counter++) {

if(counter%2 == 0) {
sum = sum + counter;
}
}
printf("Sum of all Even numbers between 1 to %d is %d", N, sum);

return 0;
}

Write a C program to print sum of all odd numbers between 1 to n.

#include <stdio.h>

int main() {
int counter, N, sum = 0;
printf("Enter a Positive Number\n");
scanf("%d", &N);

for(counter = 1; counter <= N; counter++) {


if(counter%2 == 1) {
sum = sum + counter;
}
}

printf("Sum of all odd numbers between 1 to %d is %d", N, sum);

return 0;
}

Write a C program to print table of any number.

#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
return 0;
}

Write a C program to enter any number and calculate sum of all natural numbers
between 1 to n.
#include <stdio.h>

int main()
{
int i, n, sum=0;
printf("Enter any number to find sum of first n natural numbers: ");
scanf("%d", &n);

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


{
sum += i;
}

printf("\nSum of first %d natural numbers = %d", n, sum);

return 0;
}

Write a C program to enter any number and find its first and last digit.

#include <stdio.h>

int main()
{
int i, n, sum=0;
printf("Enter any number to find sum of first and last digit: ");
scanf("%d", &n);
if(n>10)
{

sum += n % 10;
}
while(n>=10)
{
n = n / 10;
}

sum += n;

printf("Sum of first and last digit = %d", sum);

return 0;
}

Write a C program to enter any number and calculate sum of its digits.

#include <stdio.h>

void main()
{
long num, temp, digit, sum = 0;

printf("Enter the number \n");


scanf("%ld", &num);
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}

Write a C program to enter any number and calculate product of its digits.
#include <stdio.h>

int main()
{
int n;
long product=1;

printf("Enter any number to calculate product of digit: ");


scanf("%d", &n);

product = product * (n % 10);


n = n / 10;
}

printf("\nProduct of digits = %ld", product);

return 0;

Write a C program to swap first and last digits of any number.


#include <stdio.h>

int main()
{
int n, lastDigit;

printf("Enter any number: ");


scanf("%d", &n);

lastDigit = n % 10;

printf("\nLast digit = %d", lastDigit);

return 0;
}

Write a C program to enter any number and print its reverse.


#include <stdio.h>

int main()
{
int n, reverse = 0;

printf("Enter a number to reverse\n");


scanf("%d", &n);

while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n

= n/10;

printf("Reverse of entered number is = %d\n", reverse);

return 0;
}

Write a C program to enter any number and check whether the number is palindrome or
not.
#include <stdio.h>
int main()

{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}
Write a C program to enter any number and check whether the number is palindrome or
not.
#include <stdio.h>
int main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);

temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}

Write a C program to enter any number and print it in words


#include<stdio.h>

int main(){

int number,i=0,j,digit;
char * word[1000];

printf("Enter any integer: ");


scanf("%d",&number);

while(number){

digit = number %10;


number = number /10;

switch(digit){
case 0: word[i++] = "zero"; break;
case 1: word[i++] = "one"; break;
case 2: word[i++] = "two"; break;
case 3: word[i++] = "three"; break;
case 4: word[i++] = "four"; break;
case 5: word[i++] = "five"; break;
case 6: word[i++] = "six"; break;
case 7: word[i++] = "seven"; break;
case 8: word[i++] = "eight"; break;
case 9: word[i++] = "nine"; break;

}
}

for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}

return 0;

Write a C program to print all ASCII character with their values.


#include<stdio.h>

void main() {
int i = 0;
char ch;

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


printf("%c ", ch);
ch = ch + 1;
}
}

Write a C program to find power of any number using for loop.


#include <stdio.h>

int main()
{
int base, exponent;
long long power = 1;
int i;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++)
{
power = power * base;
}

printf("\n%d ^ %d = %lld", base, exponent, power);

return 0;

Write a C program to enter any number and find the sum of first and last digit of the
number.
#include <stdio.h>

int main()
{
int i, n, sum=0;
printf("Enter any number to find sum of first and last digit: ");
scanf("%d", &n);
if(n>10)
{
sum += n % 10;
}
while(n>=10)
{
n = n / 10;
}
sum += n;

printf("Sum of first and last digit = %d", sum);

return 0;
}

Write a C program to enter any number and print all factors of the number.
#include <stdio.h>
int main()
{
int n,i;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factors of %d are: ", n);
for(i=1;i<=n;++i)
{
if(n%i==0)
printf("%d ",i);
}
return 0;
}

Write a C program to enter any number and calculate its


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

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

#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i<=num1 || i<=num2; ++i)
{
if(num1%i==0 && num2%i==0)
hcf=i;
}
printf("H.C.F of %d and %d is %d", num1, num2, hcf);
getch();
}

Write a C program to find LCM of two numbers.


#include <stdio.h>
#include<conio.h>
int main()
{
int num1, num2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
max=(num1>num2) ? num1 : num2;
while(1)
{
if(max%num1==0 && max%num2==0)
{
printf("LCM of %d and %d is %d", num1, num2,max);
break;
}

++max;
}
getch();
}

Write a C program to enter any number and check whether it


is Prime number or not.
#include <stdio.h>
#include<conio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
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);
getch();
}

Write a C program to enter any number and check whether it


is Armstrong number or not.
#include <stdio.h>
#include<conio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)

{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
getch();
}

Write a C program to enter any number and check whether it


is Perfect number or not.
#include<stdio.h>
#include<conio.h>
int main() {
intnum, i = 1, sum = 0;

printf("Enter a number: ");


scanf("%d", &num);

while (i<num) {
if (num % i == 0) {
sum = sum + i;
}
i++;
}

if (sum == num)
printf("%d is a Perfect Number", i);
else
printf("%d is Non Perfect Number", i);

getch();
}

Write a C program to enter any number and check


whether it is Strong number or not.

#include <stdio.h>
#include<conio.h>
int main()
{
inti, n, num, sum=0;
long fact;
printf("Enter any number to check Strong number: ");
scanf("%d", &n);

num = n;
while(n!=0)
{
fact = 1;
for(i=1; i<=n%10; i++)
{
fact = fact * i;
}
sum = sum + fact;

n = n/10;
}
if(sum==num)

{
printf("\n%d is Strong number", num);
}
else
{
printf("\n%d is not Strong number", num);
}

getch();
}

Write a C program to print all Prime numbers between 1 to n.


#include <stdio.h>
#include<conio.h>
int main()
{
inti, j, n, isPrime;
printf("Find prime numbers between 1 to : ");
scanf("%d", &n);
printf("\nAll prime numbers between 1 to %d are:\n", n);

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


{
isPrime = 1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
isPrime = 0;
break;
}
}
if(isPrime==1)
{
printf("%d is Prime number\n", i);
}
}

getch();
}

Write a C program to print all Armstrong numbers between 1 to


n.
#include <stdio.h>
#include<conio.h>
int main()
{
inti, n, num, lastDigit, sum = 0;
printf("Enter any number to find Armstrong number upto: ");
scanf("%d", &n);

printf("All Armstrong numbers between 1 to %d:\n", n);


for(i=1; i<=n; i++)
{
num = i;
sum = 0;

while(num!=0)
{
lastDigit = num % 10;
sum += lastDigit * lastDigit * lastDigit;
num = num / 10;

}
if(i==sum)
{
printf("%d is ARMSTRONG NUMBER\n", i);
}
}

getch();
}

Write a C program to print all Perfect numbers between 1 to n.


#include <stdio.h>
#include<conio.h>
int main()
{
inti, j, n, sum = 0;
printf("Enter any number to print perfect number up to: ");
scanf("%d", &n);

printf("\nAll Perfect numbers between 1 to %d:\n", n);


for(i=1; i<=n; i++)
{
sum = 0;
for(j=1; j<i; j++)
{
if(i%j==0)
{
sum += j;
}
}
if(sum == i)
{
printf("%d is Perfect Number\n", i);
}
}

getch();
}

Write a C program to print all Strong numbers between 1 to


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

int main()
{
inti, j, cur, n;
long fact, sum;
printf("Find Strong numbers between 1 to ");
scanf("%d", &n);

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


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

sum = 0;
while(cur!=0)
{
fact = 1;
for( j=1; j<=cur%10; j++)
{
fact = fact * j;
}

sum = sum + fact;

cur = cur / 10;


}
if(sum==i)
{
printf("%d is Strong number\n", i);
}
}
getch();
return 0;
}

Write a C program to enter any number and print its prime


factors.
#include <stdio.h>
#include <conio.h>
int main()
{
inti, j, num, isPrime;
printf("Enter any number to print Prime factors: ");
scanf("%d", &num);

printf("\nAll Prime Factors of %d are: \n", num);


for(i=2; i<=num; i++)
{
if(num%i==0)
{
isPrime = 1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{

isPrime = 0;
break;
}
}
if(isPrime==1)
{
printf("%d\n", i);
}
}
}

getch();
}

Write a C program to find sum of all prime numbers between 1


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

int main()
{
inti, j, n, isPrime, sum=0;
printf("Find sum of all prime between 1 to : ");
scanf("%d", &n);
for(i=2; i<=n; i++)
{
isPrime = 1;
for(j=2; j<=i/2 ;j++)
{
if(i%j==0)
{
isPrime = 0;
break;
}
}
if(isPrime==1)
{
sum += i;
}
}

printf("Sum of all prime numbers between 1 to %d = %d", n, sum);

getch();
}

Write a C program to print Fibonacci series up to n terms.


#include <stdio.h>
#include <conio.h>
int main()
{
int count, n, t1=0, t2=1, display=0;
printf("Enter number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: %d+%d+", t1, t2);
count=2;
while (count<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;

printf("%d+",display);
}
getch();
}

Write a C program to find one's complement of a binary number.


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

#define SIZE 8

int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1];
inti, error=0;
printf("Enter any %d bit binary value: ", SIZE);
gets(binary);
for(i=0; i<SIZE; i++)
{
if(binary[i]=='1')

{
onesComp[i] = '0';
}
else if(binary[i]=='0')
{
onesComp[i] = '1';
}
else
{
printf("Invalid Input");
error = 1;
break;
}
}
onesComp[SIZE] = '\0';

if(error==0)
{
printf("\nOriginal binary = %s\n", binary);
printf("Ones complement = %s", onesComp);
}
getch();

return 0;
}

Write a C program to find two's complement of a binary


number.
#inclde <stdio.h>
#include <string.h>
#include <conio.h>
#define SIZE 8

int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
inti, carry=1;
printf("Enter any %d bit binary value: ", SIZE);
gets(binary);
for(i=0; i<SIZE; i++)
{
if(binary[i]=='1')
{
onesComp[i] = '0';

}
else if(binary[i]=='0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i]=='1' && carry==1)
{
twosComp[i] = '0';
}
else if(onesComp[i]=='0' && carry==1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}

}
twosComp[SIZE] = '\0';

printf("\nOriginal binary value = %s\n", binary);


printf("One's complement = %s\n", onesComp);
printf("Two's complement = %s", twosComp);
getch();
return 0;
}

Write a C program to convert Binary to Octal number system.


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

intbinarynum, octalnum = 0, j = 1, remainder;

printf("Enter the value for binary number: ");


scanf("%ld", &binarynum);

while (binarynum != 0)
{
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
printf("Equivalent octal value: %lo", octalnum);
getch();
return 0;
}

Write a C program to convert Binary to Decimal number


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

longintbinaryNumber,decimalNumber=0,j=1,remainder;

printf("Enter any number any binary number: ");


scanf("%ld",&binaryNumber);

while(binaryNumber!=0){
remainder=binaryNumber%10;
decimalNumber=decimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}

printf("Equivalent decimal value: %ld",decimalNumber);


getch();
return 0;
}

Write a C program to convert Binary to Hexadecimal number


system.
#include <stdio.h>
#include<conio.h>
int main()
{
longintbinaryval, hexadecimalval = 0, i = 1, remainder;

printf("Enter the binary number: ");


scanf("%ld", &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("Equivalent hexadecimal value: %lX", hexadecimalval);
getch();
return 0;
}

Write a C program to convert Octal to Binary number system.


#include <stdio.h>
#include <conio.h>
int main()
{
int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};
longlong octal, tempOctal, binary, place;

int rem;
printf("Enter any Octal number: ");
scanf("%lld", &octal);
tempOctal = octal;

binary = 0;
place = 1;
while(tempOctal!=0)
{
rem = tempOctal % 10;
binary = (OCTALVALUES[rem] * place) + binary;
tempOctal /= 10;

place *= 1000;
}

printf("\nOctal number = %lld\n", octal);


printf("Binary number = %lld", binary);
getch();
return 0;
}

Write a C program to convert Octal to Decimal number


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

int main()
{
long long octal, tempOctal, decimal;
int rem, place;
printf("Enter any octal number: ");
scanf("%lld", &octal);
tempOctal = octal;

decimal = 0;
place = 0;
while(tempOctal!=0)
{
rem = tempOctal % 10;
decimal += pow(8, place) * rem;
tempOctal /= 10;

place++;
}

printf("\nOctal number = %lld\n", octal);


printf("Decimal number = %lld", decimal);

return 0;
}

Write a C program to convert Octal to Hexadecimal number


system.
#include <stdio.h>
#include <string.h>
int main() {
int octalDigitToBinary[8] = {0, 1, 10, 11, 100, 101, 110, 111};
int hexDigitToBinary[16] = {0, 1, 10, 11, 100, 101, 110, 111, 1000,
1001, 1010, 1011, 1100, 1101, 1110, 1111};
char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
char hexadecimalNumber[30];
long long octalNumber, binaryNumber = 0, position;
int digit, fourDigit, i;
printf("Enter an Octal Number\n");
scanf("%ld", &octalNumber);
position = 1;

while(octalNumber != 0) {
digit = octalNumber % 10;
binaryNumber = (octalDigitToBinary[digit]*position)+binaryNumber;
octalNumber /= 10;
position *= 1000;
}
position = 0;
while(binaryNumber != 0){
fourDigit = binaryNumber%10000;
for(i = 0; i < 16; i++){
if(hexDigitToBinary[i] == fourDigit){
hexadecimalNumber[position] = hexDigits[i];
break;
}
}
position++;
binaryNumber /= 10000;
}
hexadecimalNumber[position] = '\0';
strrev(hexadecimalNumber);
printf("HexaDecimal Number = %s", hexadecimalNumber);
return 0;
}

Write a C program to convert Decimal to Binary number


system.
#include <stdio.h>
#include <conio.h>
long decimalToBinary(long n);
int main() {
long decimal;
printf("Enter a decimal number\n");
scanf("%ld", &decimal);
printf("Binary number of %ld is %ld", decimal,
decimalToBinary(decimal));

getch();
return 0;
}
long decimalToBinary(long n) {
int remainder;
long binary = 0, i = 1;
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}

Write a C program to convert Decimal to Octal number


system.
#include <stdio.h>

int main()
{

long long decimal, tempDecimal, octal;


int i, rem, digit = 1;

octal = 0;
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
while(tempDecimal!=0)
{
rem = tempDecimal % 8;

octal = (rem * digit) + octal;

tempDecimal /= 8;
digit *= 10;
}

printf("\nDecimal number = %lld\n", decimal);


printf("Octal number = %lld", octal);

return 0;
}

Write a C program to convert Decimal to Hexadecimal


number system.
#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
temp = quotient % 16;
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
printf("Equivalent hexadecimal value of decimal number %d:
",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%c",hexadecimalNumber[j]);
return 0;
}

Write a C program to convert Hexadecimal to Binary number


system.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main() {
long long decimalNumber=0;
char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
char hexadecimal[30];
int i, j, power=0, digit;
printf("Enter a Hexadecimal Number\n");
scanf("%s", hexadecimal);
for(i=strlen(hexadecimal)-1; i >= 0; i--) {
for(j=0; j<16; j++){
if(hexadecimal[i] == hexDigits[j]){
decimalNumber += j*pow(16, power);
}
}
power++;
}
printf("Decimal Number : %ld", decimalNumber);
return 0;
}

Write a C program to convert Hexadecimal to Octal number


system.
#include <stdio.h>
#include <string.h>
int main() {
int hexDigitToBinary[16] = {0, 1, 10, 11, 100, 101, 110, 111,

1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111};


char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int octalDigitToBinary[8] = {0,1,10,11,100,101,110,111};
char hexadecimal[30];
long long binaryNumber =0, octalNumber;
int i = 0, j, index=0, multiple = 1, threeDigits;
printf("Enter a Hexadecimal Number\n");
scanf("%s", hexadecimal);
for(i=0; hexadecimal[i] != '\0'; i++) {
for(j = 0; j < 16; j++){
if(hexDigits[j] == hexadecimal[i]){
binaryNumber = binaryNumber*10000 + hexDigitToBinary[j];
}
}
}
while(binaryNumber != 0) {
threeDigits = binaryNumber % 1000;
for(i = 0; i < 8; i++) {
if(octalDigitToBinary[i] == threeDigits) {
octalNumber = (i * multiple) + octalNumber;
break;
}
}
binaryNumber = binaryNumber/1000;
multiple *= 10;
}
printf("Octal Mumber : %ld", octalNumber);
return 0;
}

Write a C program to convert Hexadecimal to Decimal number


system.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main() {
long long decimalNumber=0;
char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
char hexadecimal[30];
int i, j, power=0, digit;
printf("Enter a Hexadecimal Number\n");
scanf("%s", hexadecimal);
for(i=strlen(hexadecimal)-1; i >= 0; i--) {
for(j=0; j<16; j++){
if(hexadecimal[i] == hexDigits[j]){
decimalNumber += j*pow(16, power);
}
}
power++;
}
printf("Decimal Number : %ld", decimalNumber);
return 0;
}

Write a C program to print Pascal triangle upto n rows.


#include<stdio.h>
int main(){
int length,i,j,k;
printf("Enter the length of pascal's triangle : ");
scanf("%d",&length);

for(i=1;i<=length;i++) {
for(j=1;j<=length-i;j++)
printf(" ");
for(k=1;k<i;k++)
printf("%d",k);
for(k=i;k>=1;k--)
printf("%d",k);
printf("\n");
}
return 0;
}

Star pattern programs - Write a C program to print the given star


patterns.
#include <stdio.h>
int main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see
");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;

for ( c = 1 ; c <= 2*row - 1 ; c++ )


printf("*");
printf("\n");
}
return 0;
}

You might also like