# C Programming if-else: Examples-part2

0 like 0 dislike
197 views

### For Indian Students- INR 360/- || For International Students- \$9.99/-

S.No.

Course Name

Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon

0 like 0 dislike
by Goeduhub's Expert (8.3k points)

C Program to find whether an integer is positive or negative

#include
int main()
{
//Fill the code
int num;
scanf(“%d”,&num);
if(num > 0)
printf(“Positive”);
else
printf(“Negative”);
return 0;

Program to Swap Two Numbers Without Using Third Variable

#include<stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);  //consider two numbers as 10 and 20
a = a + b;  //a = 10 + 20 = 30
b = a - b;  //b = 30 - 20 = 10
a = a - b;  //a = 30 - 10 = 20
printf("Numbers after swapping: %d %d", a, b);

C Program to find HCF and LCM of two numbers

Find the highest common factor and the least common multiple of two integers. HCF is also known as the greatest common divisor (GCD) or the greatest common factor (GCF).

#include <stdio.h>

int main() {

int a, b, x, y, t, gcd, lcm;

printf("Enter two integers\n");

scanf("%d%d", &x, &y);

a = x;

b = y;

while (b != 0) {

t = b;

b = a % b;

a = t;

}

gcd = a;

lcm = (x*y)/gcd;

printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);

printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

return 0;

}

C Program to count number of digits in an integer

#include <stdio.h>
int main()
{
int n;
int count = 0;
printf(“\nEnter the number: “);
scanf(“%d”, &n);
while(n != 0)
{
n = n/10;
++count;
}
printf(“\nNumber of digits: %d\n”, count);

C Program to find the sum of digits of a number

# include<stdio.h>

int sum_of_digits(int n)
{
int sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}

int main()
{
int n;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
printf(“\nSum of digits of %d is %d\n”, n,sum_of_digits(n));
return 0;

Note-

• Take Input the number from user, Declare a variable called sum and initialize it to 0.
• while (number > 0)
• Get the rightmost digit of the number using ‘%’ operator by dividing it with 10 and add it to sum
• Divide the number by 10.
• Return sum

C Program to find the sum of natural numbers

#include <stdio.h>

int main()

{
int n;
printf(“\nEnter the number : “);
scanf(“%d”, &n);

int sum = 0;
for(int i = 1; i <= n; i++)
{
sum += i;
}
printf(“\nSum of %d Natural Numbers is %d\n “,n,sum);
return 0;
}