c language problems

Download as pdf or txt
Download as pdf or txt
You are on page 1of 91

Problem no.

51:Write a C program to read an array of length 6, change the first element by the last, the
second element by the fifth and the third element by the fourth. Print the elements of the modified
array.:
#include <stdio.h>
#define AL 5
int main() {

int array_n[AL], i, temp;

printf("Input the 5 members of the array:\n");


for(i = 0; i < AL; i++) {
scanf("%d", &array_n[i]);
}
for(i = 0; i < AL; i++) {
if(i < (AL/2)) {
temp = array_n[i];
array_n[i] = array_n[AL-(i+1)];
array_n[AL-(i+1)] = temp;
}
}

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


printf("array_n[%d] = %d\n", i, array_n[i]);
}

return 0;
}

problem no.52: Write a C program to read an array of length 6 and find the smallest element and its
position.

#include <stdio.h>
int main() {
int e, i, sval, position;

printf("\nInput the length of the array: ");


scanf("%d", &e);

int v[e];

printf("\nInput the array elements:\n ");


for(i = 0; i < e; i++) {
scanf("%d", &v[i]);
}
sval = v[0];
position = 0;

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


if(sval > v[i]) {
sval = v[i];
position = i;
}
}

printf("Smallest Value: %d\n", sval);


printf("Position of the element: %d\n", position);

return 0;
}

problem 53: Write a C program that accepts the principle, rate of interest, and time and calculates
simple interest

#include<stdio.h>
int main() {
int p, r, t, int_amt;
printf("Input principle, Rate of interest & time to find simple interest: \n");
scanf("%d%d%d", &p, &r, &t);
int_amt = (p * r * t) / 100;

printf("Simple interest = %d", int_amt);

return 0;
}

Problem no.54: Write a C program that accepts a distance in centimeters and prints the corresponding
value in inches.
#include <stdio.h>
#define INCH_TO_CM 2.54
int main() {
double inch, cm;
printf("Input the distance in cm:\n");
scanf("%lf", &cm);
inch = cm / INCH_TO_CM;
printf("Distance of %0.2lf cms is = %0.2lf inches\n", cm, inch);

return 0;
}

problem no.55: Write a C program that swaps two numbers without using a third variable.

#include<stdio.h>
int main()
{
int x, y;
printf("Input value for x & y: \n");
scanf("%d%d",&x,&y);
printf("Before swapping the value of x & y: %d %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\nAfter swapping the value of x & y: %d %d",x,y);
return 0;
}
problem no.56: Write a C program to shift given data by two bits to the left.
#include<stdio.h>
int main() {
int a, b;

printf("Read the integer from keyboard-");


scanf("%d",&a);

printf("\nInteger value = %d ",a);

a <<= 2;
b = a;

printf("\nThe left shifted data is = %d ",b);

return 0;
}
problem no. 57:Write a C program to reverse and print a given number

#include<stdio.h>
int main() {
int num, x, r_num = 0;

printf("Input a number: ");


scanf("%d", &num);

printf("\nThe original number = %d", num);

while (num >= 1) {


x = num % 10;
r_num = r_num * 10 + x;
num = num / 10;
}

printf("\nThe reverse of the said number = %d", r_num);

return 0;
}

problem no. 58: Write a C program that accepts 4 real numbers from the keyboard and prints out the
difference between the maximum and minimum values of these four numbers.

#include <stdio.h>
int main() {
double a1, a2, a3, a4;
double max, min;

printf("Input four numbers: \n");


scanf("%lf%lf%lf%lf", &a1, &a2, &a3, &a4);

if (a1 >= a2 && a1 >= a3 && a1 >= a4)


max = a1;
else if (a2 >= a1 && a2 >= a3 && a2 >= a4)
max = a2;
else if (a3 >= a1 && a3 >= a2 && a3 >= a4)
max = a3;
else
max = a4;

if (a1 <= a2 && a1 <= a3 && a1 <= a4)


min = a1;
else if (a2 <= a1 && a2 <= a3 && a2 <= a4)
min = a2;
else if (a3 <= a1 && a3 <= a2 && a3 <= a4)
min = a3;
else
min = a4;
printf("Difference is %0.4lf\n", max - min);

return 0;
}

problem no. 59: Write a C program to display the sum of series 1 + 1/2 + 1/3 + ………. + 1/n.

