0% found this document useful (0 votes)
98 views

Board Question C Program Solve

The document contains code snippets for several C programs that demonstrate different programming concepts: 1) The first program determines if a number is prime or not. 2) The second calculates the area and perimeter of a rectangle given length and width inputs. 3) The third finds the roots of a quadratic equation. 4) The fourth calculates the factorial of a given integer. 5) Additional programs demonstrate concepts like temperature conversion, circle calculations, grade calculation, and summing integers.

Uploaded by

dfdasf
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)
98 views

Board Question C Program Solve

The document contains code snippets for several C programs that demonstrate different programming concepts: 1) The first program determines if a number is prime or not. 2) The second calculates the area and perimeter of a rectangle given length and width inputs. 3) The third finds the roots of a quadratic equation. 4) The fourth calculates the factorial of a given integer. 5) Additional programs demonstrate concepts like temperature conversion, circle calculations, grade calculation, and summing integers.

Uploaded by

dfdasf
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/ 33

/*Write a program in C to detennine whether a number a is prime

or not .

*/

#include <stdio.h>

#include <conio.h>

main()

int n, i, flag=0;

printf("Enter a positive number : ");

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 program that will obtain the length and width of a

rectangle from the user and compute its are a and perimeter.

*/

#include<stdio.h>

#include<conio.h>

main()

float length, width, area, perimeter;

printf("Enter the length and width of rectangle:");

scanf("%f%f ",&length,&width);

area=length*width;

perimeter=2*length+2*width;

printf("Result: %d",perimeter);

getch();

}
/*page-76

An equation of the form

ax^2 + bx+c=0

is know as the quadratic equation . The values of x that

satisfy the equation are know as the roots of the equation.

A quadratic equation has two roots which are given by the following two formulae.

root1=(-b+sqrt(b^2-4ac))/2a.

root2=(-b-sqrt(b^2-4ac))/2a

The program requests the user to input the values of a,b and c and output

roots 1 and root2. */

#include<stdio.h>

#include<conio.h>

#include<math.h>

main()

float a, b, c, discriminant, root1, root2;

printf ("Input values of a, b, and c\n ");

scanf ("%f %f %f",&a, &b, &c);

discriminant=b*b-4*a*c;

if(discriminant<0)

printf ("\n \n Roots are Imaginary\n");

else

root1=(-b+sqrt(discriminant) )/(2.0*a);

root2=(-b-sqrt(discriminant))/(2.0*a);

printf("\n \n Root1=%5.2f\n\n Root2=%5.2f\n",root1,root2);

getch();

}
/* C Program to calculate the factorial of an integer .

*/

#include<stdio.h>

#include<conio.h>

main()

long i,n,fac=1;

printf("Enter value of n:");

scanf("%ld",&n);

for(i=n;i>=1;--i)

fac*=i;

printf("\nFactorial of %ld is %ld",n,fac);

getch(); //to stop the screen

}
/* C program to check whether a number is prime or not. */

#include <stdio.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);

return 0;

}
/* Consider the following Conditional statement:-

if(a>b)

x=a;

else

x=b;

Write the equivalent code segment using a ternary

operator pair "?:"

Ans:- a>b?x=a:x=b;

*/

#include<stdio.h>

#include<conio.h>

main()

int a,b,x;

a>b?x=a:x=b;

if(a>b)

x=a;

else

x=b;

getch();

}
Enter the Radius of circle and Find out its Area and Perimeter.

#include<stdio.h>

#include<conio.h>

main()

float radius , area;

float PI=3.142 ;

printf(" Enter the radius\n");

scanf("%f" , &radius);

area= PI * radius * radius ;

printf(" Area of circle=%f\n " , area);

printf(" Perimeter=%f\n" ,pmeter);

getch();

}
Fibonacci Series:

#include <stdio.h>

#include <conio.h>

main()

int a,i,F=0, S=1,T;

printf("the limit");

scanf("%d",&a);

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

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

T=F+S;

F=S;

S=T;

getch();

}
/*Write a program to compute the sum of the digits of a given

integer number.

*/

#include<stdio.h>

#include<conio.h>

main()

int num,sum=0,r;

printf("Enter a number:");

scanf("%d",&num);

for(num!=0;num=num/10;num++)

r=num%10;

sum=sum+r;

printf("sum of digits of number:%d",sum);

getch();

}
/*Write a cprogram to inter change the values

of two given integer.

*/

#include<stdio.h>

#include<conio.h>

main()

int x,y,t;

