0% found this document useful (0 votes)
35 views14 pages

C Problems

The document contains solutions to 10 programming problems in C language. The problems cover topics like checking the type of solutions for quadratic equations, evaluating functions based on conditions, calculating electricity bills, checking leap years, and more. Multiple solutions are provided for each problem using concepts like if-else, switch-case statements.

Uploaded by

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

C Problems

The document contains solutions to 10 programming problems in C language. The problems cover topics like checking the type of solutions for quadratic equations, evaluating functions based on conditions, calculating electricity bills, checking leap years, and more. Multiple solutions are provided for each problem using concepts like if-else, switch-case statements.

Uploaded by

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

Problem 1: Write a program that check the possibility of complex number solutions of quadratic

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;

printf("Enter the value of a b & c :\n");


scanf("%f%f%f", &a, &b, &c);
if(b*b-4*a*c<0)
{
printf("Complex solution.");
}
else
printf("x1= %.2f \t x2= %.2f", (-b+sqrt(b*b-4*a*c))/2*a, (-b+sqrt(b*b-4*a*c))/2*a);

return 0;
}

(Using Switch…case)
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, x1, x2, t;
int k;

printf("Enter the value of a b & c :\n");


scanf("%f%f%f", &a, &b, &c);
if(a!=0)
{
t=b*b-4*a*c;
if(t<0)
k=1;
else if(t>0)
k=2;

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");
}

Problem 2: Write a code to evaluate the value of the following functions:


x2+2x=5; x>0
y= 56;
2x+5; x<0
#include<stdio.h>
int main()
{
float x, y;
printf("Enter the value of x\n");
scanf("%f", &x);
if(x>0)
y=x*x+2*x+5;
else if(x==0)
y=56;
else if(x<0)
y=2*x+5;
printf("The value of y is %.2f", y);

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)
{

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


{
dig[i]= num %10;
num = num/10;
}

for(; count>0; count--)


{
printf("%d ", dig[count]);
}
}
printf("]");
return 0;
}

Problem 4: Write a program to check a triange is valid or not.


#include<stdio.h>
int main()
{
float a, b, c;
printf("Enter 3 arms of the triangle :\n");
scanf("%f%f%f", &a, &b, &c);
if(a+b>c && b+c>a && c+a>b)
printf("This is a valid triangle.");
else
printf("Invalid triangle.");
return 0;
}

Problem 5: Write a program to check a number is +ve or –ve 0.


#include<stdio.h>
int main()
{
int n;
printf("Enter the number :\n");
scanf("%d", &n);
if(n>0)
printf("This is a positive number.");
else if(n<0)
printf("This is a negative number");
else
printf("This is zero");

return 0;
}

Problem 6: Write a program to check a letter is vowel or consonant.


#include<stdio.h>
int main()
{
char c;
printf("Enter a character: \n");
scanf("%c", &c);
if((c>='A' && c<='Z') || (c>='a' && c<='z'))
{
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
{
printf("This a vowel");
}
else
printf("This a consonant");
}
else
printf("This is not a character");
}

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

Plus 20% surcharge.


#include<stdio.h>
int main()
{
float unit, bill_s, surcharge, bill, _50, _100, _200, _250;
printf("Enter electricity unit\n");
scanf("%f", &unit);
if(unit<=50)
{
_50=unit;
_100=0;
_200=0;
_250=0;
}
else if(unit<=150)
{
_50=50;
_100=unit-50;
_200=0;
_250=0;
}
else if(unit<=250)
{
_50=50;
_100=100;
_200=unit-150;
_250=0;
}
else if(unit>250)
{
_50=50;
_100=100;
_200=100;
_250=unit-250;
}
bill_s=_50*.50+_100*.75+_200*1.2+_250*1.5;

surcharge=bill_s*20/100;

bill=bill_s+surcharge;
printf("Bill=%.2f", bill);
}

Problem 8: Write a program to check whether a year is a leap year or not.


#include <stdio.h>

int main() {
int year;
printf("Enter Year\n");
scanf("%d", &year);

if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))


printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;
}

Problem 9: Write a program to check whether a number is divisible by 5 or 11.


#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(!(num % 5) && !(num % 11))
printf("Number is divisible by 5 and 11");
else
printf("Number is not divisible by 5 and 11");
return 0;
}

Problem 10: Write a program to check a triangle is equilateral or scalene or isosceles.


#include<stdio.h>
int main()
{
float a, b, c;
printf("Enter 3 arms of the triangle :\n");
scanf("%f%f%f", &a, &b, &c);
if(a+b>c && b+c>a && c+a>b){
if(a==b && b==c)
printf("The triangle is equilateral");
else if(a==b || a==c || b==c)
printf("The triangle is Isosceles");
else
printf("The triangle is Scalene");
}
else
printf("Not a triangle");

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");

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


{
scanf("%d", &age[i]);
if(age[i]>=100)
{
printf("You inserted incorrect age!!");
exit(0);
}

if(age[i]>=50 && age[i]<=60)


{
c++;
}

printf("%d persons are between 50 and 60 years. ", c);

return 0;
}

Problem 5: Write a program to check a triangle is equilateral or scalene or isosceles.


#include<stdio.h>
int main()
{
float a, b, c;
printf("Enter 3 arms of the triangle :\n");
scanf("%f%f%f", &a, &b, &c);
if(a+b>c && b+c>a && c+a>b){
if(a==b && b==c)
printf("The triangle is equilateral");
else if(a==b || a==c || b==c)
printf("The triangle is Isosceles");
else
printf("The triangle is Scalene");
}
else
printf("Not a triangle");

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 7: Write a program to print following pattern:


1
00
111
0000
11111
#include<stdio.h>
int main()
{
int i, j, n;
printf("Enter n :");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
if(i%2==1)
{
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
}
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;
}

printf("Customer ID : %d\n", customer);


printf("Bills to paid : %.2f", bill_s);

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

You might also like