#include<stdio.h>
int main() {
int num, i, sum = 0;
printf("Input any number: ");
scanf("%d", &num);
printf("1 + ");
for(i = 2; i <= num - 1; i++)
printf(" 1/%d +", i);
for(i = 1; i <= num; i++)
sum = sum + i;
printf(" 1/%d", num);
printf("\nSum = 1/%d", sum + 1/num);

return 0;
}
problem no.60:
#include <stdio.h>
int main() {

enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat};

printf("Sun = %d", Sun);


printf("\nMon = %d", Mon);
printf("\nTue = %d", Tue);
printf("\nWed = %d", Wed);
printf("\nThu = %d", Thu);
printf("\nFri = %d", Fri);
printf("\nSat = %d", Sat);

return 0;
}

problem no.61: Write a C program that accepts a real number x and prints out the corresponding value
of sin(1/x) using 4-decimal places.
#include <stdio.h>
#include <math.h>
int main() {
double x, tval;

printf("Input value of x: \n");

scanf("%lf", &x);

if (x != 0.0) {

tval = sin(1/x);
printf("Value of sin(1/x) is %0.4lf\n", tval);
} else {

printf("Value of x should not be zero.");


}

return 0;
}

problem no. 62: Write a C program that accepts a positive integer less than 500 and prints out the sum
of the digits of this number.
#include <stdio.h>
int main() {
int a, x = 0, y;
printf("Input a positive number less than 500: \n");
scanf("%d", &a);
y = a;
if (y < 1 || y > 999) {

printf("The given number is out of range\n");


} else {

x += y % 10;
y /= 10;
x += y % 10;
y /= 10;
x += y % 10;
printf("Sum of the digits of %d is %d\n", a, x);
}

return 0;
}

problem 63: Write a C program that accepts a positive integer n less than 100 from the user. It prints out
the sum of 14 + 24 + 44 + 74 + 114 + • • • + m4. In this case, m is less than or equal to n. Print an
appropriate message.

#include <stdio.h>
int main() {
int i, j, n, sum_int = 0;

// Prompt user for input


printf("Input a positive number less than 100: \n");

// Read the input value


scanf("%d", &n);

// Check if the input is valid


if (n < 1 || n >= 100) {
printf("Wrong input\n");
return 0;
}

j = 1;
for (i = 1; j <= n; i++) {
sum_int += j * j * j * j;
j += i;
}

// Display the result


printf("Sum of the series is %d\n", sum_int);

return 0;
}

problem 64: Write a C program that accepts integers from the user until a zero or a negative number,
displays the number of positive values, the minimum value, the maximum value, and the average value.
#include <stdio.h>
int main() {
int a, ctr = 0, min_num, max_num, s = 0;
double avg;
printf("Input a positive integer:\n");
scanf("%d", &a);

if (a <= 0) {
printf("No positive number entered\n");
return 0;
}
min_num = a;
max_num = a;
while (a > 0) {
s += a;
ctr++;
max_num = a > max_num ? a : max_num;
min_num = a < min_num ? a : min_num;

printf("Input next positive integer:\n");


scanf("%d", &a);
}

avg = s / (double) ctr;

printf("Number of positive values entered is %d\n", ctr);


printf("Maximum value entered is %d\n", max_num);
printf("Minimum value entered is %d\n", min_num);
printf("Average value is %0.4lf\n", avg);

return 0;
}

problem no.65: Write a C program that prints out the prime numbers between 1 and 200. The output
should be such that each row contains a maximum of 20 prime numbers.

#include <stdio.h>
int main() {
int i, j, flag, ip = 0;
printf("The prime numbers between 1 and 199 are:\n");
for (i = 2; i < 199; i++) {
flag = 1;

for (j = 2; j <= i / 2 && flag == 1; j++) {


if (i % j == 0) {
flag = 0;
}
}

if (flag == 1) {
printf("%5d ", i);
ip++;

if (ip % 10 == 0) {
printf("\n");
}
}
}

printf("\n");

return 0;
}

problem 66: Write a C program that generates 50 random numbers between -0.5 and 0.5 and writes
them to the file rand.dat. The first line of ran.dat contains the number of random numbers, while the
next 50 lines contain 50 random numbers.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define N 50

int main() {
int i;

char str;

FILE * fptr;

fptr = fopen("rand.dat", "w");

if (fptr == NULL) {

printf("Error in creating output.dat\n");

return 0;

srand(time(NULL));

fprintf(fptr, "%d\n", N);

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

fprintf(fptr, "%0.4lf\n", (rand() % 2001 - 1000) / 2.e3);

fclose(fptr);

fptr = fopen ("rand.dat", "r");

str = fgetc(fptr);
while (str != EOF)

printf ("%c", str);

str = fgetc(fptr);

fclose(fptr);

return 0;
}
problem no. 67: Write a C program to evaluate the equation y=xn when n is a non-negative integer.

#include <stdio.h>

int main(){

int count, n;

float x,y;

printf("Input the values of x and n:\n");

scanf("%f%d", &x,&n);

y=1.0;

count=1;

while(count<=n)

y=y*x;

count++;

printf("x=%f; n=%d; \nx to power n=%f", x, n,y);

return 0;
}

problem no.68: Write a C program that prints the powers of 2 table for the powers 0 to 10, both positive
and negative.

#include<stdio.h>

int main() {

long int p;

int n;

double q;

printf("\n=======================================");

printf("\n n 2 to power n 2 to power -n");

printf("\n=======================================");

p = 1;
for (n = 0; n < 11; ++n) {

if (n == 0)

p = 1;

else

p = p * 2;

q = 1.0 / (double) p;

printf("\n%2d %8d %20.12lf", n, p, q);

printf("\n======================================");

return 0;

}
problem no.69: Write a C program to print a binomial coefficient table.
#include<stdio.h>

#define MAX 10

int main() {

int n, a, bi_nom;

printf("Mx ");

for (n = 0; n <= 10; ++n)

printf("%d ", n);

printf("\n----------------------------------------------------------\n");

n = 0;
do {

a = 0, bi_nom = 1;

printf("%2d", n);

while (a <= n) {

if (n == 0 || a == 0)

printf("%4d", bi_nom);

else {

bi_nom = bi_nom * (n - a + 1) / a;

printf("%4d", bi_nom);

a = a + 1;

printf("\n");

n = n + 1;

} while (n <= MAX);

printf("----------------------------------------------------------");

return 0;

}
p
problem no.70: Write a C program to print the alphabet set in decimal and character form.

#include <stdio.h>

#define N 10

int main() {

char chr;

printf("\n");

for (chr = 65; chr <= 122; chr = chr + 1) {

if (chr > 90 && chr < 97)


continue;

printf("[%2d-%c] ", chr, chr);

return 0;

problem no. 71: Write a C program to copy a given string into another and count the number of
characters copied.
#include<stdio.h>

#define N 10

int main() {

char str1[80], str2[80];

int i;

printf("Input a string: ");

scanf("%s", str2);

for(i=0; str2[i]!='\0'; i++)

str1[i]=str2[i];

str1[i]='\0';

printf("\n");
printf("Original string: %s", str1);

printf("\nNumber of characters = %d\n", i);

return 0;

problem no. 72: Write a C program to remove any negative sign in front of a number

#include<stdio.h>

#define N 10

int abs_val(int);

int main() {

int x, result;

printf("Input a value (negative): \n");

scanf("%d", &x);

printf("Original value = %d", x);

result = abs_val(x);
printf("\nAbsolute value = %d", result);

return 0;

int abs_val(int y) {

if(y < 0)

return(y * -1);

else

return(y);

problem 73: Write a C program that reads two integers and checks whether the first integer is a multiple
of the second integer.
#include<stdio.h>

int is_Multiple(int n1, int n2)

{
return n1 % n2 == 0;

int main()

int n1, n2;

printf("Input the first integer : ");

scanf("%d", &n1);

printf("Input the second integer: ");

scanf("%d", &n2);

if(is_Multiple(n1, n2))

printf("\n%d is a multiple of %d.\n", n1, n2);

else

printf("\n%d is not a multiple of %d.\n", n1, n2);

return 0;
}

Problem 74: Write a C program to display the integer equivalents of letters (a-z, A-Z).

#include<stdio.h>

int main()

char* letters = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int n;

printf("List of integer equivalents of letters (a-z, A-Z).\n");

printf("==================================================\n");

for(n=0; n<53; n++) {

printf("%d\t", letters[n]);

if((n+1) % 6 == 0)

printf("\n");

}
return 0;

problem 75: Write a C program that accepts a seven-digit number, separates the number into its
individual digits, and prints the digits separated from one another by two spaces each.

#include<stdio.h>

int main()

int n;

printf( "Input a seven digit number: " );

scanf("%d", &n);

printf( "\nOutput: " );


printf("%d ", (n/1000000));

n = n - ((n/1000000)*1000000);

printf("%d ", (n/100000));

n = n - ((n/100000)*100000);

printf("%d ", (n/10000));

n = n - ((n/10000)*10000);

printf("%d ", (n/1000));

n = n - ((n/1000)*1000);

printf("%d ", (n/100));

n = n - ((n/100)*100);

printf("%d ", (n/10));

n = n - ((n/10)*10);

printf("%d\n", (n%10));

return 0;

}
problem 76: Write a C program to calculate and print the squares and cubes of the numbers from 0 to
20. It uses tabs to display them in a table of values.

#include<stdio.h>

int main()

int x;

printf("Number\tSquare\tCube\n");

printf("=========================\n");

for(x=0; x<=20; x++)

printf("%d\t%d\t%d\n", x, x*x, x*x*x);

return 0;
}

problem 77: Write a C program that accepts principal amount, rate of interest and days for a loan and
calculates the simple interest for the loan, using the following formula.

#include<stdio.h>
int main()

float principal_amt, rate_of_interest, days, interest;

const int yearInDays = 365;

printf( "Input loan amount (0 to quit): " );

scanf( "%f", &principal_amt );

while( (int)principal_amt != 0)

printf( "Input interest rate: " );

scanf( "%f", &rate_of_interest );

printf( "Input term of the loan in days: " );

scanf( "%f", &days );

interest = (principal_amt * rate_of_interest * days )/ yearInDays;


printf( "The interest amount is $%.2f\n", interest );

printf( "\n\nInput loan principal_amt (0 to quit): " );

scanf( "%f", &principal_amt );

return 0;

problem 78: Write a C program to demonstrate the difference between predecrementing and
postdecrementing using the decrement operator --.

#include<stdio.h>

int main()

int x = 10;
printf("Predecrementing:\n");

printf("x = %d\n", x);

printf("x-- = %d\n", x--);

printf("x = %d\n\n", x);

x = 10;

printf("Postdecrementing:\n");

printf(" x = %d\n", x);

printf("--x = %d\n", --x);

printf(" x = %d\n", x);

return 0;
}

problem 79: Write a C program using looping to produce the following table of values.

#include<stdio.h>

int main()

int x; // Declare variable x

// Print header for table

printf("x\tx+2\tx+4\tx+6\n\n");

printf("---------------------------\n");

// Loop to generate and print table values

for(x=1; x<=15; x+=3)


printf("%d\t%d\t%d\t%d\n", x, (x+2), (x+4), (x+6));

return 0; // Indicate successful program execution

Problem 80: Write a C program that reads the side (side sizes between 1 and 10 ) of a square and prints
square using hash (#) character.

#include<stdio.h>

int main()

int size, i, j;

printf( "Input the size of the square: \n" );

scanf( "%d", &size );

if(size < 1 || size > 10) {

printf("Size should be in the range 1 to 10\n");

return 0; }
for(i=0; i<size; i++) {

for(j=0; j<size; j++) {

printf(" #"); }

printf("\n");

return 0;

}
problem 81: Write a C program that reads the side (side sizes between 1 and 10 ) of a square and prints
a hollow square using the hash (#) character.

#include <stdio.h>

int main()

// Declare variables

int size, i, j;

printf("Input the size of the square: ");

scanf("%d", &size);

if (size < 1 || size > 10) {

printf("Size should be in the range 1 to 10\n");

return 0;

for (i = 0; i< size; i++)

for (j = 0; j < size; j++)


{

if (i == 0 || i == size - 1)

printf("#");

else if (j == 0 || j == size - 1)

printf("#");

else

printf(" ");

printf("\n");

return 0;
}

problem 82: Write a C program that reads a five-digit integer and determines whether or not it's a
palindrome.

#include <stdio.h>

int is_Palindrome(int);

int main()

int n;
printf("Input a five-digit number: ");

scanf("%d", &n);

if (is_Palindrome(n))

printf("%d is a palindrome.", n);

else

printf("%d is not a palindrome.", n);

return 0;

int is_Palindrome(int n) {

int x = n;

int reverse_num = 0;

reverse_num += x / 10000;

x = x - ((x / 10000) * 10000);

reverse_num += ((x / 1000) * 10);


x = x - ((x / 1000) * 1000);

reverse_num += ((x / 100) * 100);

x = x - ((x / 100) * 100);

reverse_num += ((x / 10) * 1000);

x = x - ((x / 10) * 10);

reverse_num += ((x % 10) * 10000);

return n == reverse_num;

Problem 83: Write a C program to calculate and print the average of some integers. Accept all the values
preceding 888.
#include <stdio.h>

int main()

{
int ctr = 0, n;

int sum = 0;

float avg_value = 0;

printf("Input each number on a separate line (888 to exit):\n");

scanf("%d", &n);

while (n != 888) {

sum += n;

ctr++;

scanf("%d", &n);

if (ctr)

avg_value = (float) sum / ctr;

printf("\nThe average value of the said numbers is %f\n", avg_value);

return 0;
}

problem no. 85: Write a C program to print a table of all the Roman numeral equivalents of decimal
numbers in the range 1 to 50.

#include <stdio.h>

int main()

int i, x;

printf("Decimal Roman\n");

printf("number numeral\n");

printf("-------------------\n");

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

{
x = i;

printf("%d ", x);

if(x == 100) {

printf("C");

x = 0;

if(x >= 50) {

printf("L");

x -= 50;

while(x >= 10) {

printf("X");

x -= 10;

if(x >= 5) {

if(x % 10 == 9) {

printf("IX");

x -= 9;

else {

printf("V");

x -= 5;

}
}

while(x > 0)

if(x % 10 == 4) {

printf("IV");

x -= 4;

else {

printf("I");

x -= 1;

printf("\n");

return 0;
}
problem 86: Write a C program to display the sizes and ranges for each of C's data types.

#include <stdio.h>

#include <stdint.h>

#include <stdbool.h>

#include <limits.h>

int main(void) {
printf("Size and Range of C data types:\n\n");

printf("%-20s %-20s %-20s\n", "Type", "Bytes", "Range");

printf("------------------------------------------------------\n");

printf("%-20s %lu %-20d to %-20d\n", "char", sizeof(char), CHAR_MIN, CHAR_MAX);

printf("%-20s %lu %-20d to %-20d\n", "int8_t", sizeof(int8_t), INT8_MIN, INT8_MAX);

printf("%-20s %lu %-20d to %-20d\n", "unsigned char", sizeof(unsigned char), 0, UCHAR_MAX);

printf("%-20s %lu %-20d to %-20d\n", "uint8_t", sizeof(uint8_t), 0, UINT8_MAX);

printf("%-20s %lu %-20d to %-20d\n", "short", sizeof(short), SHRT_MIN, SHRT_MAX);

printf("%-20s %lu %-20d to %-20d\n", "int16_t", sizeof(int16_t), INT16_MIN, INT16_MAX);

printf("%-20s %lu %-20d to %-20d\n", "uint16_t", sizeof(uint16_t), 0, UINT16_MAX);

printf("%-20s %lu %-20d to %-20d\n", "int", sizeof(int), INT_MIN, INT_MAX);

printf("%-20s %lu %-20u to %-20u\n", "unsigned", sizeof(unsigned), 0, UINT_MAX);

printf("%-20s %lu %-20ld to %-20ld\n", "long", sizeof(long), LONG_MIN, LONG_MAX);

printf("%-20s %lu %-20lu to %-20lu\n", "unsigned long", sizeof(unsigned long), 0, ULONG_MAX);

printf("%-20s %lu %-20d to %-20d\n", "int32_t", sizeof(int32_t), INT32_MIN, INT32_MAX);

printf("%-20s %lu %-20u to %-20u\n", "uint32_t", sizeof(uint32_t), 0, UINT32_MAX);

printf("%-20s %lu %-20lld to %-20lld\n", "long long", sizeof(long long), LLONG_MIN, LLONG_MAX);

printf("%-20s %lu %-20llu to %-20llu\n", "int64_t", sizeof(int64_t), 0LL, ULLONG_MAX);

printf("%-20s %lu %-20lld to %-20lld\n", "int64_t", sizeof(int64_t), LLONG_MIN, LLONG_MAX);

printf("%-20s %lu %-20llu to %-20llu\n", "uint64_t", sizeof(uint64_t), 0ULL, ULLONG_MAX);

printf("\n");

return 0;
}

problem 87:Write a C program to display the minimum and maximum values for each of C's data types.

#include <stdio.h>

#include <stdint.h>

#include <stdbool.h>

#include <limits.h>

#include <float.h>

int main( void )

printf( "Ranges for integer data types in C\n\n" );


printf( "------------------------------------------------------------\n");

printf( "int8_t %20d %20d\n" , SCHAR_MIN , SCHAR_MAX );

printf( "int16_t %20d %20d\n" , SHRT_MIN , SHRT_MAX );

printf( "int32_t %20d %20d\n" , INT_MIN , INT_MAX );

printf( "int64_t %20lld %20lld\n" , LLONG_MIN , LLONG_MAX );

printf( "uint8_t %20d %20d\n" ,0 , UCHAR_MAX );

printf( "uint16_t %20d %20d\n" ,0 , USHRT_MAX );

printf( "uint32_t %20d %20u\n" ,0 , UINT_MAX );

printf( "uint64_t %20d %20llu\n" , 0 , ULLONG_MAX );


printf( "\n" );

printf( "============================================================\n\n");

printf( "Ranges for real number data types in C\n\n" );

printf( "------------------------------------------------------------\n");

printf( "float %14.7g %14.7g\n" , FLT_MIN , FLT_MAX );

printf( "double %14.7g %14.7g\n" , DBL_MIN , DBL_MAX );

printf( "long double %14.7Lg %14.7Lg\n" , LDBL_MIN , LDBL_MAX );

printf( "\n" );
return 0;

}
problem 88: Write a C program to create an extended ASCII table. Print the ASCII values 32 through 255.

#include <stdio.h>

int main( void )

unsigned char char1 , char2 , char3 , char4 , char5 , char6 , char7 , char8 ;

printf("|---------------------------------------------------------------------------------------------------------|\n" );

printf("|extended ASCII table - excluding control characters |\n" );

printf("| Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch
Dec Hex |\n" );

printf("|----------------|----------------|-------------|--------------|--------------|-------------|-------------|\n" );

for(int i = 0 ; i< 32; i++)

char1 = i;

char2 = i+32;

char3 = i+64;

char4 = i+96;

char5 = i+128;

char6 = i+160;
char7 = i+192;

char8 = i+224;

printf( "| %c %3d %#x " , char2 , char2 , char2 );

printf( "| %c %3d %#x " , char3 , char3 , char3 );

if( char4 == 127 ) {

printf("|%s %3d %#x |" , "DEL" , char4 , char4 );

} else {

printf("| %c %3d %#x |" , char4 , char4 , char4 );

printf(" %c %3d %#x | %c %3d %#x | %c %3d %#x | %c %3d %#x |\n" ,

char5 , char5 , char5 ,

char6 , char6 , char6 ,

char7 , char7 , char7 ,

char8 , char8 , char8 );

return 0;

}
problem 89: Write a C programming to calculate (x + y + z) for each pair of integers x, y and z where -
2^31 <= x, y, z<= 2^31-1.

#include <stdio.h>

int main()

long long x, y, z;

printf("Input x, y, z:\n");

scanf("%lld %lld %lld", &x, &y, &z);


printf("Result: %lld\n", x + y + z);

return 0;

problem 90: Write a C program to find all prime palindromes in the range of two given numbers x and y
(5 <= x<y<= 1000,000,000).

#include<stdio.h>

int compute(int n){

if(n == 9)

return 11;

int res = 0, tmp, base = 1;


for(tmp = n / 10; tmp> 0; tmp = tmp / 10){

res = res * 10 + tmp % 10;

base *= 10;

return n * base + res;

int prime(int n){

int i;

int flag;

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

flag = 0;

if(n % i == 0){

flag = 1;

break;

if(flag == 0){

printf("%d\n", n);
}

int main(){

int x, y, temp = 0;

printf("Input two numbers (separated by a space):\n");

scanf("%d %d", &x, &y);

printf("List of prime palindromes:\n");

for(; temp <= y; x++){

temp = compute(x);

prime(temp);

return 0;

}
problem 91:Write a C program to find the angle between (12:00 to 11:59) the hour hand and the minute
hand of a clock. The hour hand and the minute hand are always between 0 and 180 degrees.

#include <stdio.h>

#include <math.h>

#include <stdlib.h>
int main()

int h, m;

double angle;

const int num[13] = {0,30,60,90,120,150,180,210,240,270,300,330,0};

printf("Input hour(h) and minute(m) (separated by a space):\n");

scanf("%d %d",&h,&m);

angle = num[h] - m*5.5;

if (angle < 0)

angle = -angle;

if (angle > 180)

angle = 360 - angle;

if ( m < 10 )

printf("At %d:0%d the angle is %.1f degrees.\n",h,m,angle);

else

printf("The angle is %.1f degrees at %d:%d.\n",angle,h,m);

return 0;

}
problem 92: Write a C program to find the last non-zero digit of the factorial of a given positive integer.

For example for 5!, the output will be "2" because 5! = 120, and 2 is the last nonzero digit of 120.
#include <stdio.h>

#include <string.h>
const char ftr[] = {1,1,2,6,4,4,4,8,4,6};

void comp(char i[], int* len)

int j;

char c, tmp;

if(i[0] < 5)

c = i[0];

(*len)--;

for(j = 0; j < *len; j++)

tmp = (c*10 + i[j+1]) % 5;

i[j] = (c*10 + i[j+1]) / 5;

c = tmp;

else

{
c = 0;

for(j = 0; j < *len; j++)

tmp = (c*10 + i[j]) % 5;

i[j] = (c*10 + i[j]) / 5;

c = tmp;

char fact(char i[], int len)

char ans, c, d;

if(len == 1 &&i[0] < 5)

return ftr[(int)i[0]];

else

ans = ftr[(int)i[len-1]] * 6 % 10;

comp(i, &len);

d = (i[len-1] + ((len> 1) ? i[len-2] * 10 : 0)) & 0x03;


for(c = 0; c < d; c++)

if(ans == 2 || ans == 6)

ans += 10;

ans /= 2;

return fact(i, len) * ans % 10;

int main()

char chr[1002];

int len, i;

char c;

printf("Input a positive number:\n");

scanf("%s", chr);

len = strlen(chr);

for(i = 0; i<len; i++)

chr[i] -= '0';
c = fact(chr, len);

printf("The last non-zero digit of the said factorial:\n");

putchar(c + '0');

putchar(10);

return 0;

problem 93: Write a C program to check if a given number is nearly prime number or not.

Nearly prime numbers are a positive integer which is equal to the product of two prime numbers.

#include <stdio.h>
#define NUM_OF_PRIMES 3500

int is_prime(int num);

int main() {

int primes[NUM_OF_PRIMES], num_of_primes = 0;

primes[num_of_primes++] = 2;

for(int num = 3; num * num<= 1000000000; num++) {

int flag = 1;

for(int id = 0; id <num_of_primes; id++) {

if(num % primes[id] == 0) {

flag = 0;

break;

if(flag) primes[num_of_primes++] = num;


}

int N, num;

scanf("%d", &num);

int flag = 0;

for(int j = 0; (j <num_of_primes) && (primes[j] * primes[j] <= num); j++) {

if(num % primes[j] == 0) {

num /= primes[j];

flag = 1;

break;

if(flag &&is_prime(num))

printf("It is a Nearly prime number.\n");

else

printf("It is not a Nearly prime number.\n");

return 0;

}
int is_prime(int num) {

if(num != 2 &&num % 2 == 0)

return 0;

for(int factor = 3; factor * factor <= num ; factor += 2) {

if(num % factor == 0)

return 0;

return 1;

problem no. 94:Write a C program to calculate body mass index and display the grade.

#include <stdio.h>

void BMI(int, float);

int main(void) {
int w;
float h;

printf("Input the weight: ");

scanf("%d", &w);

printf("Input the height: ");

scanf("%f", &h);

BMI(w, h);

void BMI(int weight, float height) {

float temp = weight / (height * height);

printf("BMI = %f\n", temp);

printf("\nGrade: ");

temp< 18.5 ? printf("Under ") : temp < 25 ? printf("Normal ") : temp < 30 ? printf("Over ") : temp < 40 ?
printf("Obese ") : printf("Error");

}
problem 95: Write a C program to print the corresponding Fahrenheit to Celsius and Celsius to
Fahrenheit.

#include <stdio.h>

int main() {

float f_temp, c_temp;

float start_temp, end_temp;

int STEP;

start_temp = 0;

end_temp = 150;

STEP = 10;
printf("Fahrenheit to Celsius");

printf("\n---------------------\n");

printf("Fahrenheit Celsius\n");

while (start_temp<= end_temp) {

f_temp = start_temp * 9 / 5 + 32;

printf("%6.1f \t %8.1f\n", start_temp, f_temp);

start_temp = start_temp + STEP;

start_temp = 0;

end_temp = 150;

STEP = 10;

printf("\n\nCelsius to Fahrenheit\n");

printf("---------------------\n");

printf("Celsius Fahrenheit\n");
while (start_temp<= end_temp) {

c_temp = (start_temp - 32) * 5 / 9;

printf("%6.1f \t %8.1f\n", start_temp, c_temp);

start_temp = start_temp + STEP;

problem 96: Write a C program to count blanks, tabs, and newlines in input text.

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

int blank_char, tab_char, new_line;

blank_char = 0;

tab_char = 0;

new_line = 0;

int c;

printf("Number of blanks, tabs, and newlines:\n");

printf("Input few words/tab/newlines\n");

for (; (c = getchar()) != EOF;) {

if (c == ' ') {

++blank_char;

if (c == '\t') {

++tab_char;

if (c == '\n') {

++new_line;

printf("blank=%d,tab=%d,newline=%d\n", blank_char, tab_char, new_line);


}

problem no. 97: Write a C program that accepts a string and counts the number of characters, words
and lines.

#include <stdio.h>

int main() {

long ctr_char, ctr_word, ctr_line;

int c;

int flag;

ctr_char = 0;

flag = ctr_line = ctr_word = 0;

printf("Input a string and get number of characters, words and lines:\n");

while ((c = getchar()) != EOF) {


++ctr_char;

if (c == ' ' || c == '\t') {

flag = 0;

} else if (c == '\n') {

++ctr_line;

flag = 0;

} else {

if (flag == 0) {

++ctr_word;

flag = 1;

printf("\nNumber of Characters = %ld", ctr_char);

printf("\nNumber of words = %d", ctr_word);

printf("\nNumber of lines = %d", ctr_line);

}
Problem no. 98: Write a C program that accepts some text from the user and prints each word of that
text on a separate line.
#include <stdio.h>

int main() {

long nc = 0;

int new_l = 0;

int n_word = 0;

int chr;

int flag = 0;

int last = 0;

printf("Input some text:\n");

while ((chr = getchar()) != EOF) {

++nc;

if (chr == ' ' || chr == '\t') {

flag = 0;

} else if (chr == '\n') {

++new_l;

flag = 0;

} else {

if (flag == 0) {

++n_word;

}
flag = 1;

if (flag == 0 && last == 0) {

printf("\n");

last = 1;

} else {

putchar(chr);

last = 0;

problem no.99: Write a C program that takes some integer values from the user and prints a histogram.

#include <stdio.h>

void print_Histogram ( int *hist, int n );


int main() {

int i, j;

int inputValue, hist_value=0;

printf("Input number of histogram bar (Maximum 10): \n");

scanf("%d", &inputValue);

int hist[inputValue];

if (inputValue<=10)

printf("Input the values between 0 and 10 (separated by space): \n");

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

scanf("%d", &hist_value);

if (hist_value>=1 && hist_value<=10)

hist[i] = hist_value;

hist_value=0;

int results[10] = {0};

for(j = 0; j < inputValue; j++) {

if ( hist[j] == i){

results[i]++;
}

printf("\n");

print_Histogram(hist, inputValue);

return 0;

void print_Histogram(int *hist, int n) {

printf("\nHistogram:\n");

int i, j;

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

for ( j = 0; j < hist[i]; ++j) {

printf("#");

printf("\n");

}
}

Problem no.100: Write a C program to convert a currency value (floating point with two decimal places)
to the number of coins and notes.

#include <stdio.h>

#include <math.h>

int main() {

double amt;

unsigned int int_amt, frac_amt;


printf("Input the currency value (floating point with two decimal places):\n");

scanf("%lf", &amt);

int_amt = (int) amt;

amt -= int_amt;

frac_amt = round((amt * 100));

printf("\nCurrency Notes:");

printf("\n100 number of Note(s): %d", int_amt / 100);

int_amt -= (int_amt / 100) * 100;

if (int_amt > 50) {

printf("\n50 number of Note(s): 1");

int_amt -= 50;

}
if (int_amt/20 > 0)

printf("\n20 number of Note(s): %d", int_amt / 20);

int_amt -= (int_amt / 20) * 20;

if (int_amt/10 > 0)

printf("\n10 number of Note(s): %d", int_amt / 10);

int_amt -= (int_amt / 10) * 10;

if (int_amt/5 > 0)

printf("\n5 number of Note(s): %d", int_amt / 5);

int_amt -= (int_amt / 5) * 5;

if (int_amt > 0)

printf("\n2 number of Note(s): %d", int_amt / 2);


int_amt -= (int_amt / 2) * 2;

if (int_amt > 0)

printf("\n1 number of Note(s): %d", int_amt);

printf("\n\nCurrency Coins:");

if (frac_amt > 50) {

printf("\n.50 number of Coin(s): 1");

frac_amt -= 50;

if (frac_amt/25 > 0)

printf("\n.25 number of Coin(s): %d", frac_amt / 25);

frac_amt -= (frac_amt / 25) * 25;

if (frac_amt/10 > 0)
printf("\n.10 number of Coin(s): %d", frac_amt / 10);

frac_amt -= (frac_amt / 10) * 10;

if (frac_amt/5 > 0)

printf("\n.05 number of Coin(s): %d", frac_amt / 5);

frac_amt -= (frac_amt / 5) * 5;

if (frac_amt > 0)

printf("\n.01 number of Coin(s): %d", frac_amt);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy