# 2.4 – C procedural programs to practice part-2

by on August 13, 2017

specification:
Accept two numbers into two variables and interchange their values. Finally print the values of variables as output. (swapping two numbers)

Logic:

When a variable is assigned with another variable then the original value of variable is over written by new value. Hence we can’t directly assign a variable to another to interchange the values of variables. Instead we use logic to interchange the values of any two variables.

Example Explained:
20 is assigned to x and 45 is assigned to y
y can’t be directly assigned to x. because, it may overwrite the original value of x.
Hence x is assigned to temp before assigning y to x, in order to hold the original value of x.
temp ( former value of x) is assigned to y after assigning the value of y to x. Program:

```#include<stdio.h>
int main()
{
int x,y,temp;
printf("Accept a number into x:");
scanf("%d",&x);
printf("Accept a number into y:");
scanf("%d",&y);
temp=x;
x=y;
y=temp;
printf("The value of x: %d",x);
printf(" \nThe value of y: %d",y);
return 0;
}
```

Execution:
Accept a number into x:20
Accept a number into y:45
The value of x: 45
The value of y: 20

Specification:
Accept two numbers into two variables from the keyboard and print them by interchanging their values without using temporary variable (Swapping without using a third variable)

Logic:
We can swap the values of two variable using arithmetic operations Program:

```#include<stdio.h>
int main()
{
int x,y;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of y:");
scanf("%d",&y);
x=x+y;
y=x-y;
x=x-y;
printf("The value of x: %d",x);
printf("\nThe value of y: %d",y);
return 0;
}
```

Execution:
Enter the value of x: 20
Enter the value of y: 45
The value of x: 45
The value of y: 20

Specification:
Accept any three digit number and print the sum of all digits.

Logic:
The modulo division of any number with 10 is the last digit.
The modulo division of a single digit number with 10 is the same digit.
Division of any integer by another integer is an integer.
In the example the statement “n=n/10;” every time removes the last digit from the number. We will see how it happens, for example if n is 345 then n/10 yields 34 instead 34.5 because an integer mode expression always returns an integer, that will be over writing on n results loosing of last digit.  Program:

```#include<stdio.h>
int main()
{
int n,first,middle,last,sum;
printf("Enter any 3 digit number:");
scanf("%d",&n);
last=n%10;
n=n/10;
middle=n%10;
n=n/10;
first=n%10;
sum=first+middle+last;
printf("Sum of all the digits %d",sum);
return 0;
}
```

Execution:
Enter any 3 digit number: 345
Sum of all the digits 12

Specification:
Accept any three digit number and print it in reverse.
Logic: Program:

```#include<stdio.h>
int main()
{
int n,first,middle,last,rev;
printf("Enter any 3 digit number:");
scanf("%d",&n);
last=n%10;
n=n/10;
middle=n%10;
n=n/10;
first=n%10;
rev=last*100+middle*10+first*1;
printf("The reverse number %d",rev);
return 0;
}
```

Execution:
Enter any 3 digit number: 345
The reverse number 543

Specification:
Accept a four digit number and print the sum of all the digits and the reverse number.

Program:

```#include<stdio.h>
int main()
{
int n,first,second,third,fourth,sum,rev;
printf("Enter any four digit number:");
scanf("%d",&n);
fourth=n%10;
n=n/10;
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
sum=first+second+third+fourth;
rev=fourth*1000+third*100+second*10+first*1;
printf("Sum of all the digits %d",sum);
printf("\nThe reverse number %d",rev);
return 0;
}
```

Execution:
Enter any four digit number: 5678
The reverse number 8765

Previous post: