Thursday, 1 March 2018

Friday, 13 May 2016

SPA 40 Programs Solution

1.       Write a program to accept a two digit number and display it in reversed form.
Solution:
#include<stdio.h>
void main()
{
    int no, unit, ten,rev=0;
    printf("\n Enter the no:");
    scanf("%d",&no);
    unit=no%10;
    ten=no/10;
    rev=unit*10+ten;
    if(no%10==0)
        printf("The reverse of the no is 0%d",rev);
    else
        printf("The reverse of the no is %d",rev);
}

Output:
Enter the no:50
The reverse of the no is 05
2.       Write a program to display the first n natural numbers, where the value of n is taken from user.
Solution:
#include<stdio.h>
void main()
{
    int n,i,sum=0;
    printf("\n Enter the value of n:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        sum=sum+i;
    }
    printf("The sum of n natural nos is %d",sum);
}

Output:
Enter the value of n:5
The sum of n natural nos is 15
3.       Write a program to find the factorial of a number.
Solution:
#include<stdio.h>
void main()
{
    int n,i,fact=1;
    printf("\n Enter the no whose factorial you want to find:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        fact=fact*i;
    }
    printf("%d!=%d",n,fact);
}

Output:
Enter the no whose factorial you want to find:5
5!=120
4.       Write a program to display the multiplication table of a user entered number. The table must be upto 10.
Solution:
#include <stdio.h>
int main()
{
    int n, i, j;
    printf("Enter an integer to find multiplication table: ");
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        for(j=1;j<=10;j++)
        {
            printf("%d\t", i*j);
        }
        printf("\n");
    }
    return 0;
}
Output:
Enter an integer to find multiplication table: 4
1       2       3       4       5       6       7       8       9       10
2       4       6       8       10      12      14      16      18      20
3       6       9       12      15      18      21      24      27      30
4       8       12      16      20      24      28      32      36      40
5.       Write a program to calculate the value of the following series.
1/2 + 1/3 +1/4…..1/n
Solution:
#include <stdio.h>
int main()
{
    int n, i;
    float sum=0.0;
    printf("Enter an integer No: ");
    scanf("%d",&n);
    for(i=2;i<=n;++i)
    {
        sum=sum+(float)1/i;
    }
    printf("\nThe sum of series 1/2+1/3+...1/n is %f",sum);
    return 0;
}
Output:
Enter an integer No: 5
The sum of series 1/2+1/3+...1/n is 1.283333
6.       Write a program to display the following for the user’s specified number of lines.
*
**
***   
****
*****
|
                             n lines
Solution:
#include <stdio.h>
 int  main()
{
int i,j,n;
printf("Enter line no:");
scanf("%d",&n);
for ( i = 1; i <= n; i++)
{
 for ( j = 1; j <= i; j++)
 {
     printf("*");
 }
 printf("\n");
}
return 0;
}
Output:
Enter line no:6
*
**
***
****
*****
******
7.       Write a program to display the following for the user specified number of lines.
       *
      **
     ***      
    ****
   *****
  ****** 
    |
   n lines
Solution:
#include <stdio.h>
 int  main()
{
int i,j,n;
printf("Enter line no:");
scanf("%d",&n);
for ( i = 1; i <= n; i++)
{
 for ( j = n; j >= i; j--)
 {
     printf(" ");
 }

 for ( j = 1; j <=i; j++)
 {
     printf("* ");
 }
 printf("\n");
}
return 0;
}
Output:
Enter line no:6
      *
     * *
    * * *
   * * * *
  * * * * *
 * * * * * *
8.       Write  a program to display the following asking the user for the number of lines
1
1 2
1 2 3
1 2 3 4
Solution:
#include <stdio.h>
 int  main()
{
int i,j,n;
printf("Enter line no:");
scanf("%d",&n);
for ( i = 1; i <= n; i++)
{
 for ( j = 1; j <= i; j++)
 {
     printf("%d",j);
 }
 printf("\n");
}
return 0;
}
Output:
Enter line no:5
1
12
123
1234
12345
9.       Write a program to display the following asking the user for the number of lines.
                                        1
                                      1 2 A
                                   1 2 3 A B
                                  1 2 3 4 A B C
