C Problems
C Problems
equation ax2+bx+c=o, that means if the solution is complex number then print this information
otherwise just print the solutions at the output terminal.
Solution: (Using if…else)
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c, x1,x2;
return 0;
}
(Using Switch…case)
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, x1, x2, t;
int k;
switch(k)
{
case 1:
printf("Complex solution");
break;
case 2:
printf("x1= %.2f \t x2= %.2f", (-b+sqrt(b*b-4*a*c))/2*a, (-b+sqrt(b*b-4*a*c))/2*a);
break;
}
}
else
printf("Invalid input");
}
return 0;
}
In different way :
#include<stdio.h>
int main()
{
float x, y;
int k;
printf("Enter the value of x\n");
scanf("%f", &x);
if(x>0)
k=1;
else if(x==0)
k=2;
else if(x<0)
k=3;
switch(k)
{
case 1:
printf("%.2f", x*x+2*x+5);
break;
case 2:
printf(" 56");
break;
case 3:
printf("%.2f",2*x+5);
break;
}
}
Problem #: Write a program that takes a number a print its digit separately. [Input 1234: Output 4
321]
#include<stdio.h>
int main()
{
int t, s;
long n;
printf("Enter the number\n");
scanf("%d", &n);
printf("[");
while(n>=1)
{
t=n%10;
n=n/10;
s=t;
printf("%d ", s);
}
printf("]");
return 0;
}
Problem 3: Write a program that takes a number and return a list of its digits.
#include <stdio.h>
int main ( )
{
int i, dig[32], count=0;
int num, c;
printf("Enter a number: ");
scanf("%d", &num);
c=num;
printf("[");
while(c>=1)
{
c=c/10;
count++;
}
while(num!=0)
{
return 0;
}
Problem 7: Write a program to input electricity unit charges and calculate total electricity bill
according to given information:
1st 50 unit =0.5/unit
Next 100 unit = 0.75/unit
Next 100 unit =1.20/unit
Above 250unit=1.50/unit
surcharge=bill_s*20/100;
bill=bill_s+surcharge;
printf("Bill=%.2f", bill);
}
int main() {
int year;
printf("Enter Year\n");
scanf("%d", &year);
return 0;
}
Problem 1: Write a program to determine the first n Fibonacci numbers as given below :
1 1 2 3 5 8 13 …..
#include<stdio.h>
int main()
{
int i, n;
double t1=1, t2=1, nxt;
printf("Enter the number of terms :\n");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
printf("%f\n", t1);
nxt=t1+t2;
t1=t2;
t2=nxt;
}
return 0;
}
Problem 2: Write a program to show no of digits, summation of given number and print reverse
order.
[Input n=1245, No of digit=4, sum=12, reverse order=5421]
#include<stdio.h>
int main()
{
int n, t;
int r=0, s=0, c=0, re;
printf("Enter the number :");
scanf("%d", &n);
t=n;
while(n!=0)
{
re=n%10;
s=s+re;
n=n/10;
c++;
r=r*10+re;
}
printf("The number = %d\nNo. of digit = %d\nSum = %d\n", t, c, s);
if(t%10==0)
{
printf("Reverse = 0%d", r);
}
else
printf("Reverse = %d", r);
return 0;
Problem 3: Write a program to calculate the Celsius to Fahrenheit conversion table starting from
0 to 500 in steps of 25.
#include<stdio.h>
int main()
{
float f;
int n, c, i;
printf("Celcius \t Fahrenheit\n");
for(c=1; c<=500; c++)
{
f=c*1.8+32;
printf("%d\t%.2f\n", c, f);
for(i=0; i<=25; i++){
if(c==25*i)
printf("\n\n\n");
}
}
}
Problem 4: Write a program to read age of 10 persons and count the number of person in the age
group of 50-60. The program should give the following message and terminate if input is above
100. “You inserted incorrect age!!”
#include<stdio.h>
#include<stdlib.h>
int main()
{
int age[10], i, c=0;
printf("Enter 10 age:\n");
return 0;
}
return 0;
}
Problem 6: Write a program to show the number of days corresponds to each month number.
#include<stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
switch(n)
{
case 1:
printf("Month January has 31 days");
break;
case 2:
printf("Month February has 28/29 days");
break;
case 3:
printf("Month March has 31 days");
break;
case 4:
printf("Month April has 30 days");
break;
case 5:
printf("Month May has 31 days");
break;
case 6:
printf("Month June has 30 days");
break;
case 7:
printf("Month July has 31 days");
break;
case 8:
printf("Month August has 31 days");
case 9:
printf("Month September has 30 days");
break;
case 10:
printf("Month October has 31 days");
break;
case 11:
printf("Month November has 30 days");
break;
case 12:
printf("Month December has 31 days");
break;
default:
printf("Invalid Input");
return 0;
}
Problem 8: Write a program to input electricity unit charges and calculate total electricity bill
according to given information:
1st 200 unit =0.5/unit
Next 200 unit = 100+0.65/unit
Next 200 unit =230+0.80/unit
Above 600unit=390+1.00/unit
#include<stdio.h>
int main()
{
float unit, bill_s;
int customer;
printf("Enter customer number: ");
scanf("%d", &customer);
printf("Enter electricity unit: ");
scanf("%f", &unit);
if(unit<=200)
{
bill_s=unit*.50;
}
else if(unit<=400)
{
unit=unit-200;
bill_s=100+unit*.65;
}
else if(unit<=600)
{
unit=unit-400;
bill_s=230+unit*.80;
}
else if(unit>601)
{
unit=unit-600;
bill_s=390+unit*1.0;
}
return 0;
}
Problem 9: Write a program to calculate the total number of negative element of an array and print
them.
#include<stdio.h>
int main()
{
int x[20];
int i, c=0;
printf("Enter 20 number : \n");
for(i=0; i<20; i++)
{
scanf("%d", &x[i]);
}
printf("Negative numbers are : \n");
for(i=0; i<20; i++)
{
if(x[i]<0)
{
c++;
printf("%d\n", x[i]);
}
}
printf("Total Negative numbers = %d", c);
return 0;
}
Problem 10: Write a program to show the sum of each column and row of a given m*n matrix.
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i+1, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j+1, sum);
sum = 0;
}
return o;
}
Solved by
Md Raihan Khan
Dept: EEE
1803115