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

1.

Write a program to check whether a triangle is valid or not, where the three angles of the
triangle are entered through the keyboard (A triangle is valid if the sum of all the three angles is
equal to 180 degrees.)

Ans:
#include <stdio.h>

int main()
{
int side1, side2, side3;
printf("Enter three sides of triangle: \n");
scanf("%d%d%d", &side1, &side2, &side3);

if((side1 + side2) > side3)


{
if((side2 + side3) > side1)
{
if((side1 + side3) > side2)
{
printf("Triangle is valid.");
}
else
{
printf("Triangle is not valid.");
}
}
else
{
printf("Triangle is not valid.");
}
}
else

By Living Loves
{
printf("Triangle is not valid.");
}

return 0;
}

2. A five-digit number is entered through the keyboard. Write a program to obtain the reversed
number and to determine whether the original and reversed numbers are equal or not.
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder,
originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 +
remainder;
n /= 10;
}
printf("reversed Number Is :%d",reversedInteger);
if (originalInteger == reversedInteger)
printf("%d is equal to the Original Number.",
originalInteger);
else
printf("%d is not equal to the Original
Number.", originalInteger);
else.", originalInteger);

By Living Loves
return 0;
}

3. Write a program using conditional operators to determine whether a year entered through the
keyboard is a leap year or not.
#include <stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d", &year);
(year%4==0 && year%100!=0) ? printf("LEAP YEAR") :
(year%400 ==0 ) ? printf("LEAP YEAR") :
printf("COMMON YEAR");
return 0;
}

4. Write a program to find the greatest of the three numbers entered through the keyboard using
conditional operators.
# include <stdio.h>
void main()
{
int a, b, c, big ;
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
}

5. Write a program to print out all Armstrong numbers between 1 and 500.

#include<stdio.h>
int main(){
int num,r,sum,temp;

By Living Loves
for(num=1;num<=500;num++){
temp=num;
sum = 0;

while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}

return 0;
}

Output:
1 153 370 371 407

6. Write a program to produce the following output:


A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A

By Living Loves
A B C D D C B A
A B C C B A
A B B A
A A
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a=71,l,j,b=1,c=71,k;
for(l=1;l<=7;l++)
{
for(i=65;i<=a;i++)
printf("%c", i);

if(l>1)//space loop
{

for(j=1;j<=b;j++)
printf(" ");
b=b+2;
}

for(k=c;k>=65;k--)
{
if(k==71)
continue;
printf("%c", k);
}

printf("\n");

By Living Loves
a--;

c--;
}
getch();
return 0;
}

7. Write a C Program to convert any given year into its roman equivalent.

The following table shows the roman equivalents of decimal numbers:

Decimal Roman Roman Roman

1 i 100 c
5 v 500 d
10 x 1000 m
50 l
Example:

Roman equivalent of 1988 is mdcccclxxxviii

Roman equivalent of 1525 is mdxxv

#include<stdio.h>
void main()
{
int a;
printf("\nInput the year to get it in Roman numerals");
scanf("%d",&a);
roman(a);
}

roman(int year)
{
if(year>=1000)

By Living Loves
{
printf("m");
roman(year-1000);
}

else if(year>=500)
{
printf("d");
roman(year-500);
}

else if(year>=100)
{
printf("c");
roman(year-100);
}

else if(year>=50)
{
printf("l");
roman(year-50);
}

else if(year>=10)
{
printf("x");
roman(year-10);
}

else if(year>=5)

By Living Loves
{
printf("v");
roman(year-5);
}

else if(year>=1)
{
printf("i");
roman(year-1);
}
}

8. Write a C program to multiply two matrices (two-dimensional arrays) which will be entered by a
user.
#include <stdio.h>

int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i,
j, k;

printf("Enter rows and column for first matrix: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and column for second matrix: ");


scanf("%d %d",&r2, &c2);

// Column of first matrix should be equal to column of second


matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of
second.\n\n");

By Living Loves
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}

// Storing elements of first matrix.


printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}

// Storing elements of second matrix.


printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}

// Initializing all elements of result matrix to 0


for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}

By Living Loves
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}

// Displaying the result


printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
return 0;
}

9. Write a C function to evaluate the series

sin( x ) = x − ( x 3 / 3 ! ) + ( x 5 / 5 ! ) − ( x 7 / 7 ! ) + ….......

#include <stdio.h>

int main()
{
int i, j, n, fact, sign = - 1;
float x, p, sum = 0;

By Living Loves
printf("Enter the value of x : ");
scanf("%f", &x);
printf("Enter the value of n : ");
scanf("%d", &n);

for (i = 1; i <= n; i += 2)
{
p = 1;
fact = 1;
for (j = 1; j <= i; j++)
{
p = p * x;
fact = fact * j;
}
sign = - 1 * sign;
sum += sign * p / fact;
}

printf("sin %0.2f = %f", x, sum);

return 0;
}

9. Write a function to find the binary equivalent of a given decimal integer and display it.
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number
system\n");

By Living Loves
scanf("%d", &n);
printf("%d in binary number system is:\n", n);

for (c = 31; c >= 0; c--)


{
k = n >> c;

if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}

By Living Loves

You might also like