Unit1 SEE QP Programs
Unit1 SEE QP Programs
Unit 1: Introduction to C
#include<stdio.h>
void main()
{
int a, b, c, larg;
printf("Enter three integer numbers: ");
scanf("%d%d%d", &a,&b,&c);
larg = (a>b)?((a>c)?a:c):((b>c)?b:c);
printf("Largest number is: %d", larg);
}
#include<stdio.h>
int main()
{
float fahrenheit, celsius;
printf("Enter Fahrenheit: ");
scanf("%f",&fahrenheit);
celsius = (fahrenheit - 32)*5/9;
printf("Celsius: %f ", celsius);
return 0;
}
#include<stdio.h>
int …main..( ){
char a = ‘A’;
…int..x=30, y=20, result;
…result...=x * y + 2;
printf(“Character = ..%c..”,a);
printf (“Result = %d”,..result..);
…return.. 0;
}
5. Develop a C program to calculate the bill amount for an item, given its quantity sold,
value, discount and tax. 8M
#include <stdio.h>
int main()
{
float total_amt,amt, sub_total,discount_amt, tax_amt,qty,val,discount,tax;
printf ("\n Enter the quantity of item sold: ");
scanf("%f",&qty);
printf("\n Enter the value of item: ");
scanf("%f", & val);
printf("\n Enter the discount percentage: ");
scanf("%f", &discount);
printf("\n Enter the tax: ");
scanf("%f", &tax);
printf("\n\n\n ************BILL***********");
printf("\n Quantity Sold: %f", qty);
printf("\n Price per item: %f",val);
printf("\n ----------------");
printf("\n Amount:%f",discount_amt);
printf("\n Discount:%f",discount_amt);
printf("\n Discounted total: %f", sub_total);
printf("\n tax: + %f", tax_amt);
printf("\n--------------------");
printf("\n Total amount %f", total_amt);
return 0;
}
6. Analyze the code below and predict the technique used for variable mapping. Also,
write the output of the code. 6M
Solution: The technique used for variable mapping in code (i) is type casting and in
code (ii) is type conversion. Output of code (i) is sum=2 , Output of code (ii) is 10 10
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,s=0,area=0;
s = (a+b+c)/2.0; /* s is semi-perimeter */
area = (sqrt)(s*(s-a)*(s-b)*(s-c));
8. In a shop, a discount of 10% on purchase amount is given. Write a program to find net
payable amount. (Net payable amount = purchase amount – discount) 5M
#include <stdio.h>
int main(void)
{
float purchase_amt,discount,net_payable_amt;
discount=purchase_amt*0.1;
net_payable_amt=purchase_amt-discount;
printf("%f",net_payable_amt);
return 0;
}
9. Complete the code:
#include<stdio.h>
int main( )
{
……float… radius, area;
…printf..(“Enter the radius of a circle\n”);
…scanf..(“..%f.. ”,&radius);
area= ..3.14 * radius * radius..;
printf(“Area=…%f… ”, …area…);
}
10. WAP to find sum of series 1+3+5+..+N, Where N is a positive odd Integer. 6M
#include <stdio.h>
int main()
{
int N,sum = 0;
printf(“Enter the value of N”);
scanf(“%d”,&N);
for (int i = 1; i <=N; i += 2) {
sum += i;
}
printf("The sum of the series 1+3+5+7+...+N is %d\n", sum);
return 0;
}
11. WAP to calculate the cost of ‘N’ pens. The cost of each pen is Rs. 75. The value of N
(i.e., number of pens) is supplied by user. 6M
#include <stdio.h>
int main()
{
int N,cost = 0;
printf(“Enter the number of pens purchased”);
scanf(“%d”,&N);
cost = N*75;
printf("The cost of %d pens is %d\n",N,cost);
return 0;
}
12. Analyse the following program and list the errors in the following program. Write the
correct code. 8M
Errors:
• In line 1, stdio.h must be enclosed in angular brackets.
• In line 2, Main() – ‘m’ must be in small case for main().
• In line 7, printf() spelling mistake and the sum value must be printed using %d
format specifier.
• In line 8, the return statement must return only integer value 0.
Correct Program:
#include<stdio.h>
int main()
{
int a=5, sum=0;
float b=5.66;
sum=a+b;
printf(“sum=%f”, sum);
return 0;
}
Output:
i) 1
ii) Error, space between identifier name is not allowed.
iii) 17 19
14. Design a C program to read two positive integers, say M and N, check whether M is an
exact multiple of N without using loops, and print suitable output messages. 5M
#include<stdio.h>
int main()
{
int M,N;
scanf("%d %d",&M,&N);
if(M%N==0)
printf("%d",M/N);
else
printf("%d”,0);
return 0;
}
#include <stdio.h>
int main()
{
int a, b, sum;
printf("\nEnter the two integer numbers: ");
scanf("%d%d",&a,&b);
sum = a + b;
printf("Sum of Numbers %d + %d = %d", a, b, sum);
return 0;
}
16. Develop a C program to print the numbers from 4 to 9 along with their squares. 4M
#include <stdio.h>
int main()
{
int i;
printf("Number \t Square\n");
for(i=4; i<=9;i++)
printf("%d \t= %d\n", i, (i*i));
}
iii.
#include <stdio.h>
int main()
{
printf("%d",(9-12/(3+3)*(2-1)));
return 0;
}
Output:
i) 2
ii) 5
iii) 7