Skip to main content

C program for practice

1.Write a C Program to calculates the area (floating point number with two decimal places) of a Circle given it’s radius (integer value).  The value of Pi is 3.14.

#include <stdio.h>
#define PI 3.14
void main()
{
    int radius;
    float area;
    /* Enter the radius of a circle */
    /* The radius of the circle is taken from the test cases */ 
    scanf("%d", &radius);
area = PI * radius * radius;

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



InputOutput

42
Area of a circle = 5538.96

9
Area of a circle = 254.34

7
Area of a circle = 153.86

50
Area of a circle = 7850.00


2.Write a C program to check if a given Number is zero or Positive or Negative Using if...else statement.

#include <stdio.h>
int main()
{
    double number;
    scanf("%lf", &number); 
if (number <= 0.0)
    {
        if (number == 0.0)
            printf("The number is 0.\n");
        else
            printf("Negative number.\n");
    }
    else
        printf("Positive number.\n");
}


InputOutput

0
The number is 0.

-0.9
Negative number.

14.05
Positive number.

0.45
Positive number.

-50
Negative number.

3.Write a C Program to find the Largest Number (integer)  among Three Numbers (integers) using IF and Logical && operator.

#include <stdio.h>

int main()

{

    int n1, n2, n3;

    scanf("%d %d %d", &n1, &n2, &n3);
 /*Three numbers are accepted from the test case */
/*  Complete the code in the box provided below. Use printf statement as provided below:
printf("%d is the largest number.", n1);
It may be n1, n2 or n3.
*/
if( n1>=n2 && n1>=n3 )
        printf("%d is the largest number.", n1);

    if( n2>=n1 && n2>=n3 )
        printf("%d is the largest number.", n2);

    if( n3>=n1 && n3>=n2 )
        printf("%d is the largest number.", n3);
}


InputOutput

-9 -4 -20
-4 is the largest number.

45 34 67
67 is the largest number.

80 -5 3
80 is the largest number.

4.Write a C program to check whether a given number (integer) is Even or Odd

#include <stdio.h>

int main()

{

    int number;

    scanf("%d", &number);

if(number % 2 == 0)
        printf("%d is even.", number);
    else
        printf("%d is odd.", number);
}



InputOutput

51
51 is odd.

116
116 is even.

24
24 is even.

-25
-25 is odd.

5.Write a C Program to Find the Smallest Number  among Three Numbers (integer values) using Nested IF-ELSE statement.

#include <stdio.h>
int main()
{
    int n1, n2, n3; 
    scanf("%d %d %d", &n1, &n2, &n3);
if (n1<n2)
    {
        if(n1<n3)
            printf("%d is the smallest number.", n1);
        else
            printf("%d is the smallest number.", n3);
    }
    else
    {
        if(n2<n3)
            printf("%d is the smallest number.", n2);
        else
            printf("%d is the smallest number.", n3);
    }
}



InputOutput

90 -9 -8
-9 is the smallest number.

100 200 400
100 is the smallest number.

20 30 10
10 is the smallest number.

77 44 99
44 is the smallest number.


6.The length of three sides are taken as input. Write a C program to find whether a triangle can be formed or not. If not display “This Triangle is NOT possible.” If the triangle can be formed then check whether the triangle formed is equilateral, isosceles, scalene or a right-angled triangle. (If it is a right-angled triangle then only print that it is a Right-angle triangle do not print it as scalene triangle).

#include<stdio.h>

int main()

{

    int a,b,c; 

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

if(a<(b+c)&&b<(a+c)&&c<(a+b))
    {
        if(a==b&&a==c&&b==c)
        printf("Equilateral Triangle");
          else if(a==b||a==c||b==c)
          printf("Isosceles Triangle");
          else   
    if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))
        printf("Right-angle Triangle");
        else if(a!=b&&a!=c&&b!=c)
        printf("Scalene Triangle");
    }
    else
    printf("Triangle is not possible");
}



InputOutput

5 12 13
Right-angle Triangle

9 9 9
Equilateral Triangle

10 4 6
Triangle is not possible

7 6 8
Scalene Triangle


3 4 5
Right-angle Triangle

7.Write a program to find the factorial of a given number using while loop.
#include<stdio.h>

void main()

{

    int n;

    long int fact;  /* n is the number whose factorial we have to find and fact is the factorial */

    scanf("%d",&n); 
int i=1;
fact = 1;
while(i<=n)
    {
        fact*=i;
        i++;
    }
    printf("The Factorial of %d is : %ld",n,fact);
}


InputOutput

7
The Factorial of 7 is : 5040

11
The Factorial of 11 is : 39916800

5
The Factorial of 5 is : 120

10
The Factorial of 10 is : 3628800

8.Write a Program to find the sum of all even numbers from 1 to N where the value of N is taken as input. [For example when N is 10 then the sum is 2+4+6+8+10 = 30]


#include <stdio.h>  

void main()

{

int N, sum=0; 

scanf("%d", &N); 

int i;
for (i = 1; i <= N; i++)
{
if (i % 2 == 0)
sum = sum + i;
}
printf("Sum = %d", sum);
}



InputOutput

15
Sum = 56

25
Sum = 156

10
Sum = 30

30
Sum = 240


9.Write a program to find whether a given character is a Vowel or consonant. A character is taken as input. The character may be in Upper Case or in Lower Case.  

#include <stdio.h>
int main()
{
    char ch;
    scanf("%c",&ch); 
int Lower_case, Upper_case;
Lower_case = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');

   Upper_case = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');

    if (Lower_case|| Upper_case)
        printf("%c is a vowel.", ch);
    else
        printf("%c is a consonant.", ch);
   }



InputOutput

I
I is a vowel.

k
k is a consonant.

C
C is a consonant.

o
o is a vowel.

Comments

Popular posts from this blog

c programming MCQ questions and answers

c programming MCQ  questions and answers 1. Which among the following is the most complete definition of Programming?            a) Programming is a process to solve any logical problems.            b) Programming is a process to solve any numerical problem.            c) Programming is a process to solve any real life problems.            d) Programming is the process of taking an algorithm and encoding it into a notation (using a                    programming language), so that it can be executed by a computer. Solution: (d) Programming is the process of taking an algorithm and encoding it into a notation, a programming language, so that it can be executed by a computer 2. Which of the following controls the way in which the computer system works and give means by which users can interact wit...

array mcq questions in c

1. The elements of an array can be accessed by its            a) name            b) index            c) dimension            d) none of the above Solution: (b) An array is accessed by its index. 2. The elements of array are stored in contiguous memory due to             a) This way computer can keep track only the address of the first element and the addresses                     of  other elements can be calculated.             b) The architecture of computer does not allow arrays to store other than serially             c) Both             d) None Solution: (a) This way computer can keep track only the address of the first element and the addresses of other elements can be calc...

Introduction to c language

Introduction to c C is a programming language that lets us give a computer very specific commands. C was invented in 1972. It's one of the oldest languages to be used even today! It is one of the most widely used programming languages in the world. It has influenced many popular languages, especially c++. But why do we still use such an old programming language? why not something newer and easier to understand,like python? Let's find out. History of C Language History of C language  is interesting to know. Here we are going to discuss a brief history of the c language. C programming language  was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie   is known as the   founder of the c language . It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in  UNIX operating sys...