c program to add two numbers:-
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter two number to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of Enter number = %d\n",c);
getch();
}
output:-
Addition without using third variable:-
#include<stdio.h>
void main()
{
int a = 1, b = 2;
/* Storing result of addition in variable a */
a = a + b;
/* Not recommended because original value of a is lost
* and you may be using it some where in code considering it
* as it was entered by the user.*/
printf("Sum of a and b = %d\n", a);
getch();
}
C program to add two numbers repeatedly:-
#include<stdio.h>
void main()
void main()
{
int a, b, c;
char ch;
while(1)
{
printf("Enter values of a and b\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("a + b = %d\n", c);
printf("Do you wish to add more numbers(y/n)\n");
scanf(" %c",&ch);
if ( ch == 'y' || ch == 'Y' )
continue;
else
break;
}
getch();
}
Adding numbers in c using function:-
#include<stdio.h>
#include<conio.h>
long addition(long, long);
void main()
{
long first, second, sum;
scanf("%ld%ld", &first, &second);
sum = addition(first, second);
printf("%ld\n", sum);
getch();
}
long addition(long a, lengthy b)
{
long result;
return = a + b;
return result;
}
No comments:
Post a Comment