C program to find GCD Using for loop In this program, two integers entered by the user are stored in […]
Category: C Programs
C program to find HCF and LCM using function
C program to find HCF and LCM using function
1 |
#include long fun(long, long); // function declarationint main(){long a, b, hc, lc;printf("Enter two numbers: ");scanf("%ld%ld", &a, &b);hc = fun(a, b);// Function Callinglc = (a*b)/hc;printf(" The highest Common Factor of %ld and %ld = %ld\n", a, b, hc);printf(" The Least Common Multiple of %ld and %ld = %ld\n", a, b, lc);return 0;}long fun(long a, long b) // Function definition{if (a == 0){return b;}while (b != 0) {if (a > b) {a = a - b;}else {b = b - a;}}return a;} |
C program to find HCF and LCM using recursion
C program to find HCF and LCM using recursion This tutorial on finding HCF and LCM of a whole number […]
Calculate the Power of a Number using recursion
Calculate the Power of a Number using recursion
1 |
#include <stdio.h>int power(int n1, int n2);int main(){int base, powerRaised, result;printf("Enter base number: ");scanf("%d",&base);printf("Enter power number(positive integer): ");scanf("%d",&powerRaised);result = power(base, powerRaised);printf("%d^%d = %d", base, powerRaised, result);return 0;}int power(int base, int powerRaised){if (powerRaised != 0)return (base*power(base, powerRaised-1));elsereturn 1;} //OutputEnter base number: 3Enter power number(positive integer): 43^4 = 81</stdio.h> |
C Program to Find HCF and LCM
C Program to Find HCF and LCM Writing a program to find the HCF and LCM of two whole numbers […]
Calculate the Power of a Number Using pow() Function
Calculate the Power of a Number Using pow() Function
1 |
#include <stdio.h>#include <math.h>int main(){double base, exponent, result;printf("Enter a base number: ");scanf("%lf", &base);printf("Enter an exponent: ");scanf("%lf", &exponent);// calculates the powerresult = pow(base, exponent);printf("%.1lf^%.1lf = %.2lf", base, exponent, result);return 0;}//OutputEnter a base number: 2.3Enter an exponent: 4.52.3^4.5 = 42.44</math.h></stdio.h> |
C Program to Calculate the Power of a Number using while Loop
C Program to Calculate the Power of a Number using while Loop
1 |
#include <stdio.h>int main(){int base, exponent;long long result = 1;printf("Enter a base number: ");scanf("%d", &base);printf("Enter an exponent: ");scanf("%d", &exponent);while (exponent != 0){result *= base;--exponent;}printf("Answer = %lld", result);return 0;}//OutputEnter a base number: 5Enter an exponent:6Answer = 15625</stdio.h> |
C Program to Calculate the Power of a Number using for loop
C Program to Calculate the Power of a Number using for loop Input base and exponents from the user. Store […]
Pascal triangle program in c language
Pascal triangle program in c language
1 |
#include<stdio.h>long fact(int);int main(){int line,i,j;printf("Enter the no. of lines: ");scanf("%d",&line);for(i=0;i<><line-i-1;j++)printf(" ");for(j="0;j<=i;j++)printf("%ld" ",fact(i)/(fact(j)*fact(i-j)));printf("\n");}return="" 0;}long="" fact(int="" num){long="" f="1;int" i="1;while(i<=num){f=f*i;i++;}return"></line-i-1;j++)printf("></stdio.h> |
1 |
<span style="color: #ff6600;"> Sample output:</span>Enter the no. of lines: 7 11 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 |
C program to print perfect numbers from 1 to n
C program to print perfect numbers from 1 to n A perfect number is a positive number which sum of […]
C program to check perfect number
C program to check perfect number A perfect number is a positive number which sum of all positive divisors excluding […]
C program to print all prime numbers between 1 to N using for loop
C program to print all prime numbers between 1 to N using for loop
1 |
#include<stdio.h>#include<conio.h>int main(){int N, i, j, isPrime, n;printf("To print all prime numbers between 1 to N\n");printf("Enter the value of N\n");scanf("%d",&N);/* For every number between 2 to N, checkwhether it is prime number or not */printf("Prime numbers between %d to %d\n", 1, N);for(i = 2; i <= N; i++){isPrime = 0;/* Check whether i is prime or not */for(j = 2; j <= i/2; j++){/* Check If any number between 2 to i/2 divides Icompletely If yes the i cannot be prime number */if(i % j == 0){isPrime = 1;break;}}if(isPrime==0 && N!= 1)printf("%d ",i);}getch();return 0;}</conio.h></stdio.h> |
Prime Number Program in C using while loop
Prime Number Program in C using while loop
1 |
#include<stdio.h>int main(){int a,i,f;printf("Enter a number: ");scanf("%d",&a);f=0;i=2;while(i <= a/2){if(a%i == 0){f=1;break;}i++;}if(f==0)printf("Prime Number")elseprintf("Not Prime Number");return 0;}</stdio.h> |
Prime Number program in C using for loop
Prime Number program in C using for loop
1 |
#include<stdio.h>int main(){int a,i,f;printf("Enter a number: ");scanf("%d",&a);f=0;for(i=2;i <= a/2;i++){if(a%i == 0){f=1;break;}}if(f==0)printf("Prime Number")elseprintf("Not Prime Number");return 0;}</stdio.h> |
Check Prime Number or not program in C
Check Prime Number or not program in C Prime number in C: Prime number is a number that is greater than […]
Swap two numbers using pointers in C
Swap two numbers using pointers in C
1 |
#include <stdio.h>int main(){int x, y, *a, *b, temp;printf("Enter the value of x and y\n");scanf("%d%d", &x, &y);printf("Before Swapping\nx = %d\ny = %d\n", x, y);a = &x;b = &y;temp = *b;*b = *a;*a = temp;printf("After Swapping\nx = %d\ny = %d\n", x, y);return 0;}</stdio.h> |
Swapping numbers using call by reference in C
Swapping numbers using call by reference in C
1 |
#include <stdio.h>void swap(int*, int*);int main(){int x, y;printf("Enter the value of x and y\n");scanf("%d%d",&x,&y);printf("Before Swapping\nx = %d\ny = %d\n", x, y);swap(&x, &y);printf("After Swapping\nx = %d\ny = %d\n", x, y);return 0;}void swap(int *a, int *b){int temp;temp = *b;*b = *a;*a = temp;}</stdio.h> |
Swapping of two numbers without a third variable
Swapping of two numbers without a third variable You can also swap two numbers without using temp or temporary or third variable. […]
C programming code to swap using bitwise XOR
C programming code to swap using bitwise XOR
1 |
#include <stdio.h>int main(){int x, y;scanf("%d%d", &x, &y);printf("x = %d\ny = %d\n", x, y);x = x ^ y;y = x ^ y;x = x ^ y;printf("x = %d\ny = %d\n", x, y);return 0;}</stdio.h> |
Swap Two Numbers Program in C
Swap Two Numbers Program in C swapping two variables refers to mutually exchanging the values of the variables. Usually, this […]