Basic C Programs(PART 1)

1.Swapping Two numbers without Temporary Variables:

include <stdio.h>
int main()
{
  int a, b;
   a=10;
   b=20;
 
 a=a+b;
 b=a-b;
 a=a-b;
  
printf("%d\n", a);
printf("%d", b);
}

2.Swapping Two Numbers with using Temporary Variable:

#include <stdio.h>
int main()
{
  int a, b;
  int temp;
  a=10;
  b=20;
  
  temp=a;
  a=b;
  b=temp;
  
  printf("%d\n", a);
  printf("%d", b);
}

3.Fibonacci Series upto n Series:

#include <stdio.h>
int main()
{
   int i, t, n1 = 0, n2 = 1, n3;
   printf("Enter the number of terms: ");
   scanf("%d", &t);

    for (i = 1; i <= t; ++i) {
        printf("%d, ", n1);
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
    }

    return 0;
}

4.Greatest of three numbers:

#include <stdio.h>
int main()
{
  int n1, n2, n3;
   printf("\nEnter value of n1, n2 and n3:");
   scanf("%d %d %d",&n1,&n2,&n3);

   if((n1>n2)&&(n1>n3))
      printf("\n n1 is greatest");
   else if((n2>n3)&&(n2>n1))
      printf("\n n2 is greatest");
   else
      printf("\n n3 is greatest");
   return 0;
}

5.Greatest of Three numbers using conditinal operators:

# include <stdio.h>

void main()
{
    int a, b, c, n;

    printf("Enter three numbers : ") ;

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

    n = a > b ? (a > c ? a : c) : (b > c ? b : c) ;

    printf("\nThe greatest number is : %d", n) ;
}

Note: Any Related short programs regarding the above code, put it in comment session.

Other Related Posts:

Basic C Programs(PART 2)

Basic C Programs(PART 3)

Basic C Programs(PART 4)

Basic C Programs(PART 5)

Posted on by