0% found this document useful (0 votes)
12 views10 pages

C1 Basic

The document contains 11 C programming examples: 1) Find the largest of three numbers 2) Check if a number is prime 3) Swap two variables without extra variable 4) Convert binary to decimal 5) Check if year is leap year 6) Check if number is Armstrong number 7) Reverse a number 8) Check if number is palindrome 9) Find lowest common multiple (LCM) of two numbers 10) Find max and min of two numbers without conditions/loops 11) Calculate area of a circle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

C1 Basic

The document contains 11 C programming examples: 1) Find the largest of three numbers 2) Check if a number is prime 3) Swap two variables without extra variable 4) Convert binary to decimal 5) Check if year is leap year 6) Check if number is Armstrong number 7) Reverse a number 8) Check if number is palindrome 9) Find lowest common multiple (LCM) of two numbers 10) Find max and min of two numbers without conditions/loops 11) Calculate area of a circle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1. Find the largest number among the three numbers.

 C

// C Program to find
// Largest of three numbers
#include <stdio.h>

int main()
{
int a = 1, b = 2, c = 3;

// condition for a is greatest


if (a > b && a > c)
printf("%d", a);

// condition for b is greatest


else if (b > a && b > c)
printf("%d", b);

// remaining conditions
// c is greatest
else
printf("%d", c);

return 0;
}

2. Write a Program to check whether a number is prime or not.


 C

// C Program for
// Checking value is
// Prime or not
#include <stdio.h>

int main()
{
int N = 91;
int flag = 0;

// Iterate from 2 to N/2


for (int i = 2; i <= N / 2; i++) {

// If n is divisible by any number between 2 and


// n/2, it is not prime
if (N % i == 0) {
flag = 1;
break;
}
}

if (flag == 1)
printf("Not a Prime Number");
else
printf("Is a Prime Number");
return 0;
}

3. Write a Program in C to Swap the values of two variables without using


any extra variable.
 C

// C Program to
// Swap two numbers
// No Extra Space
#include <stdio.h>

int main()
{

int x = 10;
int y = 20;

printf("x: %d , y: %d\n", x, y);

// Code to swap 'x' and 'y'


x = x + y;
y = x - y;
x = x - y;

printf("x: %d , y: %d\n", x, y);

return 0;
}

4. Write a Program to convert the binary number into a decimal number.


 C

// C Program for converting


// binary to decimal
#include <stdio.h>

int main()
{
int N = 11011;

// Initializing base value a to 1


int a = 1;
int ans = 0;
while (N != 0) {
ans = ans + (N % 10) * a;
N = N / 10;
a = a * 2;
}

printf("%d", ans);
return 0;
}

5. Write a Program to check if the year is a leap year or not.


 C

// C Program to check
// Year is leap year or not
#include <stdio.h>

// Function Declaration to check leap year


void leap_year(int year)
{
// If a year is multiple of 400, then leap year
if (year % 400 == 0)
printf("%d is a leap year.\n", year);

// If a year is multiple of 100, then not a leap year


else if (year % 100 == 0)
printf("%d is not a leap year.\n", year);

// If a year is multiple of 4, then leap year


else if (year % 4 == 0)
printf("%d is a leap year.\n", year);
// Not leap year
else
printf("%d is not a leap year.\n", year);
}

int main()
{
leap_year(2000);
leap_year(2002);
leap_year(2008);

return 0;
}

6. Write a Program to Check if a number is an Armstrong number or not.


 C

// C program to check if number


// is Armstrong number or not
#include <stdio.h>

// Function to calculate x raised to the power y


int power(int x, unsigned int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);

return x * power(x, y / 2) * power(x, y / 2);


}

// Function to calculate order of the number


int order(int n)
{
int res = 0;
while (n) {
res++;
n = n / 10;
}
return res;
}
// Function to check whether the given number is
// Armstrong number or not
int isArmstrong(int x)
{
// Calling order function
int n = order(x);
int temp = x, sum = 0;
while (temp) {
int r = temp % 10;
sum += power(r, n);
temp = temp / 10;
}

// If satisfies Armstrong condition


if (sum == x)
return 1;
else
return 0;
}

// Driver Program
int main()
{
int x = 120;
if (isArmstrong(x) == 1)
printf("True\n");
else
printf("False\n");

x = 1634;
if (isArmstrong(x) == 1)
printf("True\n");
else
printf("False\n");

return 0;
}

7. Write a Program to reverse a number.


 C

// C Programs to Calculate
// reverse of a number
#include <stdio.h>
// Iterative approach
int reverse_iteration(int N)
{
int ans = 0;
while (N != 0) {

ans = ans * 10 + (N % 10);


N = N / 10;
}

return ans;
}

// recursive approach
int reverse(int n, int ans)
{
if (n == 0)
return ans;

ans = ans * 10 + n % 10;


return reverse(n / 10, ans);
}

int main()
{
int N = 15942;
printf("Initial number:%d\n", N);

N = reverse_iteration(N);
printf("%d after reverse using iteration\n", N);

int ans = 0;
ans = reverse(N, ans);
printf("%d after again reverse using recursion", ans);

return 0;
}

8. Check whether a number is a palindrome.


 C

// C Program for
// Checking Palindrome
#include <stdio.h>

// Checking if the number is


// Palindrome number
void check_palindrome(int N)
{
int T = N;
int rev = 0; // This variable stored reversed digit

// Execute a while loop to reverse digits of given


// number
while (T != 0) {
rev = rev * 10 + T % 10;
T = T / 10;
}

// Compare original_number with reversed number


if (rev == N)
printf("%d is palindrome\n", N);
else
printf("%d is not a palindrome\n", N);
}

int main()
{
int N = 13431;
int M = 12345;

// Function call
check_palindrome(N);
check_palindrome(M);

return 0;
}

9. Write a C program to find the LCM of two numbers.


 C

// C program to find
// LCM of two numbers
#include <stdio.h>

// minimum of two numbers


int Min(int Num1, int Num2)
{
if (Num1 >= Num2)
return Num2;
else
return Num1;
}

int LCM(int Num1, int Num2, int K)


{
// If either of the two numbers
// is 1, return their product
if (Num1 == 1 || Num2 == 1)
return Num1 * Num2;

// If both the numbers are equal


if (Num1 == Num2)
return Num1;

// If K is smaller than the


// minimum of the two numbers
if (K <= Min(Num1, Num2)) {

// Checks if both numbers are


// divisible by K or not
if (Num1 % K == 0 && Num2 % K == 0) {

// Recursively call LCM() function


return K * LCM(Num1 / K, Num2 / K, 2);
}

// Otherwise
else
return LCM(Num1, Num2, K + 1);
}

// If K exceeds minimum
else
return Num1 * Num2;
}

int main()
{
// Given N & M
int N = 12, M = 9;

// Function Call
int ans = LCM(N, M, 2);

printf("%d", ans);

return 0;
}

10. Write a C Program to find the Maximum and minimum of two


numbers without using any loop or condition.
 C

// C Program to check
// Maximum and Minimum
// Between two numbers
// Without any condition or loop
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a = 55, b = 23;

// return maximum among the two numbers


printf("max = %d\n", ((a + b) + abs(a - b)) / 2);

// return minimum among the two numbers


printf("min = %d", ((a + b) - abs(a - b)) / 2);

return 0;
}

11. Write a Program to find the area of a circle.


 C

// C program to find area


// of circle
#include <math.h>
#include <stdio.h>
#define PI 3.142

double findArea(int r) { return PI * pow(r, 2); }


int main()
{
printf("Area is %f", findArea(5));
return 0;
}

You might also like