Anda di halaman 1dari 14

1.

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


1. /*
2. * C program to check whether a given integer is odd or even
3. */
4. #include <stdio.h>
5.
6. void main()
7. {
8.
int ival, remainder;
9.
10.
printf("Enter an integer : ");
11.
scanf("%d", &ival);
12.
remainder = ival % 2;
13.
if (remainder == 0)
14.
printf("%d is an even integer\n", ival);
15.
else
16.
printf("%d is an odd integer\n", ival);
17. }

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


1. /*
2. * C program to check whether a given integer is positive
3. * or negative
4. */
5. #include <stdio.h>
6.
7. void main()
8. {
9.
int number;
10.
11.
printf("Enter a number \n");
12.
scanf("%d", &number);
13.
if (number >= 0)
14.
printf("%d is a positive number \n", number);
15.
else
16.
printf("%d is a negative number \n", number);
17. }

4. C Program to Find the Biggest of 3 Numbers


his C Program calculates the biggest of 3 numbers.The program assumes 3 numbers as a, b, c.
First it compares any 2 numbers check which is bigger. After that it compares the biggest
element with the remaining number. Now the number which is greater becomes your biggest
of 3 numbers.
Here is source code of the C program to calculate the biggest of 3 numbers. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1.
2.
3.
4.
5.
6.
7.
8.
9.

/*
* C program to find the biggest of three numbers
*/
#include <stdio.h>
void main()
{
int num1, num2, num3;

10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28. }

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");

$ cc pgm6.c
$ a.out
Enter the values of num1, num2 and num3
6 8 10
num1 = 6 num2 = 8 num3 = 10
num3 is the greatest among three

5. C Program to Calculate the Sum of Odd & Even Numbers


This C Program calculates the sum of odd & even numbers. The program first seperates odd
and even numbers. Later it adds the odd and even numbers seperately.
Here is source code of the C program to calculate the sum of odd & even numbers. The C
program is successfully compiled and run on a Linux system. The program output is also
shown below.
1. /*
2. * C program to find the sum of odd and even numbers from 1 to N
3. */
4. #include <stdio.h>
5.
6. void main()
7. {
8.
int i, num, odd_sum = 0, even_sum = 0;
9.
10.
printf("Enter the value of num\n");
11.
scanf("%d", &num);
12.
for (i = 1; i <= num; i++)
13.
{
14.
if (i % 2 == 0)
15.
even_sum = even_sum + i;
16.
else
17.
odd_sum = odd_sum + i;
18.
}
19.
printf("Sum of all odd numbers = %d\n", odd_sum);
20.
printf("Sum of all even numbers = %d\n", even_sum);
21. }

$ cc pgm12.c
$ a.out
Enter the value of num
10
Sum of all odd numbers = 25
Sum of all even numbers = 30
$ a.out
Enter the value of num
100
Sum of all odd numbers = 2500
Sum of all even numbers = 2550

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


This C Program reverses a number & checks if it is a palindrome or not. First it reverses a
number. Then it checks if given number and reversed numbers are equal. If they are equal,
then its a palindrome.
Here is source code of the C program to reverse a number & checks it is a palindrome or not.
The C program is successfully compiled and run on a Linux system. The program output is
also shown below.
1. /*
2. * C program to reverse a given integer number and check
3. * whether it is a palindrome. Display the given number
4. * with appropriate message
5. */
6. #include <stdio.h>
7.
8. void main()
9. {
10.
int num, temp, remainder, reverse = 0;
11.
12.
printf("Enter an integer \n");
13.
scanf("%d", &num);
14.
/* original number is stored at temp */
15.
temp = num;
16.
while (num > 0)
17.
{
18.
remainder = num % 10;
19.
reverse = reverse * 10 + remainder;
20.
num /= 10;
21.
}
22.
printf("Given number is = %d\n", temp);
23.
printf("Its reverse is = %d\n", reverse);
24.
if (temp == reverse)
25.
printf("Number is a palindrome \n");
26.
else
27.
printf("Number is not a palindrome \n");
28. }

