Documentos de Académico
Documentos de Profesional
Documentos de Cultura
/*
* 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. }
$ 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
$ 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
$ 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
$ 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
$ 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
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
13.
14.
15.
16.
17.
18.
19.
20.
21. }
$ cc pgm42.c
$ a.out
Enter the number
567865
Given number
= 567865
Its reverse is = 568765
$ 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. }
$ 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
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. }
$ 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
46.
47.
48.
49.
50.
scanf("%d-%d-%d", &item[i].mfg.day,
&item[i].mfg.month, &item[i].mfg.year);
}
printf("
*****
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
------------------------------------------------------------------
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