Solution:
#include <stdio.h>
int main()
{
                int i,j,k,l,n, alpha=65;
                printf("Enter line no:");
              scanf("%d",&n);
                for(i=1;i<=n;i++)   //Row
                {
                    for(j=n;j>i;j--)
                    {
                        printf(" ");
                    }
                    for(k=1;k<=i;k++) // inner loop for no triangle
                    {
                        printf("%d",k);
                    }
                    for(alpha=65,l=1;l<i;l++,alpha++) // inner loop for alphabet triangle
                    {
                        printf("%c",(char)alpha);
                    }

                    printf("\n");
                }
                return 0;
}
Output:
Enter line no:5
    1
   12A
  123AB
 1234ABC
12345ABCD
10.   Write a program to check if the entered number is prime number or not.
Solution:
#include<stdio.h>
int main()
{
    int n,i;
    printf("Enter the No:");
    scanf("%d",&n);
    for(i=2;i<n;i++)
    {
        if(n%i==0)
            break;
    }
    if(i==n)
    {
         printf("%d is a Prime no\n",n);
    }
    else
    {
         printf("%d is not a Prime no\n",n);
    }
    return 0;
}
Output:
Enter the No:11
11 is a Prime no
11.   Write a program to check if the entered number is Armstrong or not.
Solution:
/* C program to check whether a number entered by user is Armstrong or not. */

#include <stdio.h>
#include<math.h>
int main()
{
  int n, n1, rem, len=0, arm=0;
  printf("Enter a positive  integer: ");
  scanf("%d", &n);
  n1=n;
  while(n1!=0)
  {
      len++;
      n1/=10;
  }
  n1=n;
  while(n1!=0)
  {
      rem=n1%10;
      arm=arm+pow(rem,len);
      n1/=10;
  }
  if(arm==n)
    printf("%d is an Armstrong number.",n);
  else
    printf("%d is not an Armstrong number.",n);
}
Output:
Enter a positive  integer: 153
153 is an Armstrong number.
12.   Write a program to check if the year entered is leap year or not.
Solution:
#include<stdio.h>
int main()
{
    int year;
    printf("Enter any year: ");
    scanf("%d",&year);
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
         printf("%d is a leap year",year);
    else
         printf("%d is not a leap year",year);
    return 0;
}
Output:
Enter any year: 2016
2016 is a leap year
13.   Write a program to display the user entered number in words.
Solution:
#include<stdio.h>
int main()
{
   int no,no1,rem,rev=0;
   printf("Enter the No:-> ");
   scanf("%d",&no);
    no1=no;
    while (no1!=0)
    {
        rem = no1%10;
        rev=rev*10+rem;
        no1=no1/10;
    }
    no1=rev;
   while(no1!=0)
   {
        rem=no1%10;
        switch(rem)
        {
            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 number\nPlease try again ....\n");
            break;
        }
        no1=no1/10;
    }
    return 0;
 }
Output:
Enter the No:-> 405
Four Zero Five
14.   Write a program to find value of y using function, where y=xn.
Solution:
#include<stdio.h>
void power(void);
int main()
{
    power();
    return 0;
}

void power()
{
    int x,n,i,res=1;
    printf("\n Enter the base and index:");
    scanf("%d%d",&x,&n);
    for(i=1;i<=n;i++)
    {
        res=res*x;
    }
    printf("%d^%d=%d",x,n,res);
}
Output:
Enter the base and index:2
5
2^5=32
15.   Write a menu driven program to perform add / subtract / multiply / divide / modulus based on the users choice.
Solution:
#include<stdio.h>
int main()
{
    int choice,a,b,res;
    printf("Enter the value of A and B\n");
    scanf("%d%d",&a,&b);
    printf("Enter your choice\n");
    printf("Press 1 to Add\n");
    printf("Press 2 to Subtract\n");
    printf("Press 3 to Multiply\n");
    printf("Press 4 to Divide\n");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1: res=a+b;
                printf("%d+%d=%d",a,b,res);
                break;
        case 2: res=a-b;
                printf("%d-%d=%d",a,b,res);
                break;
        case 3: res=a*b;
                printf("%d*%d=%d",a,b,res);
                break;
        case 4: printf("Quotient of %d/%d=%f",a,b,(float)a/b);
                printf("\nRemainder of %d/%d=%d",a,b,a%b);
                break;
        default:printf("Invalid Input");
    }
}
Output:
Enter the value of A and B
3
5
Enter your choice
Press 1 to Add
Press 2 to Subtract
Press 3 to Multiply
Press 4 to Divide
4
Quotient of 3/5=0.600000
Remainder of 3/5=3
16.   Write a program to display the month name by accepting the month number from user.
Solution:
#include<stdio.h>
int main()
{
   int n;
   printf("Month No:-> ");
   scanf("%d",&n);
   switch(n)
   {
                case 1:
                       printf("January\n");
                       break;
                case 2:
                       printf("February\n");
                       break;
                case 3:
                       printf("March\n");
                       break;
                case 4:
                       printf("April\n");
                       break;
                case 5:
                       printf("MAy\n");
                       break;
                case 6:
                       printf("June\n");
                       break;
                case 7:
                       printf("July\n");
                       break;
                case 8:
                       printf("August\n");
                       break;
                case 9:
                       printf("September\n");
                       break;
                case 10:
                       printf("October\n");
                       break;
                case 11:
                       printf("November\n");
                       break;
                case 12:
                       printf("December\n");
                       break;
                default:
                       printf("invalid Month number\nPlease try again ....\n");
                       break;
      }
    return 0;
 }
Output:
Month No:-> 6
June
17.   Write a program to find GCD and LCM of two numbers.
Solution:
#include<stdido.h>
 void main()
 {
     int i,a,b,min,max,lcm,hcf;
     clrscr();
     printf("Enter the two numbers:");
     scanf("%d%d",&a,&b);
     //find out smallest and largest and assign them to min and max variable respectively
     a<b?(min=a,max=b):(min=b,max=a);
     printf("%d%d",min,max);
     for(i=min;i>=1;i--)
     {
 if(max%i==0&&min%i==0)
 {
  hcf=i;
  break;
 }
     }
     lcm=(a*b)/hcf;
     printf("\nThe LCM=%d\nThe GCD=%d",lcm,hcf);
     getch();
 }

Output:
Enter the two numbers:90
75                                                                            
7590                                                                          
The LCM=450                                                                  
The GCD=15                                                                     

18.   Write a program to add two numbers using function.
Solution:
#include<stdio.h>
void add(void);
int main()
{
    add();
    return 0;
}

void add()
{
    int a,b,sum;
    printf("\n Enter the nos to be added:");
    scanf("%d%d",&a,&b);
    sum=a+b;
    printf("%d+%d=%d",a,b,sum);
}
Output:
Enter the nos to be added:
12
87
12+87=99
19.   Write a program to find the factorial of a number using a function.
Solution:
#include<stdio.h>
void factorial(void);
int main()
{
    factorial();
    return 0;
}

void factorial()
{
    int n,i,fact=1;
    printf("\n Enter the no whose factorial you want to find:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        fact=fact*i;
    }
    printf("%d!=%d",n,fact);
}
Output:
Enter the no whose factorial you want to find:5
5!=120

20.   Write a program to find factorial of a given number, using a recursive function.
Solution:
#include<stdio.h>
int factorial(int);
int main()
{
    int n,i,fact;
    printf("\n Enter the no whose factorial you want to find:");
    scanf("%d",&n);
    fact= factorial(n);
    printf("%d!=%d",n,fact);
    return 0;
}

int factorial(int n)
{
    if(n==0)
    {
        return(1);
    }
    else
    {
        return(n*factorial(n-1));
    }
}
Output:
Enter the no whose factorial you want to find:5
5!=120

21.   Write a program to find value of y using recursive function, where y=xn.
Solution:
#include<stdio.h>
int power(int,int);
int main()
{
    int x,n,i,res=1;
    printf("\n Enter the base and index:");
    scanf("%d%d",&x,&n);
    res= power(x,n);
    printf("%d^%d=%d",x,n,res);
    return 0;
}

