Uploaded by Rama Mercy cs

C

advertisement
C Program to check whether the given
integer is positive or negative
/* Description: A program to check whether the input
* integer number is positive or negative.
* Written by: Chaitanya Singh
* Published on: beginnersbook.com
*/
#include <stdio.h>
void main()
{
int num;
printf("Enter a number: \n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
}
Output 1:
Enter a number:
0
0 is neither positive nor negative
Output 2:
Enter a number:
-3
-3 is a negative number
Output 3:
Enter a number:
100
100 is a positive number
Program to find largest number using if statement
. User is asked to enter three numbers one by one. The program store these
numbers into three variables num1, num2 and num3 using scanf() function.
2. Program compares num1 to other two variables num2 & num3 and if num1 is
grater than both of these numbers then print num1 is the largest number.
3. Similarly compares num2 with num1 & num3 and if greater print num2 is the
largest number.
4. Similar to step 2 and 3, compare num3 with num1 and num2 and if greater
print num3 is the largest number.
#include <stdio.h>
int main() {
double num1, num2, num3;
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
printf("Enter third number: ");
scanf("%lf", &num3);
// if num1 is greater than num2 & num3, num1 is the largest
if (num1 >= num2 && num1 >= num3)
printf("%lf is the largest number.", num1);
// if num2 is greater than num1 & num3, num2 is the largest
if (num2 >= num1 && num2 >= num3)
printf("%lf is the largest number.", num2);
// if num3 is greater than num1 & num2, num3 is the largest
if (num3 >= num1 && num3 >= num2)
printf("%lf is the largest number.", num3);
return 0;
}
Output:
https://www.programiz.com/c-programming/examples
Program to Print an Integer
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
// reads and stores input
scanf("%d", &number);
// displays output
printf("You entered: %d", number);
return 0;
}
Run Code
Output
Enter an integer: 25
You entered: 25
In this program, an integer variable
number
is declared.
int number;
Then, the user is asked to enter an integer number. This number is stored
in the
number
variable.
printf("Enter an integer: ");
scanf("%d", &number);
Finally, the value stored in
number
printf("You entered: %d", number);
is displayed on the screen using
printf() .
Program to Add Two Integers
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculate the sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
Run Code
Output
Enter two integers: 12
11
12 + 11 = 23
In this program, the user is asked to enter two integers. These two integers
are stored in variables
number1
and
number2
respectively.
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
Then, these two numbers are added using the
stored in the
sum
variable.
sum = number1 + number2;
+
operator, and the result is
Add Two Numbers
Finally, the
printf()
function is used to display the sum of numbers.
printf("%d + %d = %d", number1, number2, sum);
Download