printf("Enter the value of x&y \n:");

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

printf("Before Swapping \n x=%d\ny=%d\n",x,y);

t=x;

x=y;

y=t;

printf("After Swapping \n x=%d\ny=%d\n",x,y);

getch();

}
/*Write a C program to computer and print a multiplication table

for numbers 1 to 5

*/

#include <stdio.h>

#include <conio.h>

main()

int n,i;

for(n=1;n<=5;n=n+1){

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

printf("%d X %d=%d\n",n,i,n*i);

getch();

}
/*Write a C program to determine whether a number is odd or

Even and print the massage

Number Is Even

or

Number Is Odd */

#include<stdio.h>

#include<conio.h>

main()

int n;

printf("Enter the number is here:");

scanf("%d",&n);

if(n%2==0)

printf("Number is Even");

else

printf("Number is Odd");

getch();

}
//Program to Calculate perimeter and area of circle

/*Program to find perimeter and area of circle*/

#include <stdio.h>

#include <math.h>

#include<conio.h>

main()

float r,p,a;

printf("Enter the radius of Circle:");

scanf("%f",&r);

p=2*3.14*r;

a=3.14*r*r;

printf("The perimeter of circle is:%f\n",p);

printf("The area of circle is:%f\n",a);

getch();

}
Recursive function for factorial in c:

#include<stdio.h>

int fact(int);

int main(){

int num,f;

printf("\nEnter a number: ");

scanf("%d",&num);

f=fact(num);

printf("\nFactorial of %d is: %d",num,f);

return 0;

int fact(int n){

if(n==1)

return 1;

else

return(n*fact(n-1));

}
/* write a program that will convert the given temperature

in fahrenheit in C celsius using the following formula.

C=(F-32)/1.8

*/

#include<stdio.h>

#include<conio.h>

main()

float c,f;

printf("Enter the temperature in Fahrenheit:");

scanf("%f",&f);

c=((f-32)/1.8);

printf("The temperature in celsius is =%f",c);

getch();

}
/*Write a program in C to Calculate

the area and circumference of a circle.

*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

main()

int r;

float area, circumference;

printf("Enter the value of radius is here:\n");

scanf("%d",&r);

circumference=2*3.1416*r;

printf("The total result=%f, ",circumference);

getch();

}
/*Write a c program to compute the grade of a subject

according the following rules:

suject's mark Grade

>=80+ A

70-79 B

60-69 C

50-59 D

40-49 E

<40 F

*/

#include<stdio.h>

#include<conio.h>

main()

int m1, m2, m3, m4, m5,per;

printf("Enter the 5 subject number:");

scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);

per=(m1+m2+m3+m4+m5)/5;

if(per>=80&&per<=100)

printf("Grade=A");

else if (per>=70&&per<=79)

printf("Grade=B");

else if (per>=60&&per<=69)

printf("Grade=C");
}

else if (per>=50&&per<=59)

printf("Grade=D");

else if (per>=40&&per<=49)

printf("Grade=E");

else

printf("Grade=F");

getch();

return 0;

}
/*PROGRAM TO FIND SUM OF ALL INTEGER WHICH IS > 100 AND< 200

AND WHICH IS DIVISIBLE BY 7*/

#include<stdio.h>

#include<conio.h>

main()

int i,sum = 0;

//scanf("%d",&i);

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

if (i % 9== 0)

sum = sum + i;

printf("\n Sum of all no between 0 and 300 ");

printf("which is divisible by 9 is :: %d",sum);

getch();

/*

**********

OUTPUT

**********

Sum of all no between 100 and 200 which is divisible by 7 is :: 2107

*/
Programming solutions E BALAGURUSAMY
Chapter-1
Problem-1.1
Write a program that will print your mailing address in the following form:
First Line: Name
Second Line : Door No, Street
Third Line: City, Pin code
Solution:
#include <stdio.h>
void main()
{
printf("Sanjida Afrin Mou\n");
printf("35 no Hamidra Dash road\n");
printf("Dhaka\n");
}

Problem -1.2
Modify the above program to provide border line to the address.
#include <stdio.h>
#include <stdlib.h>
void main()
{
printf("Sanjida Afrin Mou\n");
printf("|--------------------------------------|\n");
printf("|35 no Hamidra Dash road |\n");
printf("|Dhaka |\n");
printf("|-------------------------------------|");
}

Problem-1.3
Write a program using one print statement to print the pattern as shown below
#include <stdio.h>
#include <stdlib.h>
void main()
{
printf("*\n**\n***\n****\n");
}
Problem-1.4
Given the radius of a circle, write a program to compute and display its area. Use a symbolic
constant to define PI value and assume a suitable value for radius.
Solution:
#include <stdio.h>
#include <stdlib.h>
#define PI 3.1416
void main()
{
int radius;
float area;
scanf("%d",&radius);
area=PI*(radius*radius);
printf("The area of the cirle is %f",area);
}

Problem1.5
Write a program to output the following multiplication table
5x1=5
5x2=10
………..
………..
5x10=50
Solution:
#include <stdio.h>
#include <stdlib.h>
void main()
{i
nt num1=5,num2,result;
for(num2=1;num2<=10;num2++)
{
result=num1*num2;
printf("%d x %d=%d\n",num1,num2,result);
}}

Problem1.6
Given two integers 20 and 10, write a program that uses a function add() to add these two
numbers and sub() to find the difference of these two numbers and then display the sum in the
following form-
20+10=30
20-10=10
Solution:
#include <stdio.h>
#include <stdlib.h>
int add(int x, int y);
int sub(int x, int y);
main()
{
int sum,dif;
sum=add(10,20);
printf("20+10=%d\n",sum);
dif=sub(20,10);
printf("20-10=%d",dif);
}i
nt add(int x, int y) //function definition
{
int d;
d=x+y;
return(d);
}i
nt sub(int x, int y) //function definition
{
int d;
d=x-y;
return(d);
}

Problem 1.7
Given the values of three variables a, b and c, write a program to compute and display the value
of x, where x=a/(b-c).
Execute your proram for the following values:
(a) a=250, b=85, c=25
(b) a=300, b=70, c=70
Solution:
#include <stdio.h>
#include <stdlib.h>
main()
{
float x;
int a, b, c;
printf("Value of a\n");
scanf("%d",&a);
printf("Value of b\n");
scanf("%d",&b);
printf("Value of c\n");
scanf("%d",&c);
x=a/(b-c);
printf("The value of x is %f",x);
}
Problem 1.8
Relationship between Celsius and Fahrenheit is governed by the formula F=(9C/5)+32
Write a program to convert temperature
(a)From Celsius to Fahrenheit
(b)From Fahrenheit to Celsius
Solution:
#include <stdio.h>
#include <stdlib.h>
main()
{f
loat f,c,result;
char option;
printf("Enter your option F or C F for celcius to Fahrenheit and C for Fahrenheit to
Celcius\n");
option=getchar();
if(option=='F'|| option=='f')
{
printf("Input Celcius\n");
scanf("%f",&c);
result=((9*c)/5)+32;
printf("The Fahrenheit is %f",result);
}
else if(option=='C' || option=='c')
{
printf("Input Fahrenheit\n");
scanf("%f",&f);
result=(5*(32-f))/9;
printf("The Cecius is %f",result);
}
else
{
printf("Wrong option chosen");
}}
Problem 1.9
Area of a triangle is given by the formula A=sqrt(S(S-a)(S-b)(S-c))
Where a, b and c are sides og the triangle and 2S=a+b+c. Write a program to compute the area
of the triangle given the values a,b and c.
Solution:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main()
{
int a, b ,c;
float s, result;
printf("Enter value of a");
scanf("%d",&a);
printf("Enter value of b");
scanf("%d",&b);
printf("Enter value of c");
scanf("%d",&c);
s=(a+b+c)/2;
result=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area is %f",result);
}

Problem-1.10
Distance between two points (x1,y1) and (x2,y2) is governed by the formula
D2=(x2-x1)2+(y2-y1)2
Write a program to compute D given the coordinates of the points.
Solution:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main()
{
int x1,x2,y1,y2;
float d;
printf("Insert X1");
scanf("%d",&x1);
printf("Insert X2");
scanf("%d",&x2);
printf("Insert Y1");
scanf("%d",&y1);
printf("Insert Y2");
scanf("%d",&y2);
d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
printf("Value of D is %f",d);
}

Problem 1.11
A point on the circumference of a circle whose center is (0,0) and (5,6). Write a program to
compute perimeter and area of the circle.
#include<stdio.h>
#include<math.h>
#define pi 3.14159
void main()
{
float r,x1,x2,y1,y2,A;
x1=0;
x2=0;
y1=4;
y2=5;
r=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
A=pi*r*r;
printf("Result=%f",A);
}

Problem-1.12
The line joining the points (2,2) and(5,6) which lie on the circumference of a circle
is the
diameter of the circle. Write a program to compute the are of the circle.
#include<stdio.h>
#include<math.h>
#define pi 3.14159
void main()
{
float D,r,x1,x2,y1,y2,A;
x1=2;
x2=2;
y1=5;
y2=6;
D=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
r=D/2;
A=pi*r*r;
printf("Result=%f",A);
}
Problem 1.14
Write a program to make the equation of a line in form ax+by=c
For a=5, b=8 ,c=18
Solution:
include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
a=5;
b=8;
c=18;
printf("%dx+%dy=%d",a,b,c);
getch();
}
/* page-53

program-3.1

The program in fig 3.1 shows the use of integer arithmetic to

convert a given number of days into months and day.*/