$ cc pgm13.c
$ a.out
Enter an integer

6789
Given number is = 6789
Its reverse is = 9876
Number is not a palindrome
$ a.out
Enter an integer
58085
Given number is = 58085
Its reverse is = 58085
Number i

7. C Program to Find the Number of Integers Divisible by 5


This C Program calculates the number of integers divisible by 5. This program checks if the
given number is divisible by 5 and then prints an appropriate message.
Here is source code of the C program to calculate the number of integers divisible by 5. The
C program is successfully compiled and run on a Linux system. The program output is also
shown below.
1. /*
2. * C program to find the number of integers divisible by
3. * 5 between the given range num1 and num2, where num1 < num2.
4. *
5. * Also find the sum of all these integer numbers which are divisible
6. * by 5 and display the total.
7. */
8. #include <stdio.h>
9.
10. void main()
11. {
12.
int i, num1, num2, count = 0, sum = 0;
13.
14.
printf("Enter the value of num1 and num2 \n");
15.
scanf("%d %d", &num1, &num2);
16.
/* Count the number and compute their sum*/
17.
printf("Integers divisible by 5 are \n");
18.
for (i = num1; i < num2; i++)
19.
{
20.
if (i % 5 == 0)
21.
{
22.
printf("%3d,", i);
23.
count++;
24.
sum = sum + i;
25.
}
26.
}
27.
printf("\n Number of integers divisible by 5 between %d and %d
=
28.
%d\n", num1, num2, count);
29.
printf("Sum of all integers that are divisible by 5 = %d\n",
sum);
30. }

$ cc pgm18.c
$ a.out
Enter the value of num1 and num2

12 17
Integers divisible by 5 are
15,
Number of integers divisible by 5 between 12 and 17 = 1
Sum of all integers that are divisible by 5 = 15

8. C Program to Read Two Integers M and N & Swap their Values


This C Program reads two integers & swap their values.
Here is source code of the C program to read two integers & swap their values. The C
program is successfully compiled and run on a Linux system. The program output is also
shown below.
1. /*
2. * C program to read two integers M and N and to swap their values.
3. * Use a user-defined function for swapping. Output the values of M
4. * and N before and after swapping.
5. */
6. #include <stdio.h>
7. void swap(float *ptr1, float *ptr2);
8.
9. void main()
10. {
11.
float m, n;
12.
13.
printf("Enter the values of M and N \n");
14.
scanf("%f %f", &m, &n);
15.
printf("Before Swapping:M = %5.2ftN = %5.2f\n", m, n);
16.
swap(&m, &n);
17.
printf("After Swapping:M = %5.2ftN = %5.2f\n", m, n);
18. }
19. /* Function swap - to interchanges the contents of two items */
20. void swap(float *ptr1, float *ptr2)
21. {
22.
float temp;
23.
24.
temp = *ptr1;
25.
*ptr1 = *ptr2;
26.
*ptr2 = temp;
27. }

$ cc pgm36.c
$ a.out
Enter the values of M and N
2 3
Before Swapping:M = 2.00
After Swapping:M = 3.00

N =
N =

3.00
2.00

9. C Program to Convert the given Binary Number into Decimal

This C Program converts the given binary number into decimal. The program reads the
binary number, does a modulo operation to get the remainder, multiples the total by base 2
and adds the modulo and repeats the steps.
Here is source code of the C program to covert binary number to decimal. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C program to convert the given binary number into decimal
3. */
4. #include <stdio.h>
5.
6. void main()
7. {
8.
int num, binary_val, decimal_val = 0, base = 1, rem;
9.
10.
printf("Enter a binary number(1s and 0s) \n");
11.
scanf("%d", &num); /* maximum five digits */
12.
binary_val = num;
13.
while (num > 0)
14.
{
15.
rem = num % 10;
16.
decimal_val = decimal_val + rem * base;
17.
num = num / 10 ;
18.
base = base * 2;
19.
}
20.
printf("The Binary number is = %d \n", binary_val);
21.
printf("Its decimal equivalent is = %d \n", decimal_val);
22. }

$ cc pgm38.c
$ a.out
Enter a binary number(1s and 0s)
10101001
The Binary number is = 10101001
Its decimal equivalent is = 169S

10. C Program to Reverse a Given Number


This C Program reverses a given number by using modulo operation.
Here is source code of the C program to reverse a given number. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C program to accept an integer and reverse it
3. */
4. #include <stdio.h>
5.
6. void main()
7. {
8.
long num, reverse = 0, temp, remainder;
9.
10.
printf("Enter the number\n");
11.
scanf("%ld", &num);
12.
temp = num;

13.
14.
15.
16.
17.
18.
19.
20.
21. }

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);

