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

c practical with output 1

The document contains a series of C programming examples that demonstrate basic programming concepts, including finding the greater of two numbers, checking if a number is negative, determining even or odd numbers, calculating simple interest, and more. Each example includes code snippets along with output examples to illustrate the functionality. The document serves as a practical guide for beginners to understand fundamental programming tasks.

Uploaded by

bdsingh9040
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

c practical with output 1

The document contains a series of C programming examples that demonstrate basic programming concepts, including finding the greater of two numbers, checking if a number is negative, determining even or odd numbers, calculating simple interest, and more. Each example includes code snippets along with output examples to illustrate the functionality. The document serves as a practical guide for beginners to understand fundamental programming tasks.

Uploaded by

bdsingh9040
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1.

Find the Greater of Two Numbers

#include <stdio.h>

#include <conio.h>

void main() {

int a, b;

clrscr();

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

if (a > b) {

printf("%d is greater", a);

} else {

printf("%d is greater", b);

getch();

Output Example:

Enter two numbers: 5 8

8 is greater

2. Check if a Number is Negative

#include <stdio.h>

#include <conio.h>

void main() {

int num;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

if (num < 0) {
printf("Number is negative");

} else {

printf("Number is not negative");

getch();

Output Example:

Enter a number: -7

Number is negative

3. Check Even or Odd

#include <stdio.h>

#include <conio.h>

void main() {

int num;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0) {

printf("Even number");

} else {

printf("Odd number");

getch();

Output Example:

Enter a number: 11

Odd number

4. Check if Integer is in a Range


#include <stdio.h>

#include <conio.h>

void main() {

int num, lower, upper;

clrscr();

printf("Enter number, lower range, upper range: ");

scanf("%d %d %d", &num, &lower, &upper);

if (num >= lower && num <= upper) {

printf("Number is in the range");

} else {

printf("Number is out of range");

getch();

Output Example:

Enter number, lower range, upper range: 15 10 20

Number is in the range

5. Calculate Simple Interest

#include <stdio.h>

#include <conio.h>

void main() {

float p, r, t, si;

clrscr();

printf("Enter principal, rate, and time: ");

scanf("%f %f %f", &p, &r, &t);

si = (p * r * t) / 100;

printf("Simple Interest: %.2f", si);


getch();

Output Example:

Enter principal, rate, and time: 1000 5 2

Simple Interest: 100.00

6. Check Leap Year

#include <stdio.h>

#include <conio.h>

void main() {

int year;

clrscr();

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

printf("Leap Year");

} else {

printf("Not a Leap Year");

getch();

Output Example:

Enter a year: 2024

Leap Year

7. Print First 10 Natural Numbers

#include <stdio.h>

#include <conio.h>

void main() {

int i;
clrscr();

for (i = 1; i <= 10; i++) {

printf("%d ", i);

getch();

Output:

1 2 3 4 5 6 7 8 9 10

8. Sum of First 10 Natural Numbers

#include <stdio.h>

#include <conio.h>

void main() {

int i, sum = 0;

clrscr();

for (i = 1; i <= 10; i++) {

sum += i;

printf("Sum = %d", sum);

getch();

Output:

Sum = 55

9. Check Prime Number

#include <stdio.h>

#include <conio.h>

void main() {

int num, i, flag = 0;

clrscr();
printf("Enter a number: ");

scanf("%d", &num);

if (num <= 1) {

printf("Not a prime number");

} else {

for (i = 2; i <= num / 2; i++) {

if (num % i == 0) {

flag = 1;

break;

if (flag == 0) {

printf("Prime number");

} else {

printf("Not a prime number");

getch();

Output Example:

Enter a number: 7

Prime number

10. Print Day Name (1-7)

#include <stdio.h>

#include <conio.h>

void main() {

int day;

clrscr();

printf("Enter a number (1-7): ");


scanf("%d", &day);

if (day == 1) {

printf("Sunday");

} else if (day == 2) {

printf("Monday");

} else if (day == 3) {

printf("Tuesday");

} else if (day == 4) {

printf("Wednesday");

} else if (day == 5) {

printf("Thursday");

} else if (day == 6) {

printf("Friday");

} else if (day == 7) {

printf("Saturday");

} else {

printf("Invalid Input");

getch();

Output Example:

Enter a number (1-7): 5

Thursday

11. Print Month Name (1-12)

#include <stdio.h>

#include <conio.h>

void main() {

int month;

clrscr();

printf("Enter a number (1-12): ");


scanf("%d", &month);

if (month == 1) {

printf("January");

} else if (month == 2) {

printf("February");

} else if (month == 3) {

printf("March");

} else if (month == 4) {

printf("April");

} else if (month == 5) {

printf("May");

} else if (month == 6) {

printf("June");

} else if (month == 7) {

printf("July");

} else if (month == 8) {

printf("August");

} else if (month == 9) {

printf("September");

} else if (month == 10) {

printf("October");

} else if (month == 11) {

printf("November");

} else if (month == 12) {

printf("December");

} else {

printf("Invalid Input");

getch();

Output Example:

Enter a number (1-12): 7


July

12. Display First 10 Natural Numbers

#include <stdio.h>

#include <conio.h>

void main() {

int i;

clrscr();

for (i = 1; i <= 10; i++) {

printf("%d ", i);

getch();

Output:

1 2 3 4 5 6 7 8 9 10

13. Compute Sum of First 10 Natural Numbers

#include <stdio.h>

#include <conio.h>

void main() {

int i, sum = 0;

clrscr();

for (i = 1; i <= 10; i++) {

sum += i;

printf("Sum = %d", sum);

getch();

Output:

Sum = 55
14. Print n Natural Numbers and Their Sum

#include <stdio.h>

#include <conio.h>

void main() {

int i, n, sum = 0;

clrscr();

printf("Enter n: ");

scanf("%d", &n);

for (i = 1; i <= n; i++) {

printf("%d ", i);

sum += i;

printf("\nSum = %d", sum);

getch();

Output Example:

Enter n: 5

12345

Sum = 15

15. Read 10 Numbers and Find Sum & Average

#include <stdio.h>

#include <conio.h>

void main() {

int i, num, sum = 0;

float avg;

clrscr();

printf("Enter 10 numbers: ");


for (i = 0; i < 10; i++) {

scanf("%d", &num);

sum += num;

avg = sum / 10.0;

printf("Sum = %d, Average = %.2f", sum, avg);

getch();

Output Example:

Enter 10 numbers: 5 10 15 20 25 30 35 40 45 50

Sum = 275, Average = 27.50

16. Cube of Numbers Up to n

#include <stdio.h>

#include <conio.h>

void main() {

int i, n;

clrscr();

printf("Enter n: ");

scanf("%d", &n);

for (i = 1; i <= n; i++) {

printf("Cube of %d = %d\n", i, i * i * i);

getch();

Output Example:

Enter n: 5

Cube of 1 = 1

Cube of 2 = 8

Cube of 3 = 27
Cube of 4 = 64

Cube of 5 = 125

17. Multiplication Table

#include <stdio.h>

#include <conio.h>

void main() {

int num, i;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

for (i = 1; i <= 10; i++) {

printf("%d x %d = %d\n", num, i, num * i);

getch();

Output Example:

Enter a number: 5

5x1=5

5 x 2 = 10

...

5 x 10 = 50

18. Check Prime Number

#include <stdio.h>

#include <conio.h>

void main() {

int num, i, flag = 0;

clrscr();

printf("Enter a number: ");


scanf("%d", &num);

if (num <= 1) {

printf("Not a prime number");

} else {

for (i = 2; i <= num / 2; i++) {

if (num % i == 0) {

flag = 1;

break;

if (flag == 0) {

printf("Prime number");

} else {

printf("Not a prime number");

getch();

Output Example:

Enter a number: 7

Prime number

19. Check Armstrong Number

#include <stdio.h>

#include <conio.h>

void main() {

int num, temp, remainder, sum = 0;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);
temp = num;

while (temp != 0) {

remainder = temp % 10;

sum += remainder * remainder * remainder;

temp /= 10;

if (sum == num) {

printf("Armstrong number");

} else {

printf("Not an Armstrong number");

getch();

Output Example:

Enter a number: 153

Armstrong number

20. Check Uppercase or Lowercase String

#include <stdio.h>

#include <conio.h>

#include <ctype.h>

void main() {

char str[100];

int i, upper = 0, lower = 0;

clrscr();

printf("Enter a string: ");

gets(str);

for (i = 0; str[i] != '\0'; i++) {

if (isupper(str[i])) {
upper++;

} else if (islower(str[i])) {

lower++;

if (upper > 0 && lower == 0) {

printf("Uppercase string");

} else if (lower > 0 && upper == 0) {

printf("Lowercase string");

} else {

printf("Mixed case string");

getch();

Output Example:

Enter a string: HELLO

Uppercase string

21. Area & Perimeter of a Circle

#include <stdio.h>

#include <conio.h>

void main() {

float radius, area, perimeter;

clrscr();

printf("Enter radius: ");

scanf("%f", &radius);

area = 3.14 * radius * radius;

perimeter = 2 * 3.14 * radius;

printf("Area = %.2f, Perimeter = %.2f", area, perimeter);


getch();

Output Example:

Enter radius: 5

Area = 78.50, Perimeter = 31.40

22. Sum of Two 3×3 Matrices

#include <stdio.h>

#include <conio.h>

void main() {

int a[3][3], b[3][3], sum[3][3], i, j;

clrscr();

printf("Enter first 3×3 matrix:\n");

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

scanf("%d", &a[i][j]);

printf("Enter second 3×3 matrix:\n");

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

scanf("%d", &b[i][j]);

printf("Sum of matrices:\n");

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

sum[i][j] = a[i][j] + b[i][j];

printf("%d ", sum[i][j]);

}
printf("\n");

getch();

Output Example:

Enter first 3×3 matrix:

123

456

789

Enter second 3×3 matrix:

987

654

321

Sum of matrices:

10 10 10

10 10 10

10 10 10

23. Convert Celsius to Fahrenheit

#include <stdio.h>

#include <conio.h>

void main() {

float celsius, fahrenheit;

clrscr();

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("Temperature in Fahrenheit = %.2f", fahrenheit);

getch();

}
Output Example:

Enter temperature in Celsius: 37

Temperature in Fahrenheit = 98.60

24. Calculate Factorial

#include <stdio.h>

#include <conio.h>

void main() {

int num, i, fact = 1;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

for (i = 1; i <= num; i++) {

fact *= i;

printf("Factorial = %d", fact);

getch();

Output Example:

Enter a number: 5

Factorial = 120

25. Check Palindrome Number

#include <stdio.h>

#include <conio.h>

void main() {

int num, temp, rev = 0, digit;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);
temp = num;

while (temp > 0) {

digit = temp % 10;

rev = rev * 10 + digit;

temp /= 10;

if (num == rev) {

printf("Palindrome number");

} else {

printf("Not a Palindrome");

getch();

Output Example:

Enter a number: 121

Palindrome number

26. Print Pattern 1

#include <stdio.h>

#include <conio.h>

void main() {

int i, j;

clrscr();

for (i = 1; i <= 5; i++) {

for (j = 1; j <= i; j++) {

printf("%d ", j);

printf("\n");

getch();
}

Output:

12

123

1234

12345

27. Print Pattern 2

#include <stdio.h>

#include <conio.h>

void main() {

int i, j;

clrscr();

for (i = 1; i <= 5; i++) {

for (j = 1; j <= i; j++) {

printf("%d ", i);

printf("\n");

getch();

Output:

22

333

4444

55555

28. Print Pattern 3

#include <stdio.h>

#include <conio.h>
void main() {

int i, j;

clrscr();

for (i = 1; i <= 5; i++) {

for (j = 1; j <= i; j++) {

printf("* ");

printf("\n");

getch();

Output:

**

***

****

*****

29. Count Digits in a Number

#include <stdio.h>

#include <conio.h>

void main() {

int num, count = 0;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

while (num > 0) {

num /= 10;

count++;

}
printf("Number of digits = %d", count);

getch();

Output Example:

Enter a number: 3456

Number of digits = 4

30. Sum of All Elements in an Array

#include <stdio.h>

#include <conio.h>

void main() {

int arr[10], i, sum = 0;

clrscr();

printf("Enter 10 numbers: ");

for (i = 0; i < 10; i++) {

scanf("%d", &arr[i]);

sum += arr[i];

printf("Sum = %d", sum);

getch();

Output Example:

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10

Sum = 55

31. Reverse Array Elements

#include <stdio.h>

#include <conio.h>

void main() {
int arr[10], i;

clrscr();

printf("Enter 10 numbers: ");

for (i = 0; i < 10; i++) {

scanf("%d", &arr[i]);

printf("Reversed Array: ");

for (i = 9; i >= 0; i--) {

printf("%d ", arr[i]);

getch();

Output Example:

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10

Reversed Array: 10 9 8 7 6 5 4 3 2 1

32. Print Even Numbers in an Array

#include <stdio.h>

#include <conio.h>

void main() {

int arr[10], i;

clrscr();

printf("Enter 10 numbers: ");

for (i = 0; i < 10; i++) {

scanf("%d", &arr[i]);

printf("Even numbers: ");

for (i = 0; i < 10; i++) {


if (arr[i] % 2 == 0) {

printf("%d ", arr[i]);

getch();

Output Example:

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10

Even numbers: 2 4 6 8 10

33. Sum of All Even Numbers in an Array

#include <stdio.h>

#include <conio.h>

void main() {

int arr[10], i, sum = 0;

clrscr();

printf("Enter 10 numbers: ");

for (i = 0; i < 10; i++) {

scanf("%d", &arr[i]);

if (arr[i] % 2 == 0) {

sum += arr[i];

printf("Sum of even numbers = %d", sum);

getch();

Output Example:

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10

Sum of even numbers = 30


34. Copy an Array into Another Array

#include <stdio.h>

#include <conio.h>

void main() {

int arr1[10], arr2[10], i;

clrscr();

printf("Enter 10 numbers: ");

for (i = 0; i < 10; i++) {

scanf("%d", &arr1[i]);

arr2[i] = arr1[i];

printf("Copied array: ");

for (i = 0; i < 10; i++) {

printf("%d ", arr2[i]);

getch();

Output Example:

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10

Copied array: 1 2 3 4 5 6 7 8 9 10

35. Copy an Array into Another Array in Reverse Order

#include <stdio.h>

#include <conio.h>

void main() {

int arr1[10], arr2[10], i, j;

clrscr();

printf("Enter 10 numbers: ");


for (i = 0; i < 10; i++) {

scanf("%d", &arr1[i]);

for (i = 0, j = 9; i < 10; i++, j--) {

arr2[j] = arr1[i];

printf("Reversed Copied Array: ");

for (i = 0; i < 10; i++) {

printf("%d ", arr2[i]);

getch();

Output Example:

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10

Reversed Copied Array: 10 9 8 7 6 5 4 3 2 1

You might also like