Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
return 0;
}
Output
Sum = 7
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
float fdiv_res=0.0;
clrscr();
scanf(%d, &num1);
scanf(%d, &num2);
add_res = num1+num2;
sub_res = num1num2;
mul_res = num1*num2;
idiv_res = num1/num2;
modiv_res = num1%num2;
fdiv_res = (float)num1/num2;
printf(\n %d + %d = %d, num1, num2, add_res);
return 0;
Output
10 + 5 = 15
10 5 = 5
10 5 = 50
10 / 5 = 2 (Integer Division)
10 % 5 = 0 (Modulo Division)
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
clrscr();
scanf(%c, &ch);
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
char ch;
clrscr();
scanf(%c, &ch);
Output
Enter any character in upper case : Z
The character in lower case is : z
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
scanf(%d,&num1);
scanf(%d,&num2);
temp = num1;
num1=num2;
num2=temp;
return 0;
}
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
scanf(%d,&num1);
scanf(%d,&num2);
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
float f_num;
int i_num;
clrscr();
scanf(%f, &f_num);
i_num = (int)f_num;
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
int num;
clrscr();
scanf(%d,&num);
if(num>0)
else
if(num<0)
else
return 0;
Output
-5 is negative
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
int year;
clrscr();
scanf(%d, &year);
if(year%4 == 0)
else
return 0;
Output
Leap Year
********************************************************************
*****
12. Write a program to find the greatest number from three numbers.
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
scanf(%d, &num1);
scanf(%d, &num2);
scanf(%d, &num3);
if(num1>num2)
{
if(num1>num3)
else
else if(num2>num3)
else
return 0;
Output
********************************************************************
*****
13. Write a program to input three numbers and then find largest of
them using && operator.
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
scanf(%d, &num1);
scanf(%d, &num2);
scanf(%d, &num3);
else
getch();
return 0;
Output
********************************************************************
*****
14. Write a program to find largest of two numbers using ternary
operator.
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
scanf(%d, &num1);
scanf(%d, &num2);
large = num1>num2?num1:num2;
return 0;
Output
********************************************************************
*****
#include<conio.h>
int main()
clrscr();
scanf(%d, &num1);
scanf(%d, &num2);
scanf(%d, &num3);
large = num1>num2?(num1>num3?num1:num3):(num2>num3?num2:num3);
return 0;
Output
********************************************************************
*****
16. Write a program to take input from the user and then check
whether it is a number or a character. If it is a character,
determine whether it is in upper case or lower case.
#include <stdio.h>
#include <conio.h>
int main()
char ch;
clrscr();
scanf(%c, &ch);
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
char ch;
clrscr();
scanf(%c, &ch);
else
return 0;
Output
********************************************************************
*****
#include <conio.h>
clrscr();
scanf(%d, &marks1);
scanf(%d, &marks2);
scanf(%d, &marks3);
scanf(%d, &marks4);
avg = total/4;
printf(\n DISTINCTION);
else
printf(\n FAIL);
return 0;
Output
Total = 350
Aggregate = 87.5
DISTINCTION
********************************************************************
*****
19. Write a program to enter a number from 1-7 and display the
corresponding day of the week using switch case statement.
#include <stdio.h>
#include <conio.h>
int main()
int day;
clrscr();
scanf(%d, &day);
switch(day)
break;
break;
break;
break;
break;
break;
return 0;
Output
THURSDAY
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
{
int i=0, large = -1, num;
clrscr();
while(i<10)
scanf(%d,&num);
i++;
return 0;
Output
********************************************************************
*****
21. Write a program to read the numbers until -1 is encountered.
Also count the negative, positive and zeroes entered by the user.
#include <stdio.h>
#include <conio.h>
int main()
int num;
clrscr();
scanf(%d, &num);
while(num != -1)
if(num>0)
positives++;
else if(num<0)
negatives++;
else
zeroes++;
scanf(%d, &num);
getch();
return 0;
Output
Enter -1 to exit.
********************************************************************
*****
22. Write a program using do-while loop to display the square and
cube of first n natural numbers.
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
int i, n;
clrscr();
scanf(%d, &n);
printf(\n
-----------------------------------------------------------);
i=1;
do
printf(\n | \t %d \t | \t %d \t | \t %ld \t |, i,
pow(i,2), pow(i,3));
i++;
} while(i<=n);
printf(\n
-----------------------------------------------------------);
return 0;
Output
-----------------------------------------------------------
| 1 | 1| 1 |
| 2 | 4| 8 |
-----------------------------------------------------------
********************************************************************
*****
23. Write a program to list all the leap years from 1900 to 2100.
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
do
if(m%4 == 0)
else
m = m+1;
}while(m<=n);
return 0;
Output
********************************************************************
*****
#include <conio.h>
int main()
char ch;
clrscr();
scanf(%c, &ch);
do
uppers++;
lowers++;
numbers++;
scanf(%c, &ch);
} while(ch != *);
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
int num;
clrscr();
do
if(num>0)
sum_positives += num;
positives++;
else if(num<0)
sum_negatives += num;
negatives++;
scanf(%d,&num);
} while(num != -1);
mean_positives = sum_positives/positives;
mean_negatives = mean_neagtives/negatives;
return 0;
Output
Enter -1 to exit.
********************************************************************
*****
26. Write a program using for loop to print all the numbers from m
to n and classifying them as even or odd.
#include <stdio.h>
#include <conio.h>
int main()
int i, m, n;
clrscr();
scanf(%d, &m);
for(i=m;i<=n;i++)
if(i%2 == 0)
else
return 0;
Output
3 is odd
4 is even
5 is odd
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
printf(\n Enter the number : );
scanf(%d, &num);
if(num == 0)
fact = 1;
else
return 0;
Output
Factorial of 5 is 120
********************************************************************
*****
28. Write a program using while loop to read the numbers until -1 is
encountered. Also classify the entered number as positive, negative
or zero.
#include <stdio.h>
#include <conio.h>
int main()
int num;
clrscr();
printf(\n Enter -1 to exit.);
scanf(%d,&num);
while(num != -1)
if(num>0)
printf(\n %d is POSITIVE,num);
else if(num<0)
else
scanf(%d, &num);
return 0;
Output
Enter -1 to exit.
98 is POSITIVE
********************************************************************
*****
29. Write a program using do-while loop to read the numbers until -1
is encountered. Also count the number of prime numbers and composite
numbers entered by the user.
#include <stdio.h>
#include <conio.h>
int main()
int num, i;
clrscr();
scanf(%d,&num);
do
for(i=2;i<=num%2;i++)
if(num%i==0)
flag=1;
break;
if(flag==0)
primes++;
else
composites++:
flag=0;
scanf(%d, &num);
} while(num != -1);
return 0;
Output
Enter -1 to exit.
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
int i, num, n;
clrscr();
printf(\n Enter the number : );
scanf(%d, &num);
scanf(%d, &n);
for(i=1;i<=n;i++)
return 0;
Output
pow(2, 5) = 32
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
printf(\n Enter the number : );
scanf(%d, &num);
while(num != 0)
temp = num%10;
printf(%d,temp);
num = num/10;
return 0;
Output
********************************************************************
*****
32. Write a program to enter a number and then calculate the sum of
its digits.
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
scanf(%d, &num);
while(num != 0)
temp = num%10;
sumofdigits += temp;
num = num/10;
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
clrscr();
while(decimal_num != 0)
remainder = decimal_num%2;
binary_num += remainder*pow(10,i);
decimal_num = decimal_num/2;
i++;
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
clrscr();
printf(\n Enter the decimal number : );
scanf(%d, &decimal_num);
while(decimal_num != 0)
remainder = decimal_num%8;
octal_num += remainder*pow(10,i);
decimal_num = decimal_num/8;
i++;
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num, hex_num =0 , i =0, remainder;
clrscr();
scanf(%d, &decimal_num);
while(decimal_num != 0)
remainder = decimal_num%16;
hex_num += remainder*pow(10,i);
decimal_num = decimal_num/16;
i++;
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
clrscr();
scanf(%d, &binary_num);
while(binary_num != 0)
remainder = binary_num%10;
decimal_num += remainder*pow(2,i);
binary_num = binary_num/10;
i++;
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
clrscr();
scanf(%d, &octal_num);
while(octal_num != 0)
remainder = octal_num%10;
decimal_num += remainder*pow(8,i);
octal_num = octal_num/10;
i++;
return 0;
Output
********************************************************************
*****
#include <conio.h>
#include <math.h>
int main()
clrscr();
scanf(%d, &hex_num);
while(hex_num != 0)
remainder = hex_num%10;
decimal_num += remainder*pow(16,i);
hex_num = hex_num/10;
i++;
return 0;
Output
********************************************************************
*****
#include <conio.h>
int main()
clrscr();
scanf(%d, &num1);
scanf(%d, &num2);
if(num1>num2)
dividend = num1;
divisor = num2;
else
dividend = num2;
divisor = num1;
while(divisor)
remainder = dividend%divisor;
dividend = divisor;
divisor = remainder;
return 0;
Output
GCD of 4 and 8 is = 4
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
int n;
float sum=0.0, a, i;
clrscr();
scanf(%d, &n);
for(i=1.0;i<=n;i++)
{ a=1/i;
return 0;
Output
********************************************************************
*****
#include <stdio.h>
#include <math.h>
#include <conio.h>
int main()
int n;
float sum=0.0, a, i;
clrscr();
scanf(%d, &n);
for(i=1.0;i<=n;i++)
{ a=1/pow(i,2);
}
printf(\n The sum of series 1/12 + 1/ 22 + . 1/%d2 = %f, n,
sum);
return 0;
Output
********************************************************************
*****
42. Write a program to calculate the sum of the series 1/2 + 2/3 +
. n/(n+1).
#include <stdio.h>
#include <conio.h>
int main()
int n;
float sum=0.0, a, i;
clrscr();
scanf(%d, &n);
for(i=1.0;i<=n;i++)
{ a= i/(i+1);
return 0;
}
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
int i, n;
clrscr();
scanf(%d, &n);
for(i=1;i<=n;i++)
term = pow(i,3);
sum += term;
return 0;
Output
Enter the value of n : 4
13 + 23 + 33 + .... = 100
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
int i, n;
clrscr();
scanf(%d, &n);
for(i=1;i<=n;i++)
if(i%2 == 0)
term = pow(i,2);
sum += term;
return 0;
}
Output
22 + 42 + 62 + .... = 56
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
clrscr();
scanf(%d, &num);
n=num;
while(n>0)
r=n%10;
sum += pow(r,3);
n=n/10;
if(sum==num)
else
Output
********************************************************************
*****
#include <stdio.h>
#include <conio.h>
int main()
int i, j;
clrscr();
printf(\n
*******************************************************);
for(i=1;i<=5;i++)
for(j=1;j<=10;j++)
printf(\n);
getch();
return 0;
}
Output
Multiplication table
*******************************************************
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
********************************************************************
*****
#include <stdio.h>
int main()
scanf(%d, &num1);
scanf(%d, &num2);
return 0;
{
int result;
result = a + b;
return result;
Output
Total = 15
********************************************************************
*****
#include <stdio.h>
int main()
scanf(%d, &num1);
scanf(%d, &num2);
scanf(%d, &num3);
return 0;
return a;
return b;
else
return c;
Output
Largest number = 15
********************************************************************
*****
#include <stdio.h>
int main()
{
float area, radius;
scanf(%f, &radius);
area = cal_area(radius);
return 0;
Output
********************************************************************
*****
#include <stdio.h>
int main()
return 0;
if( y==0)
return 1;
else
Output
Pow(2, 3) = 8
********************************************************************
*****
51. Write a program to print a character. Also print its ASCII value
and rewrite the character in upper case.
#include <stdio.h>
int main()
pch = &ch;
scanf(%c, &ch);
printf(\n The character that was entered is : %c, *pch);
return 0;
Output
********************************************************************
*****
#include <stdio.h>
int main()
pfnum = &fnum;
pnum= #
scanf(%f, &fnum);
*pnum = (int)*pfnum;
Output
********************************************************************
*****
#include <stdio.h>
int main()
pch = &ch;
scanf(%c, pch);
else
return 0;
}
Output
s is not a vowel
********************************************************************
*****
#include <stdio.h>
int main()
pn = &n;
psum = ∑
pmean = &mean;
scanf(%d, pn);
}
for(i=0; i < *pn; i++)
return 0;
Output
********************************************************************
*****
#include <stdio.h>
int main()
pstr = str;
pcopy_str = copy_str;
gets(str);
if( *pstr = ;)
{ } // do nothing
if ( *pstr == ,)
*pcopy_str = .;
else
*pcopy_str = *pstr;
pstr++; pcopy_str++;
*pcopy_str = \0;
pcopy_str = copy_str;
printf(%c, *pcopy_str);
pcopy_str++;
}
return 0;
Output
********************************************************************
*****
#include <stdio.h>
int main()
scanf(%d, &n);
read_array(num, n);
return 0;
int i;
for(i=0;i<n;i++)
scanf(%d, arr+i);
for(i=0;i<n;i++)
*small = *(arr+i);
*pos = i;
Output
array[0] = 2
array[1] = 1
array[2] = 3
array[3] = 5
array[4] = 4
#include <stdio.h>
int main()
int num[10], n;
scanf(%d, &n);
read_array(num, n);
sort_array(num, n);
display_array(num, n);
return 0;
int i;
for(i=0;i<n;i++)
scanf(%d, arr+i);
}
}
int i;
for(i=0;i<n;i++)
int i, j, temp;
for(i=0;i<n;i++)
for(j= i+1;j<n;j++)
temp = *(arr+i);
*(arr+i) = *(arr+j);
*(arr+j) = temp;
Output
array[0] = 2
array[1] = 1
array[2] = 3
array[3] = 5
array[4] = 4
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
********************************************************************
*****
#include <stdio.h>
int main()
printf(\n 9. EXIT);
scanf(%d, &option);
switch(option)
case 1:
read_str(str1);
break;
case 2:
display_str(str1);
break;
case 3:
read_str(str2);
case 4:
scanf(%d, &m);
scanf(%d, &n);
break;
case 5:
cal_len(str1, &length);
break;
case 6:
count(str1);
break;
case 7:
count_wlc(str1);
break;
case 8:
replace_str(str1);
break;
return 0;
while(*my_str!= *)
scanf(%c, my_str);
my_str++;
*my_str = \0;
while(*my_str != \0)
printf(%c, *my_str);
my_str++;
while(*my_str1 != \0)
*my_str3 = *my_str1;
my_str1++, my_str3++;
}
while(*my_str2 != \0)
*my_str3 = *my_str2;
my_str3++, my_str2++;
*my_str3 = \0;
display_str(my_str3);
int i =0;
while (i<=n)
*copy_str = *my_str;
copy_str++, my_str++;
*copy_str[j] = \0;
display_str(copy_str);
while(*my_str != \0)
*len++;
}
void count(char my_str[])
upper_case++;
lower_case++;
numbers++;
else
spcl_car++;
while(*my_str != \0)
if (*my_str == \n)
lines++;
if (*my_str == && *(my_str+1) != )
words++;
characters++;
my_str++;
if(*my_str == ,)
*my_str = ;;
my_str++;
display_str(my_str);
Output
6. Count the number of upper case, lower case, numbers and special
characters
9. EXIT
6. Count the number of upper case, lower case, numbers and special
characters
8. Replace , with ;
9. EXIT
6. Count the number of upper case, lower case, numbers and special
characters
8. Replace , with ;
9. EXIT
#include <stdio.h>
#include <string.h>
int main()
int i=0, n;
pstr = str;
pcopy_str = copy_str;
gets(str);
scanf(%d, &n);
while(*pstr != \0)
pstr++; pcopy_str++;
*pcopy_str = \0;
puts(copy_str);
return 0;
Output
********************************************************************
*****
60. Write a program to enter two points and then calculate the
distance between them.
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
typedef struct
int x, y;
}point;
float distance;
clrscr();
getch();
return 0;
Output