#include<stdio.h>
int main()
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
if (num > 0)
printf("n%d is a positive number.", num);
else if (num < 0)
printf("n%d is a negative number.", num);
else
printf("nYou have entered 0.");
}
Computer Science » C Program » Find Sum of Digits of a Number Program : [php]#include <stdio.h> int main() { long number, digit, sum = 0; printf("Enter the number :"); scanf("%ld", &number); while (number > 0) { digit = number % 10; sum = sum + digit; number = number / 10; } printf("nSum of the digits of given number is : %ld", sum); }[/php] Output : [php]Enter the number : 2882 Sum of the digits of given number is : 20[/php]
Computer Science » C Program » Inverted Pyramid Program : [php]#include<stdio.h> int main() { int i, j; char ch = '*'; for (i = 4; i >= 0; i--) { printf("n"); for (j = 0; j < i; j++) printf("%c", ch); } return(0); }[/php] Output : [php]**** *** ** *[/php]
Computer Science » C Program » Find the Number of Elements in An Array Program : [php]#include <stdio.h> int main() { int array[] = {10, 20, 30, 40, 50, 60, 70,80,90,100}; int n,m; n = sizeof(array); m = n/sizeof(int); printf("Size of the given array is : %d", m); return 0; }[/php] Output : [php]Size of the given array is : 10[/php]
Computer Science » C Program » Check Whether a Number is Armstrong or Not Program : [php]#include<stdio.h> int main() { int num, temp, sum = 0, rem; printf("nEnter a number : "); scanf("%d", &num); temp = num; while (num != 0) { rem = num % 10; sum = sum + (rem * rem * rem); num = num / 10; } if (temp == sum) printf("n%d is a amstrong number.", temp); else printf("n%d is not a amstrong number", temp); return (0); }[/php] Output 1: [php]Enter a number : 153 153 is a amstrong number.[/php] Output 2: [php]Enter a number : 150 150 is not a amstrong number[/php]
Computer Science » C Program » Check Whether a Number is Perfect or Not Program : [php]#include<stdio.h> int main() { int num, i = 1, sum = 0; printf("nEnter a number : "); scanf("%d", &num); while (i < num) { if (num % i == 0) { sum = sum + i; } i++; } if (sum == num) printf("n%d is a perfect number.", i); else printf("n%d is not a perfect number.", i); return 0; }[/php] Output 1: [php]Enter a number : 6 6 is a perfect number.[/php] Output 2: [php]Enter a number : 5 is not a perfect number.[/php]