#include <stdio.h> int main() { int n, sum = 0, c, array[100]; scanf("%d", &n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); sum = sum + array[c]; } printf("Sum = %d\n",sum); return 0; }
/*
Output ::
5 4 3 2 1
Sum = 15
*/
#include <stdio.h> int main() { int n, sum = 0, c, value; printf("\nEnter the number of integers you want to add :: ");
scanf("%d", &n); printf("\nEnter %d integers :: ",n);
for (c = 1; c <= n; c++) { scanf("%d",&value); sum = sum + value; } printf("\nSum of entered integers :: %d",sum);
return 0; }
/*
Output ::
Enter the number of integers you want to add :: 5Enter 5 integers :: 5 4 3 2 1Sum of entered integers :: 15
*/
#include<stdio.h> long factorial(int); long find_ncr(int, int); long find_npr(int, int); main() { int n, r; long ncr, npr; printf("\nEnter the value of n and r :: ");
scanf("%d%d",&n,&r); ncr = find_ncr(n, r); npr = find_npr(n, r); printf("%dC%d = %ld\n", n, r, ncr); printf("%dP%d = %ld\n", n, r, npr); return 0; } long find_ncr(int n, int r) { long result; result = factorial(n)/(factorial(r)*factorial(n-r)); return result; } long find_npr(int n, int r) { long result; result = factorial(n)/factorial(n-r); return result; } long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }
/*
Output ::
Enter the value of n and r :: 5 25c2 = 10
5p2 = 20
*/
#include <stdio.h> #include <stdlib.h> char *decimal_to_binary(int); main() { int n, c, k; char *pointer; printf("\nEnter an integer in decimal number system :: ");
scanf("%d",&n); pointer = decimal_to_binary(n); printf("\nBinary string of %d is :: %s\n", n, t);
free(pointer); return 0; } char *decimal_to_binary(int n) { int c, d, count; char *pointer; count = 0; pointer = (char*)malloc(32+1); if ( pointer == NULL ) exit(EXIT_FAILURE); for ( c = 31 ; c >= 0 ; c-- ) { d = n >> c; if ( d & 1 ) *(pointer+count) = 1 + '0'; else *(pointer+count) = 0 + '0'; count++; } *(pointer+count) = '\0'; return pointer; }
/*
Output ::
Enter an integer in decimal number system :: 5
Binary string of 5 is ::
*/
#include <stdio.h> int main() { int n, c, k; printf("\nEnter an integer in decimal number system ::");
scanf("%d", &n); printf("\n%d in binary number system is ::", n); for (c = 31; c >= 0; c--) { k = n >> c; if (k & 1) printf("1"); else printf("0"); } printf("\n"); return 0; }
/*
Output :
Enter an integer in decimal number system :: 5
5 in binary number system is :: 101
*/
#include <stdio.h> long gcd(long, long); int main() { long x, y, hcf, lcm; printf("Enter two integers :"); scanf("%ld%ld", &x, &y); hcf = gcd(x, y); lcm = (x*y)/hcf; printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf); printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
return 0; } long gcd(long x, long y) { if (x == 0) { return y; } while (y != 0) { if (x > y) { x = x - y; } else { y = y - x; } } return x; }
/*
Output :
Enter two integers : 510
Greatest common divisor of 5 and 10 = 5
Least common multiple of 5 and 10 = 10
*/
#include <stdio.h> long gcd(long, long); int main() { long x, y, hcf, lcm; printf("Enter two integers :"); scanf("%ld%ld", &x, &y); hcf = gcd(x, y); lcm = (x*y)/hcf; printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf); printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm); return 0; } long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } }
/*
Output :
Enter two integers :510
Greatest common divisor of 5 and 10 = 5
Least common multiple of 5 and 10 = 10*/
#include<stdio.h> long factorial(int); int main() { int n; long f; printf("Enter an integer to find factorial :"); scanf("%d", &n); if (n < 0) printf("Negative integers are not allowed.\n"); else { f = factorial(n); printf("Factorial od %d = %d", n, f); } return 0; } long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); }
/*
Output :
Enter an integer to find factorial : 5Factorial od 5 = 120
*/
#include <stdio.h> long factorial(int); int main() { int number; long fact = 1; printf("Enter a number to calculate it's factorial :"); scanf("%d", &number); printf("Factorial of %d = %d",number,result); return 0; } long factorial(int n) { int c; long result = 1; for (c = 1; c <= n; c++) result = result * c; return result; }
/*
Output :
Enter a number to calculate it's factorial : 5
#include <stdio.h> int main() { int c, n, fact = 1; printf("Enter a number to calculate it's factorial:"); scanf("%d", &n); for (c = 1; c <= n; c++) fact = fact * c; printf("Factorial of %d = %d\n", n, fact); return 0; }
/*
Output :
Enter a number to calculate it's factorial: 5Factorial of 5 = 120
*/
#include <stdio.h> int main() { int n, sum = 0, remainder; printf("Enter an integer :"); scanf("%d",&n); while(n != 0) { remainder = n % 10; sum = sum + remainder; n = n / 10; } printf("\nSum of digits of entered number = %d\n",sum); return 0; }
/*
Output:
Enter an integer : 12345
Sum of digits of entered number = 15
*/
#include <stdio.h> int main() { int year; printf("Enter a year to check if it is a leap year : "); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.\n", year); else if ( year%100 == 0) printf("%d is not a leap year.\n", year); else if ( year%4 == 0 ) printf("%d is a leap year.\n", year); else printf("%d is not a leap year.\n", year); return 0; }
/*
Output : #1
Enter a year to check if it is a leap year : 20102010 is not a leap year.
Output : #2
Enter a year to check if it is a leap year : 20122012 is a leap year.
Output : #3
Enter a year to check if it is a leap year : 20002000 is a leap year.
Output : #4
Enter a year to check if it is a leap year : 21002100 is not a leap year.
*/
#include <stdio.h>
int main()
{
char ch;
int flg;
printf("Input a character : ");
scanf("%c",&ch);
flg=check_vowel(ch);
if(flg==1)
printf("%c is a vowel",ch);
else
printf("%c is not a vowel",ch);
return 0;
}
int check_vowel(char a)
{
if (a=='a'|| a == 'e'|| a == 'i'|| a == 'o'|| a == 'u'||a=='A'|| a == 'E'|| a == 'I'|| a == 'O'|| a == 'U')
return 1;
return 0;
}
/*
Output : #1
Input a character :A
A is a vowel
Output : #2
Input a character :x
x is not a vowel
*/
#include <stdio.h> int main() { char ch; printf("Input a character : "); scanf("%c", &ch); switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("%c is a vowel.\n", ch); break; default: printf("%c is not a vowel.\n", ch); } return 0; }
/*
Output : #1
Input a character : AA is a vowel.Input a character : xx is not a vowel.
*/