Comments in C language:
Any thing written with in /* */ is called comments.
The compiler will not execute any thing written within
comments.
It is only used to write comments on different parts of
program.
It is like documentation to other programmers.
specification 9:
Accept two numbers into two variables and interchange their
values. finally print the values of variables as output.
Logic:
When a variable is assigned by another variable, then the original
value of variable is overwriting by new value.
We cannot directly assign a variable to another to
interchange the values of variables.
We take a temporary variable to interchange the values of
two variables.
Program:
#include<stdio.h>
void main()
{
int x,y,temp;
clrscr();
printf(“Accept a number into x:”);
scanf(“%d”,&x);
printf(“Accept a number into y:”);
scanf(“%d”,&y);
printf("Before swapping::X=%d ,Y=%d \n",x,y);
temp=x;
x=y;
y=temp;
printf("After swapping X=%d,Y=%d \n",x,y);
getch();
}
Execution:
Accept a number into x:20
Accept a number into y:45
Before swapping x=20 , y=45
After swapping x=45 , y=20
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 (value of x) is assigned to y after assigning the value
of y to x.
specification 10:
Accept two numbers into two variables from the keyboard and
print them by interchanging their values without using the temporary variable.
Logic:
We can swap the values of two variable using arithmetic
operations
Program:
#include<stdio.h>
void main()
{
int x,y;
clrscr();
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);
getch();
}
Execution:
Enter the value of x: 20
Enter the value of y: 45
The value of x: 45
The value of y: 20
Specification 11:
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.
#include<stdio.h>
void main()
{
int n,first,second,third,sum;
clrscr();
printf(“Enter any three digit number:”);
scanf(“%d”,&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
sum=first+second+third;
printf(“Sum of all the digits %d”,sum);
getch();
}
Execution:
Enter any three digit number: 345
Sum of all the digits 12
Specification 12:
Accept any three digit number and print it in reverse.
Logic:
Program:
#include<stdio.h>
void main()
{
int n,first,second,third,rev;
clrscr();
printf(“Enter any three digit number:”);
scanf(“%d”,&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
rev=third*100+second*10+first*1;
printf(“The reverse number %d”,rev);
getch();
}
EXECUTION:
enter three digits number: 123
The reverse number: 321
Specification13:
Accept a four digit number and print the sum of all the
digits and the reverse number.
Program:
#include<stdio.h>
void main()
{
int n,first,second,third,fourth,sum,rev;
clrscr();
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);
getch();
}
EXECUTION:
Enter any four digit number: 1234
Sum of all the digits: 10
The reverse number: 4321
0 comments:
Post a Comment