Total Pageviews

Showing posts with label c program. Show all posts
Showing posts with label c program. Show all posts

print numbers 1 to n using while loop


Program to print numbers from 1 to n using while loop.

#include
#include
void main()
{
int i=1, n;
clrscr();
printf("Enter n : ");
scanf("%d", &n);
while(i<=n)
{
printf("%d\t",i);
i++;
}
getch();
}
@

Program to print total marks, average and grade

Program to accept rollnumber and marks of three subjects from user and print total marks, average and grade.

#include
#include
void main()
{
  int RollNum, s1, s2, s3, total;
  float avg;
  clrscr();
  printf("Enter Roll Number : ");
  scanf("%d",&RollNum);
  printf("Enter marks for three subjects : ");
  scanf("%d%d%d", &s1, &s2, &s3);

  total=s1+s2+s3;
  avg=total/3.0;

  printf("\nTotal is : %d", total);
  printf("\nAverage is : %5.2f", avg);

  if(avg>80)
    printf("\nGrade : A");
  else if((avg>60)&&(avg<=80))
    printf("\nGrade : B");
  else if((avg>40)&&(avg<=60))
    printf("\nGrade : C");
  else if((avg>=33)&&(avg<=40)
    printf("\nGrade : D");
  else
    printf("\nGrade : Fail");

getch();
}

Program to accept three numbers from user and print them in ascending and descending order

Program to accept three numbers from user and print them in ascending and descending order

#include
#include
void main()
{
  int a,b,c;
  clrscr();
  printf("Enter numbers:");
  scanf("%d%d%d",&a,&b,&c);
  if((a>=b)&&(a>=c))
    {
      if(b>=c)
        {
          printf("\n Descending order: %d %d %d",a,b,c);
          printf("\n Ascending order : %d %d %d",c,b,a);
        }
      else
        {
        printf("\n Descending order : %d %d %d",a,c,b);
        printf("\n Ascending order : %d %d %d",b,c,a);
        }
      }
  else if((b>=a)&&(b>=c))
    {
      if(a>=c)
        {
          printf("\n Descending order : %d %d %d",b,a,c);
          printf("\n Ascending order : %d %d %d",c,a,b);
        }
      else
        {
        printf("\n Descending order : %d %d %d",b,c,a);
        printf("\n Ascending order : %d %d %d",a,c,b);
        }
    }
  else 
    {
      if(a>=b)
        {
          printf("\n Descending order : %d %d %d",c,a,b);
          printf("\n Ascending order : %d %d %d",b,a,c);
        }
      else
        {
        printf("\n Descending order : %d %d %d",c,b,a);
        printf("\n Ascending order : %d %d %d",a,b,c);
        }
    }
getch();
}

Program to check whether the number is even or odd

Program to check whether the number is even or odd.

#include
#include
void main()
{
  int n;
  clrscr();
  printf("Enter number: );
  scanf("%d",&n);
  if(n%2==0)
    printf("Number is even");
  else
    printf("Number is odd");
  getch();
}

Program to accept a number and check whether the number is Positive, Negative or Zero

Program to accept a number and check whether the number is Positive, Negative or Zero

#include
#include
void main()
{
int n;
clrscr();
printf("Enter number: ");
scanf("%d",&n);
if(n>0)
printf("Number is positive");
else if(n<0)
printf("Number is negative");
else
printf("Number is Zero");
getch();
}

Program to accept two number and print largest among them


Program to accept two number and print largest among them.

#include
#include
void main()
{
int a,b;
clrscr();
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
if(a>b)
printf("Largest value is: %d",a);
else
printf("Largest value is: %d",b);
getch();
}

Program to accept two values of a & b and swap their values using temp/ addition/ multiplication


#include
#include
void main()
{
  int a,b,temp;
  clrscr();
  printf("Enter 1st number: ");
  scanf("%d",&a);
  printf("Enter 2nd number: ");
  scanf("%d",&b);
  printf("\nBefore Swapping..\n A=%d, B=%d",a,b);
  temp=a;                                               //a=a+b    or a=a*b
  a=b;                                                   //b=a-b    or b=a/b
  b=temp;                                                 //a=a-b    or a=a/b
  printf("\nAfter Swapping..\n A=%d, B=%d",a,b);
  getch();
}

Program to accept a number from user and print it’s square & cube

Program to accept a number from user and print it’s square & cube.

#include
#include
void main()
{
  int n,sqre,cube;
  clrscr();
  printf("Enter Number: ");
  scanf("%d",&n);
  sqre=n*n;
  cube=n*n*n;
  printf("\nSquare: %d\nCube: %d",sqre,cube);
  getch();
}

Program to accept value of radius and print area of a circle


 Program to accept value of radius and print area of a circle.

#include
#include
void main()
{
  float area,radius;
  clrscr();
  printf("Enter Radius:");
  scanf("%f",&radius);
  area=3.14*radius*radius;
  printf("Area of the given radius is : %6.2f",area);
  getch();
}

Program to print simple interest


Program to print simple interest

#include
#include
void main()
{
  float interest, p, r, n;
  clrscr();
  printf("Enter value of P: ");
  scanf("%f",&p);
  printf("Enter value of R: ");
  scanf("%f",&r);
  printf("Enter value of N: ");
  scanf("%f",&n);
  interest=p*r*n/100f;
  printed("Simple Interest : %f", interest);
  getch();
}

Program to accept values of two numbers and print their addition with scanf


Program to accept values of two numbers and print their addition.

#include
#include
void main()
{
  int a,b,ans;
  clrscr();
  printf("Enter 1st number:");
  scanf("%d",&a);
  printf("Enter 2nd number:");
  scanf("%d",&b);
  ans=a+b;
  printf("Addition is : %d",ans);
  getch();
}

Program to printing the given number in words

Printing the given number in words 
#include

int main()
{
  int number;
  scanf("%d",&number);
  switch(number)
    {
        case 1:
             printf("One");
             break;
        case 2:
             printf("Two");
              break;
        case 3:
             printf("Three");
             break;
        case 4:
            printf("Four");
            break;
        case 5:
           printf("Five");
           break;
        case 6:
             printf("Six");
             break;
        case 7:
             printf("Seven");
              break;
        case 8:
             printf("Eight");
             break;
        case 9:
            printf("Nine");
            break;
        case 0:
           printf("Zero");
           break;
        default:
             printf("Invalid");
    }
  return 0;
}

C Program to assign values of two numbers and print their addition without scanf

Program to assign values of two numbers and print their addition.

#include
#include
void main()
{
  int a,b,ans;
  clrscr();
  a=10;
  b=20;
  ans=a+b;
  printf("Addition is : %d",ans);
  getch();
}

Program to print "Hello World!!".

/*
 * First C program that says Hello (Hello.c)
 */
#include               // Needed to perform IO operations
 
int main() 
{                               // Program entry point
    printf("Hello, world!\n");  // Says Hello
    return 0;                   // Terminate main()
}                               // End of main()





output

 Hello, world!