int power(int x,int n)
{
    if(n==0)
    {
        return(1);
    }
    else
    {
        return(x*power(x,n-1));
    }
}
Output:
Enter the base and index:2
5
2^5=32
22.   Write a program to accept ‘n’ integers from user into an array and display the average of these numbers.
Solution:
# include<stdio.h>
void main()
{
    int arr[20];
    int i,n;
    float sum=0,avg=0;
    printf("How many elements you want in array: ");
    scanf("%d",&n);
    printf("Enter %d elements\n",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
        sum = sum + arr[i];
    }
    printf("\n Sum of array elements = %f",sum);
    avg = sum/10;
    printf("\n Average = %f",avg);
}
Output:
How many elements you want in array: 5
Enter 5 elements
1
1
1
1
1
 Sum of array elements = 5.000000
 Average = 0.500000
23.   Write a program to find an element in an array and display the index of the element.
Solution:
#include<stdio.h>
void main()
{
        int a[10]={1,0,3,6,8,9,5,8,0,2},d,i,c=0;
        d=10;
        printf("\nEnter the Element to be searched in the Array:");
        scanf("%d",&d);
        for(i=0;i<10;i++)
        {
            if(d==a[i])
            {
                c++;
                printf("\n%d found at %d Location",d,i);
            }
        }
        if(c==0)
        {
            printf("\n%d not found");
        }
        else
        {
            printf("\n%d found %d times",d,c);
        }
}
Output:
Enter the Element to be searched in the Array:8
8 found at 4 Location
8 found at 7 Location
8 found 2 times
24.   Write a program to sort numbers in ascending order.
Solution:

#include<stdio.h>
void main()
{
     int ar[100],j,n,i,tmp;

     printf(" Enter the size of the array \t");
     scanf("%d",&n);

     printf("Now enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }

     printf("\n Array is : ");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }

     for(i=0;i<=n-2;i++)
     {
           for(j=i+1;j<=n-1;j++)
           {
                 if(ar[i]>ar[j])
                 {
                       tmp=ar[j];
                       ar[j]=ar[i];
                       ar[i]=tmp;
                 }
           }
     }

     printf("\n\n Array in the Ascending order is - \n");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
}
Output:
Enter the size of the array    5
Now enter the elements in the array     8  9  2  -1 4
 Array is :      8       9       2       -1      4
 Array in the Ascending order is -
         -1      2       4       8       9
25.    Write a program to accept ‘n’ integers from user into an array and display the largest element.
Solution:

#include<stdio.h>
void main()
{
     int ar[100],l,n,i;
     printf(" Enter the size of the array \t");
     scanf("%d",&n);
     printf("Now enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
     printf("\n Array is : ");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
     for(l=ar[0],i=1;i<n;i++)
     {
        if(l<ar[i])
        {
            l=ar[i];
        }
     }
     printf("\nLargest Element in Array is %d",l);
}
Output:
Enter the size of the array    5
Now enter the elements in the array     8 9 6 10 2
 Array is :      8       9       6       10      2
Largest Element in Array is 10
26.   Write a program to accept ‘n’ integers from user into an array and display the smallest element.
Solution:

#include<stdio.h>
void main()
{
     int ar[100],s,n,i;
     printf(" Enter the size of the array \t");
     scanf("%d",&n);
     printf("Now enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
     printf("\n Array is : ");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
     for(s=ar[0],i=1;i<n;i++)
     {
        if(s>ar[i])
        {
            s=ar[i];
        }
     }
     printf("\nLargest Element in Array is %d",s);
}
Output:
Enter the size of the array    5
Now enter the elements in the array     8 9 2 6 10
 Array is :      8       9       2       6       10
Largest Element in Array is 2
27.   Write a program to sort numbers in descending order.
Solution:

#include<stdio.h>
void main()
{
     int ar[100],j,n,i,tmp;

     printf(" Enter the size of the array \t");
     scanf("%d",&n);

     printf("Now enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }

     printf("\n Array is : ");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }

     for(i=0;i<=n-2;i++)
     {
           for(j=i+1;j<=n-1;j++)
           {
                 if(ar[i]<ar[j])
                 {
                       tmp=ar[j];
                       ar[j]=ar[i];
                       ar[i]=tmp;
                 }
           }
     }

     printf("\n\n Array in the Descending order is - \n");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
}
Output:
Enter the size of the array    5
Now enter the elements in the array     2 9 -1 3 8
 Array is :      2       9       -1      3       8
 Array in the Descending order is -
         9       8       3       2       -1
28.   Write a program to add two matrices of size m x n.
Solution:
#include <stdio.h>
 int main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");
    for (c = 0; c < m; c++)
    {  for (d = 0; d < n; d++)
        {
           scanf("%d", &first[c][d]);
        }
    }
   printf("Enter the elements of second matrix\n");

   for (c = 0; c < m; c++)
   {   for (d = 0 ; d < n; d++)
        {
           scanf("%d", &second[c][d]);
        }
   }
   printf("Sum of entered matrices:-\n");

   for (c = 0; c < m; c++)
   {
      for (d = 0 ; d < n; d++)
      {
         sum[c][d] = first[c][d] + second[c][d];
         printf("%d\t", sum[c][d]);
      }
      printf("\n");
   }
   return 0;
}
Output:
Enter the number of rows and columns of matrix
2 3
Enter the elements of first matrix
1 1 1 1 1 1
Enter the elements of second matrix
2 2 2 2 2 2
Sum of entered matrices:-
3       3       3
3       3       3
29.   Write a program to accept two strings, join them and display the result using string header file.
Solution:
#include<stdio.h>
#include<string.h>
int main()
{
  char str1[20],str2[20];
  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);
  printf("Before concatenation the strings are\n");
  puts(str1);
  puts(str2);
  strcat(str1,str2);
  printf("After concatenation the strings are\n");
  puts(str1);
  puts(str2);
 return 0;
}
Output:
Enter first string
Ahlam
Enter second string
Ansari
Before concatenation the strings are
Ahlam
Ansari
After concatenation the strings are
AhlamAnsari
Ansari
30.   Write a program to accept a string and find its length without using the string header file.
Solution:
#include <stdio.h>
int main(void)
{
    char a[10];
    int len;
    puts("Enter the string");
    gets(a);
    for(len=0;a[len]!='\0';len++);
    printf("Length of string :%s is %d",a,len);

Output:
Enter the string
Ahlam
Length of string :Ahlam is 5
31.   Write a program to reverse a user entered string.
Solution:
#include<stdio.h>
int main()
{
  int i=0,j=0;
  char str[20],rev[20];
  puts("Enter the string");
  gets(str);
  printf("Before reversing the string value is: %s",str);
  while(str[i]!='\0')
  {
      i++;
  }
  i=i-1;
  while(str[j]!='\0')
  {
      rev[j]=str[i];
      j++;
      i--;
  }
  rev[j]='\0';
  printf("After reversing the string is %s",rev);
  return 0;
}
Output:
Enter the string
ansari
Before reversing the string value is: ansariAfter reversing the string is irasna
32.   Write a program to check whether the entered string is palindrome or not (Do not use the string header file).

Solution:
#include <stdio.h>
int main(void)
{
    char a[10];
    int i,len;
    puts("Enter the string");
    gets(a);
    for(len=0;a[len]!='\0';len++);
    for(i=0;i<len/2;i++)
    {
        if(a[i]!=a[len-1-i])
        {
            break;
        }

    }
    if(i==len/2)
    {
        printf("%s is palindrome",a);
    }
    else
    {
        printf("%s is not palindrome",a);
    }
    return 0;
}

Output:
Enter the string
nitin
nitin is palindrome
33.   Write a program to swap two numbers using a function. Pass the values to be swapped to this function using call-by-value method.
Solution:
#include<stdio.h>
void swap(int,int);
void main()
{
                int a,b;
                printf("Enter the value of A");
                scanf("%d",&a);
                printf("Enter the value of B");
                scanf("%d",&b);
                printf("\nBefore Swapping : A=%d and B=%d",a,b);
                swap(a,b);
}
void swap(int a, int b)
{
    int t;
    t=a;
    a=b;
    b=t;
    printf("\nAfter Swapping : A=%d and B=%d",a,b);
}
Output:
Enter the value of A4
Enter the value of B6

Before Swapping : A=4 and B=6
After Swapping : A=6 and B=4
34.   Write a program to swap two numbers using a function. Pass the values to be swapped to this function using call-by-reference method.
Solution:
#include<stdio.h>
void swap(int *,int *);
void main()
{
                int a,b;
                printf("Enter the value of A");
                scanf("%d",&a);
                printf("Enter the value of B");
                scanf("%d",&b);
                printf("\nBefore Swapping : A=%d and B=%d",a,b);
                swap(&a,&b);
}
void swap(int *aptr, int *bptr)
{
    int t;
    t=*aptr;
    *aptr=*bptr;
    *bptr=t;
    printf("\nAfter Swapping : A=%d and B=%d",*aptr,*bptr);
}
Output:
Enter the value of A 10
Enter the value of B 12

Before Swapping : A=10 and B=12
After Swapping : A=12 and B=10
35.   Write a program to accept basic salary from the keyboard. Calculate the gross salary that includes basic salary, 60% DA and 45% HRA and exclude 10% income tax.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
                float basic,gross,hra,da,it,net;
                clrscr();
                printf("Enter the Basic Salary");
                scanf("%f",&basic);
                da = basic*(float)60/100;
                hra = basic*(float)45/100;
                gross = basic+hra+da;
                it = gross*(float)10/100;
                net = gross-it;
                printf("\nBasic Salary: $%f/-",basic);
                printf("\nDA: $%f/-",da);
                printf("\nHRA: $%f/-",hra);
                printf("\nGross Salary: $%f/-",gross);
                printf("\nNet Salary: $%f/-",net);
                getch();
}