$ cc pgm42.c
$ a.out
Enter the number
567865
Given number
= 567865
Its reverse is = 568765

11. C Program to Illustrate how User Authentication is Done


This C Program illustrate how user authentication is done. The program accepts the username
and password. It checks whether the password is correct with respect to the username.
Here is source code of the C program to illustrate user authentication. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C program is to illustrate how user authentication is done.
3. * Program asks for the user name and password and displays
4. * the password as '*' character
5. */
6. #include <stdio.h>
7.
8. void main()
9. {
10.
char password[10], username[10], ch;
11.
int i;
12.
13.
printf("Enter User name: ");
14.
gets(username);
15.
printf("Enter the password < any 8 characters>: ");
16.
for (i = 0; i < 8; i++)
17.
{
18.
ch = getchar();
19.
password[i] = ch;
20.
ch = '*' ;
21.
printf("%c", ch);
22.
}
23.
password[i] = '\0';
24.
/* Original password can be printed, if needed */
25.
printf("\n Your password is :");
26.
for (i = 0; i < 8; i++)
27.
{
28.
printf("%c", password[i]);
29.
}
30. }

$ cc pgm43.c
$ a.out
Enter User name: rajaraman
Enter the password <any 8 characters>: shashi12
********
Your password is :shashi12

12. C Program to Convert a Decimal Number to Binary & Count the Number of 1s
This C Program converts a decimal number into binary & count the number of 1s. The
program uses module operation and multiplication with base 2 operation for conversion. It
also uses modulo operation to check for 1s and accordingly increments the count of 1s.
Here is source code of the C program to convert a decimal number to binary & count the
number of 1s. The C program is successfully compiled and run on a Linux system. The
program output is also shown below.
1.
2.
3.
4.
5.
6.
7.
8.
9.

/*
* C program to accept a decimal number and convert it to binary
* and count the number of 1's in the binary number
*/
#include <stdio.h>
void main()
{
long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s
= 0;

10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29. }

printf("Enter a decimal integer \n");


