-->

9. C-Programming Examples

C-Nesting of Loops: Previous                                                               Next: C-Functions
Quest1.    Write a program to check whether an entered number is Even or Odd.
Solution:  #include<stdio.h>
void main( )
                {
                                int  num;
                                printf(“Enter Number : ”);
                                scanf(“%d”, &num);
                                if(num % 2 = = 0)
                                                printf(“Number is Even”);
                                else
                                                printf(“Number is Odd”);
                }
Output:





Quest2.    Write a C program to check whether an entered number is Negative or Positive.

Solution:  #include<stdio.h>
void main( )
                {
                                int  num;
                                printf(“Enter Number : ”);
                                scanf(“%d”, &num);
                                if(num >= 0)
                                                printf(“Number is Positive”);
                                else
                                                printf(“Number is Negative”);
                }
Quest3.    Write a C program to calculate circumference of a circle.
Solution:
#include<stdio.h>
void main( )
                {
                                int  r;
                                float area;
                                printf(“Enter Radius : ”);
                                scanf(“%d”, &r);
                               
                                area = 2 * 3.14 * r;
                                printf(“Area is %f”, area);
                }
Quest4.    Write a program to find largest number among three inputted numbers.
Solution:
#include<stdio.h>
                #include<conio.h>
void main( )
                {
                                int  num1, num2, num3;
                                clrscr();
                                printf(“Enter any 3 Numbers : ”);
                                scanf(“%d %d %d”, &num1, &num2, &num3);
                                if(num1 > num2)
                                {
                                                if(num1 > num3)
                                                {
                                                                printf(“%d is largest number”, num1);
                                                }
                                                else
                                                {
                                                                printf(“%d is largest number”, num3);
                                                }
                                }
                                else if(num2 > num3)
                                {
                                                printf(“%d is largest number”, num2);
                                }
                                else
                                {
                                                printf(“%d is largest number”, num3);
                                }
                                getch();
                }
Output:

Ques5.     Write a C program to display numbers from 1 to 100.
Solution:
#include<conio.h>

                #include<stdio.h>
void main( )
                {
                                int  i;
                               
                                for(i = 1; i <= 100; i++)
                                {
                                                printf(“%d \n”, i);
                                }
                                getch();
                }
Ques6.    Write a program to display only Odd numbers between 1 to 50.
Solution:  #include<stdio.h>
                #include<conio.h>
void main( )
                {
                                int  i;
                               
                                for(i = 1; i <= 50; i++)
                                {
                                                if(i % 2 = = 1)
                                                                printf(“%d \n”, i);
                                }
                                getch();
                }
Output:

Ques7.     Write a C program to display count of digits of entered number using while loop.
Solution:  #include<stdio.h>
                #include<conio.h>
void main( )
                {
                                int  num, count = 0;
                               
printf(“Enter a Number : ”);
scanf(“%d”, &num);
while(num)
{
                count = count + 1;
                num = num / 10;
}
                                printf(“Count of Digit is %d”, count);
                                getch();
                }
Output:

Ques8.     Write a program to whether an entered number is Prime or Not .
#include<conio.h>

Solution:  #include<stdio.h>
void main( )
                {
                                int  num, div = 2;
                                clrscr();
printf(“Enter a Number : ”);
scanf(“%d”, &num);
while(div <  num)
{
                if(num % div = = 0)
                                break;
                div = div + 1;
}|
if(div = = num)
printf(“Number is Prime”);
                                else
printf(“Number is not Prime”);
                                getch();
                }
Output:

C-Nesting of Loops: Previous                                                               Next: C-Functions