c language problems
c language problems
c language problems
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() {
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;
int v[e];
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;
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;
a <<= 2;
b = a;
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;
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;
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() {
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;
scanf("%lf", &x);
if (x != 0.0) {
tval = sin(1/x);
printf("Value of sin(1/x) is %0.4lf\n", tval);
} else {
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) {
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;
j = 1;
for (i = 1; j <= n; i++) {
sum_int += j * j * j * j;
j += i;
}
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;
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;
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;
if (fptr == NULL) {
return 0;
srand(time(NULL));
fclose(fptr);
str = fgetc(fptr);
while (str != EOF)
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;
scanf("%f%d", &x,&n);
y=1.0;
count=1;
while(count<=n)
y=y*x;
count++;
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=======================================");
p = 1;
for (n = 0; n < 11; ++n) {
if (n == 0)
p = 1;
else
p = p * 2;
q = 1.0 / (double) p;
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 ");
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;
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");
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() {
int i;
scanf("%s", str2);
str1[i]=str2[i];
str1[i]='\0';
printf("\n");
printf("Original string: %s", str1);
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;
scanf("%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>
{
return n1 % n2 == 0;
int main()
scanf("%d", &n1);
scanf("%d", &n2);
if(is_Multiple(n1, n2))
else
return 0;
}
Problem 74: Write a C program to display the integer equivalents of letters (a-z, A-Z).
#include<stdio.h>
int main()
int n;
printf("==================================================\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;
scanf("%d", &n);
n = n - ((n/1000000)*1000000);
n = n - ((n/100000)*100000);
n = n - ((n/10000)*10000);
n = n - ((n/1000)*1000);
n = n - ((n/100)*100);
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");
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()
while( (int)principal_amt != 0)
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");
x = 10;
printf("Postdecrementing:\n");
return 0;
}
problem 79: Write a C program using looping to produce the following table of values.
#include<stdio.h>
int main()
printf("x\tx+2\tx+4\tx+6\n\n");
printf("---------------------------\n");
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;
return 0; }
for(i=0; i<size; i++) {
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;
scanf("%d", &size);
return 0;
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))
else
return 0;
int is_Palindrome(int n) {
int x = n;
int reverse_num = 0;
reverse_num += x / 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;
scanf("%d", &n);
while (n != 888) {
sum += n;
ctr++;
scanf("%d", &n);
if (ctr)
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");
{
x = i;
if(x == 100) {
printf("C");
x = 0;
printf("L");
x -= 50;
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("------------------------------------------------------\n");
printf("%-20s %lu %-20lld to %-20lld\n", "long long", sizeof(long long), LLONG_MIN, LLONG_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>
printf( "============================================================\n\n");
printf( "------------------------------------------------------------\n");
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>
unsigned char char1 , char2 , char3 , char4 , char5 , char6 , char7 , char8 ;
printf("|---------------------------------------------------------------------------------------------------------|\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" );
char1 = i;
char2 = i+32;
char3 = i+64;
char4 = i+96;
char5 = i+128;
char6 = i+160;
char7 = i+192;
char8 = i+224;
} else {
printf(" %c %3d %#x | %c %3d %#x | %c %3d %#x | %c %3d %#x |\n" ,
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");
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>
if(n == 9)
return 11;
base *= 10;
int i;
int flag;
flag = 0;
if(n % i == 0){
flag = 1;
break;
if(flag == 0){
printf("%d\n", n);
}
int main(){
int x, y, temp = 0;
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;
scanf("%d %d",&h,&m);
if (angle < 0)
angle = -angle;
if ( m < 10 )
else
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};
int j;
char c, tmp;
if(i[0] < 5)
c = i[0];
(*len)--;
c = tmp;
else
{
c = 0;
c = tmp;
char ans, c, d;
return ftr[(int)i[0]];
else
comp(i, &len);
if(ans == 2 || ans == 6)
ans += 10;
ans /= 2;
int main()
char chr[1002];
int len, i;
char c;
scanf("%s", chr);
len = strlen(chr);
chr[i] -= '0';
c = fact(chr, len);
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 main() {
primes[num_of_primes++] = 2;
int flag = 1;
if(num % primes[id] == 0) {
flag = 0;
break;
int N, num;
scanf("%d", &num);
int flag = 0;
if(num % primes[j] == 0) {
num /= primes[j];
flag = 1;
break;
if(flag &&is_prime(num))
else
return 0;
}
int is_prime(int num) {
if(num != 2 &&num % 2 == 0)
return 0;
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>
int main(void) {
int w;
float h;
scanf("%d", &w);
scanf("%f", &h);
BMI(w, h);
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() {
int STEP;
start_temp = 0;
end_temp = 150;
STEP = 10;
printf("Fahrenheit to Celsius");
printf("\n---------------------\n");
printf("Fahrenheit Celsius\n");
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) {
problem 96: Write a C program to count blanks, tabs, and newlines in input text.
#include <stdio.h>
int main() {
blank_char = 0;
tab_char = 0;
new_line = 0;
int c;
if (c == ' ') {
++blank_char;
if (c == '\t') {
++tab_char;
if (c == '\n') {
++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() {
int c;
int flag;
ctr_char = 0;
flag = 0;
} else if (c == '\n') {
++ctr_line;
flag = 0;
} else {
if (flag == 0) {
++ctr_word;
flag = 1;
}
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;
++nc;
flag = 0;
++new_l;
flag = 0;
} else {
if (flag == 0) {
++n_word;
}
flag = 1;
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>
int i, j;
scanf("%d", &inputValue);
int hist[inputValue];
if (inputValue<=10)
scanf("%d", &hist_value);
hist[i] = hist_value;
hist_value=0;
if ( hist[j] == i){
results[i]++;
}
printf("\n");
print_Histogram(hist, inputValue);
return 0;
printf("\nHistogram:\n");
int 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;
scanf("%lf", &amt);
amt -= int_amt;
printf("\nCurrency Notes:");
int_amt -= 50;
}
if (int_amt/20 > 0)
if (int_amt/10 > 0)
if (int_amt/5 > 0)
int_amt -= (int_amt / 5) * 5;
if (int_amt > 0)
if (int_amt > 0)
printf("\n\nCurrency Coins:");
frac_amt -= 50;
if (frac_amt/25 > 0)
if (frac_amt/10 > 0)
printf("\n.10 number of Coin(s): %d", frac_amt / 10);
if (frac_amt/5 > 0)
frac_amt -= (frac_amt / 5) * 5;
if (frac_amt > 0)
return 0;