scanf("%ld", &num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;
/* To count no.of 1s */
if (remainder == 1)
{
no_of_1s++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Its binary equivalent is = %ld\n", binary);
printf("No.of 1's in the binary number is = %d\n", no_of_1s);

$ cc pgm46.c
$ a.out
Enter a decimal integer
134
Input number is = 134
Its binary equivalent is = 10000110
No.of 1's in the binary number is = 3

13. C Program to Find if a given Year is a Leap Year

This C Program checks whether a given year is a leap year. When A year is divided by 4. If
remainder becomes 0 then the year is called a leap year.
Here is source code of the C program to check a given year is leap year. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C program to find whether a given year is leap year or not
3. */
4. void main()
5. {
6.
int year;
7.
8.
printf("Enter a year \n");
9.
scanf("%d", &year);
10.
if ((year % 400) == 0)
11.
printf("%d is a leap year \n", year);
12.
else if ((year % 100) == 0)
13.
printf("%d is a not leap year \n", year);
14.
else if ((year % 4) == 0)
15.
printf("%d is a leap year \n", year);
16.
else
17.
printf("%d is not a leap year \n", year);
18. }

$ cc pgm47.c
$ a.out
Enter a year
2012
2012 is a leap year
$ a.out
Enter a year
2009
2009 is not a leap year

14. C Program to Swap the Contents of two Numbers using Bitwise XOR Operation
This C Program swaps the contents of two numbers using bitwise XOR operation.
Here is source code of the C program to swap the contents of two numbers using bitwise
XOR operation. The C program is successfully compiled and run on a Linux system. The
program output is also shown below.
1. /*
2. * C program to swap the contents of two numbers using bitwise XOR
3. * operation. Don't use either the temporary variable or arithmetic
4. * operators
5. */
6. #include <stdio.h>
7.
8. void main()
9. {
10.
long i, k;
11.
12.
printf("Enter two integers \n");

13.
14.
15.
16.
17.
18.
19. }

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);

$ cc pgm48.c
$ a.out
Enter two integers
45
89
Before swapping i= 45 and k = 89
After swapping i= 89 and k = 45

15. C Program to Convert a Given Number of Days in terms of Years, Weeks & Days
This C Program converts a given number of Days in terms of years, weeks & days. This
program accepts the number of days. Given the number of days, then it calculates the years,
weeks & days for this number.
Here is source code of the C program to converts a given number of Days in terms of years,
weeks & days. The C program is successfully compiled and run on a Linux system. The
program output is also shown below.
1. /*
2. * C program to convert given number of days to a measure of time
given
3. * in years, weeks and days. For example 375 days is equal to 1 year
4. * 1 week and 3 days (ignore leap year)
5. */
6. #include <stdio.h>
7. #define DAYSINWEEK 7
8.
9. void main()
10. {
11.
int ndays, year, week, days;
12.
13.
printf("Enter the number of daysn");
14.
scanf("%d", &ndays);
15.
year = ndays / 365;
16.
week =(ndays % 365) / DAYSINWEEK;
17.
days =(ndays % 365) % DAYSINWEEK;
18.
printf ("%d is equivalent to %d years, %d weeks and %d daysn",
19.
ndays, year, week, days);
20. }

$ cc pgm49.c
$ a.out
Enter the number of days
29
29 is equivalent to 0 years, 4 weeks and 1 days

$ a.out
Enter the number of days
1000
1000 is equivalent to 2 years, 38 weeks and 4 days

16. C Program to Display the Inventory of Items in a Store


This C Program display the inventory of items in a store. The program accepts the value of
item name, item code, price, quantity & manufacture date. Then display those value in a
structured way.
Here is source code of the C program to display the inventory of items in a storage. The C
program is successfully compiled and run on a Linux system. The program output is also
shown below.
1. /*
2. * C program to display the inventory of items in a store / shop
3. * The inventory maintains details such as name, price, quantity
4. * and manufacturing date of each item.
5. */
6. #include <stdio.h>
7.
8. void main()
9. {
10.
struct date
11.
{
12.
int day;
13.
int month;
14.
int year;
15.
};
16.
struct details
17.
{
18.
char name[20];
19.
int price;
20.
int code;
21.
int qty;
22.
struct date mfg;
23.
};
24.
struct details item[50];
25.
int n, i;
26.
27.
printf("Enter number of items:");
28.
scanf("%d", &n);
29.
fflush(stdin);
30.
for (i = 0; i < n; i++)
31.
{
32.
fflush(stdin);
33.
printf("Item name: \n");
34.
scanf("%s", item[i].name);
35.
fflush(stdin);
36.
printf("Item code: \n");
37.
scanf("%d", &item[i].code);
38.
fflush(stdin);
39.
printf("Quantity: \n");
40.
scanf("%d", &item[i].qty);
41.
fflush(stdin);
42.
printf("price: \n");
43.
scanf("%d", &item[i].price);
44.
fflush(stdin);
45.
printf("Manufacturing date(dd-mm-yyyy): \n");

46.
47.
48.
49.
50.

scanf("%d-%d-%d", &item[i].mfg.day,
&item[i].mfg.month, &item[i].mfg.year);
}
printf("

*****

INVENTORY ***** \n");

printf("-----------------------------------------------------------------\n");
printf("S.N.|
NAME
|
CODE
| QUANTITY | PRICE
| MFG.DATE \n");

51.
52.
53.
54.

printf("-----------------------------------------------------------------\n");
for (i = 0; i < n; i++)
printf("%d
%-15s
%-d
%-5d
%-5d
%d/%d/%d \n", i + 1, item[i].name, item[i].code,
item[i].qty,
59.
item[i].price, item[i].mfg.day, item[i].mfg.month,
60.
item[i].mfg.year);
61.
printf("--------------------------------------------------------62.
---------\n");
63. }
55.
56.
57.
58.

$ cc pgm60.c
$ a.out
Enter number of items:3
Item name:
pendrive
Item code:
123
Quantity:
6
price:
3000
Manufacturing date(dd-mm-yyyy):
30-9-2012
Item name:
computer
Item code:
124
Quantity:
10
price:
10000
Manufacturing date(dd-mm-yyyy):
30-7-2012
Item name:
optical mouse
Item code:
Quantity:
price:
Manufacturing date(dd-mm-yyyy):
***** INVENTORY *****
-----------------------------------------------------------------S.N.|
NAME
|
CODE
| QUANTITY | PRICE | MFG.DATE
-----------------------------------------------------------------1
pendrive
123
6
3000
30/9/2012
2
computer
124
10
10000
30/7/2012

3
optical
0
0
0
0/0/0
-----------------------------------------------------------------$ a.out
Enter number of items:3
Item name:
pendrive
Item code:
123
Quantity:
6
price:
3000
Manufacturing date(dd-mm-yyyy):
30-9-2012
Item name:
computer
Item code:
124
Quantity:
10
price:
10000
Manufacturing date(dd-mm-yyyy):
30-7-2012
Item name:
Mouse
Item code:
125
Quantity:
10
price:
1500
Manufacturing date(dd-mm-yyyy):
30-6-2012
***** INVENTORY *****
-----------------------------------------------------------------S.N.|
NAME
|
CODE
| QUANTITY | PRICE
| MFG.DATE
-----------------------------------------------------------------1
pendrive
123
6
3000
30/9/2012
2
computer
124
10
10000
30/7/2012
3
Mouse
125
10
1500
30/6/2012
------------------------------------------------------------------

17. C Program to Multiply given Number by 4 using Bitwise Operators


This C Program multiplies given number by 4 using bitwise operators. The bitwise operators
are or, and, xor, not, left shift, right shift. Program uses left shift operator for this.
Here is source code of the C program to multiply given number by 4 using bitwise operators.
The C program is successfully compiled and run on a Linux system. The program output is
also shown below.
1. /*
2. * C program to multiply given number by 4 using bitwise operators
3. */
4. #include <stdio.h>

5.
6. void main()
7. {
8.
long number, tempnum;
9.
10.
printf("Enter an integer \n");
11.
scanf("%ld", &number);
12.
tempnum = number;
13.
/* left shift by two bits */
14.
number = number << 2;
15.
printf("%ld x 4 = %ld\n", tempnum, number);
16. }

$ cc pgm62.c
$ a.out
Enter an integer
450
450 x 4 = 1800

18. C Program to Find the Sum of first 50 Natural Numbers using For Loop
This C Program finds sum of first 50 natural numbers using for loop.
Here is source code of the C program to find the sum of first 50 natural numbers using for
loop. The C program is successfully compiled and run on a Linux system. The program
output is also shown below.
1. /*
2. * C program to find the sum of first 50 natural numbers
3. * using for loop
4. */
5. #include <stdio.h>
6.
7. void main()
8. {
9.
int num, sum = 0;
10.
11.
for (num = 1; num <= 50; num++)
12.
{
13.
sum = sum + num;
14.
}
15.
printf("Sum = %4d\n", sum);
16. }

$ cc pgm73.c
$ a.out
Sum = 1275

Anda mungkin juga menyukai