0% found this document useful (0 votes)
201 views

C Program To Check If A Given Integer Is Odd or Even

The document contains summaries of several C programs that perform tasks like checking if a number is odd or even, finding the largest of three numbers, reversing a number, swapping values, and more. It also includes programs to illustrate concepts like pass by reference and recursion. The final program listed allows shutting down the computer in Linux using the system command.

Uploaded by

Reddy Babu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views

C Program To Check If A Given Integer Is Odd or Even

The document contains summaries of several C programs that perform tasks like checking if a number is odd or even, finding the largest of three numbers, reversing a number, swapping values, and more. It also includes programs to illustrate concepts like pass by reference and recursion. The final program listed allows shutting down the computer in Linux using the system command.

Uploaded by

Reddy Babu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Program to Check if a given Integer is Odd or Even

#include <stdio.h> void main() { int ival, remainder; printf("Enter an integer : "); scanf("%d", &ival); remainder = ival % 2; if (remainder == 0) printf("%d is an even integer\n", ival); else printf("%d is an odd integer\n", ival); }

C Program to Check if a given Integer is Positive or Negative


#include <stdio.h> void main() { int number; printf("Enter a number \n"); scanf("%d", &number); if (number > 0) printf("%d is a positive number \n", number); else printf("%d is a negative number \n", number); }

C Program to Find the Biggest of 3 Numbers


#include <stdio.h> void main() { int num1, num2, num3; printf("Enter the values of num1, num2 and num3\n"); scanf("%d %d %d", &num1, &num2, &num3); printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3); if (num1 > num2) { if (num1 > num3) { printf("num1 is the greatest among three \n"); } else { printf("num3 is the greatest among three \n"); } } else if (num2 > num3) printf("num2 is the greatest among three \n"); else printf("num3 is the greatest among three \n");

C Program to Reverse a Number & Check if it is a Palindrome


#include <stdio.h> void main() { int num, temp, remainder, reverse = 0; printf("Enter an integer \n"); scanf("%d", &num); /* original number is stored at temp */ temp = num; while (num > 0) { remainder = num % 10; reverse = reverse * 10 + remainder; num /= 10; } printf("Given number is = %d\n", temp); printf("Its reverse is = %d\n", reverse); if (temp == reverse) printf("Number is a palindrome \n"); else printf("Number is not a palindrome \n"); }

C Program to Reverse a Given Number


#include <stdio.h> void main() { long num, reverse = 0, temp, remainder; printf("Enter the number\n"); scanf("%ld", &num); temp = num; while (num > 0) { remainder = num % 10; reverse = reverse * 10 + remainder; num /= 10; } printf("Given number = %ld\n", temp); printf("Its reverse is = %ld\n", reverse); }

C Program to Swap the Contents of two Numbers using Bitwise XOR Operation
#include <stdio.h> void main() { long i, k; printf("Enter two integers \n"); scanf("%ld %ld", &i, &k); printf("\n Before swapping i= %ld and k = %ld", i, k); i = i ^ k; k = i ^ k; i = i ^ k; printf("\n After swapping i= %ld and k = %ld", i, k); }

C Program to Multiply given Number by 4 using Bitwise Operators


#include <stdio.h> void main() { long number, tempnum; printf("Enter an integer \n"); scanf("%ld", &number); tempnum = number; /* left shift by two bits */ number = number << 2; printf("%ld x 4 = %ld\n", tempnum, number); }

C Program to Convert Binary to Octal


#include <stdio.h> int main() { long int binarynum, octalnum = 0, j = 1, remainder; printf("Enter the value for binary number: "); scanf("%ld", &binarynum); while (binarynum != 0) { remainder = binarynum % 10; octalnum = octalnum + remainder * j; j = j * 2; binarynum = binarynum / 10; } printf("Equivalent octal value: %lo", octalnum); return 0; }

C Program to Illustrate Pass by Reference


#include <stdio.h> void cube( int *x); int main() { int num = 10; cube(&num); printf("the cube of the given number is %d", num); return 0; } void { } cube(int *x) *x = (*x) * (*x) * (*x);

C Program to Illustrate Pass by Value


#include <stdio.h> void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } int main() { int num1 = 10, num2 = 20; printf("Before swapping num1 = %d num2 = %d\n", num1, num2); swap(num1, num2); printf("After swapping num1 = %d num2 = %d \n", num2, num1); return 0; }

C Program to Find Sum of Digits of a Number using Recursion


#include <stdio.h> int sum (int a); int main() { int num, result; printf("Enter the number: "); scanf("%d", &num); result = sum(num); printf("Sum of digits in %d is %d\n", num, result); return 0; }

int sum (int num) { if (num != 0) { return (num % 10 + sum (num / 10)); } else { return 0; } }

C Program to find Reverse of a Number using Recursion


#include <stdio.h> #include <math.h> int rev(int, int); int main() { int num, result; int length = 0, temp; printf("Enter an integer number to reverse: "); scanf("%d", &num); temp = num; while (temp != 0) { length++; temp = temp / 10; } result = rev(num, length); printf("The reverse of %d is %d.\n", num, result); return 0; } int rev(int num, int len) { if (len == 1) { return num; } else { return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len)); } }

C Program to Shutdown or Turn Off the Computer in Linux


#include <stdio.h> int main() { system("shutdown -P now"); return 0; }

You might also like