Output:
Enter the Basic Salary
2000                                                                            
Basic Salary: $2000.000000/-                                                   
DA: $1200.000000/-                                                             
HRA: $800.000000/-                                                             
Gross Salary: $4000.000000/-                                                   
Net Salary: $3600.000000/-                            
36.   Write a program to accept three integer numbers from user and display the smallest of three using the conditional operator.
Solution:
#include<stdio.h>
void main()
{
                int a,b,c;
                printf("Enter three integer numbers");
                scanf("%d%d%d",&a,&b,&c);
                (a<b&&a<c)?printf("%d is the Smallest",a):((b<a&&b<c)?printf("%d is the Smallest",b):printf("%d is the Smallest",c));
}
Output:
Enter three integer numbers1 4 -2
-2 is the Smallest
37.   Write a program to swap two integers without using temporary variable.
Solution:
#include<stdio.h>
#include<conio.h>

void main()
{
                int a,b;
                clrscr();
                printf("Enter the value of A");
                scanf("%d",&a);
                printf("Enter the value of B");
                scanf("%d",&b);
                printf("\nBefore Swapping : A=%d and B=%d",a,b);
                a=a+b;
                b=a-b;
                a=a-b;
                printf("\nAfter Swapping : A=%d and B=%d",a,b);
                getch();
}
     
Output:
Enter the value of A
234                                                                            
Enter the value of B                                                           
123                                                                            
                                                                             
Before Swapping : A=234 and B=123                                              
After Swapping : A=123 and B=234                                                
38.   Write a program to generate n fibbonacci elements.
Solution:
#include<stdio.h>
int main()
{
int n,i, a=0,b=1,c;
printf("Enter no. of elements you want to display in series");
scanf("%d",&n);
printf("%d\t%d\t",a,b);
for(i=1;i<=n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}
return 0;
}
Output:
Enter no. of elements you want to display in series6
0       1       1       2       3       5
39.   Write a program to accept three integer numbers from user and display the largest of three using if-else.
Solution:
#include<stdio.h>
void main()
{
      int a,b,c;
     printf("Enter three integer numbers");
     scanf("%d%d%d",&a,&b,&c);
     if(a>b&&a>c)
        printf("%d is the largest",a);
    else if(b>a&&b>c)
        printf("%d is the largest",b);
    else
        printf("%d is the largest",c);
}
Output:
Enter three integer numbers 8 6 98
98 is the largest
40.   Write a program to accept n digit number and display it in reversed form.
Solution:
#include<stdio.h>
int main()
{
    int no,no1,rem=0, rno=0;
    printf("Enter the no to be reversed");
    scanf("%d",&no);
    no1=no;
    while (no!=0)
    {
        rem = no%10;
        rno=rno*10+rem;
        no=no/10;
    }
    if(no1%10==0)
    {
        printf("Reverse of %d is 0%d", no1, rno);
    }
    else
    {
        printf("Reverse of %d is %d", no1, rno);
    }
    return(0);
}
Output:
Enter the no to be reversed7090
Reverse of 7090 is 0907