#include<stdio.h>

#include<conio.h>

main()

int months, days;

printf("Enter days \n ");

scanf("%d",&days);

months=days/30;

days=days%30;

printf("Month=%d Days=%d",months,days);

getch();

}
/* page-58

program-3.2

The program in fig 3.2 prints a sequence of squares of numbers.

Note the use of the shorthand operator *=

*/

#include<stdio.h>

#include<conio.h>

#define N 100

#define A 2

main()

int a;

a=A;

while(a<N)

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

a*=a;

getch();

}
/* page-70

program-3.7

The program shows a program using a cast to evaluate the equation sum=E n i=1 (1/i) */

#include<stdio.h>

#include<conio.h>

main()

float sum;

int n;

sum=0;

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

sum=sum+1/(float)n;

printf("%2d %6.4f\n",n,sum);

getch();

}
/* page-74

program-

Acomputer manufacturing company has the

following monthly compensation polocy to their sales-persons:

Minimum base salary:1500.00

Bonus for every computer sold:200.00

commission on the total monthly sales:2per cent

*/

#include<stdio.h>

#include<conio.h>

#define BASE_SALARY 1500

#define BONUS_RATE 200

#define COMMISSION 0.02

main()

int quantity;

float gross_salary,price;

float bonus,commission;

printf("Input number sold and price\n");

scanf("%d %f",&quantity,&price);

bonus=BONUS_RATE*quantity;

commission=COMMISSION*quantity*price;

gross_salary=BASE_SALARY+bonus+commission;

printf("\n");

printf("Bonus=%f\n",bonus);

printf("Commission=%f\n",commission);

printf("Gross salary=%f",gross_salary);

getch();

}
/*page-76

An equation of the form

ax^2 + bx+c=0

is know as the quadratic equation . The values of x that

satisfy the equation are know as the roots of the equation.

A quadratic equation has two roots which are given by the following two formulae.

root1=(-b+sqrt(b^2-4ac))/2a.

root2=(-b-sqrt(b^2-4ac))/2a

The program requests the user to input the values of a,b and c and output

roots 1 and root2. */

#include<stdio.h>

#include<conio.h>

#include<math.h>

main()

float a, b, c, discriminant, root1, root2;

printf ("Input values of a, b, and c\n ");

scanf ("%f %f %f",&a, &b, &c);

discriminant=b*b-4*a*c;

if(discriminant<0)

printf ("\n \n Roots are Imaginary\n");

else

root1=(-b+sqrt(discriminant) )/(2.0*a);

root2=(-b-sqrt(discriminant))/(2.0*a);

printf("\n \n Root1=%5.2f\n\n Root2=%5.2f\n",root1,root2);

getch();

}
/*page-87

A program that reads a character from keyboard and then prints in

reverse case in given in fig.4.3 . that is, if the

inputis uper case, the output will be lower case and vice versa. */

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

main()

char alphabet;

printf("Enter an alphabet ");

putchar('\n'); /*move to next line*/

alphabet=getchar();

if(islower(alphabet))

putchar(toupper(alphabet));/*Revres ansd display*/

else

putchar(tolower(alphabet));/*Rweverse ands display*/

getch();

}
/*page-115

The program cournts the number of boys whose weight is less than

50kg ands higth is gether than 170cm.

*/

#include<stdio.h>

#include<conio.h>

main()

int count, i;

float weight, height;

count=0;

printf("Enter weight ansd hight for 10 boys\n ");

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

scanf("%f %f",&weight, &height);

if(weight<50 && height>170)

count=count+1;

printf("Number of boys with weight < 50kg\n");

printf("and height > 170 cm = %d\n",count);

getch();

}
Collated by
Nur Alam Shohug
Daffodil Institute of it
Batch:CSE-14th

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