CP - Assignment 3
CP - Assignment 3
FLOWCHART:
CODE: #include<stdio.h>
char ch ;
scanf("%c",&ch);
return 0;}
OUTPUT:
QUES : 2. Given the coordinates (x, y) of a centre of a circle and it’s radius, write a program which
will determine whether apoint lies inside the circle, on the circle or outside the circle.
FLOWCHART:
CODE: #include<math.h>
int x,y,r,sum,h,k,p,X,Y;
scanf("%d,%d",&h,&k);
scanf("%d%d%",&x,&y,&r);
X = (x-h);
Y = (y-k);
p = (pow(r,2));
if (p == sum ){
else {
}}
OUTPUT:
QUES : 3. Write a C program to print size of variables using sizeof() operator.
FLOWCHART:
CODE:
#include<stdio.h>
int main (){
int x;
float y;
char m;
printf("Size of int is %d : ",sizeof(x));
printf("Size of float is %d: ",sizeof(y));
printf("Size of char is %d : "),sizeof(m);
return 0;}
OUTPUT:
QUES : 4. Write a C program to check whether a person is eligible for voting or not?
FLOWCHART:
CODE:
#include<stdio.h>
#include<math.h>
int main (){
int age;
printf ("Enter age: ");
scanf("%d",&age);
if (age >= 18 ){
printf("Eligible to vote ");}
else {
printf(" Not Eligible to vote");}
return 0;}
OUTPUT:
QUES : 5.Write a C program to read a year as an input and find whether it is leap year or not. Also
consider end of the centuries.
FLOWCHART:
CODE:
#include<stdio.h>
int main ()
{ int year, y,y2;
printf("Enter a year ");
scanf("%d",&year);
y = year%4 ;
y2 = year%100;
if (year%400==0){
printf("It is a 1 leap year"); }
else if ( y2 == 0){
printf("It is a not leap year"); }
else if (y == 0)
{ printf("It is a leap year");}
else
{ printf ("It is not leap year");}
return 0;}
OUTPUT:
QUES : 6.Write a C program to find roots of a quadratic equation.
FLOWCHART:
CODE:
#include<stdio.h>
#include<math.h> //(4x^{2}-5x-12=0)
int main (){
float a,b,c,root2,root1,D ;
printf( "a,b,c from ax2+bx+c = 0: ") ;
scanf("%f%f%f",&a,&b,&c);
D= (pow(b,2)- (4*a*c));
root1= (-b+sqrt(D))/(2*a);
root2 = (-b-sqrt(D) )/(2*a);
if (D>0)
printf("Roots of eqn are %.2f,%.2f",root1,root2) ;
else if (D==0)
printf("Roots of eqn are %.2f,%.2f",root1,root2);
else if (D<0)
printf("No real Roots");
return 0;}
OUTPUT:
QUES : 7.Write a C program to reverse the digits of a number.
FLOWCHART:
CODE:
#include<stdio.h>
int main (){
int number,k,sum = 0;
printf("Enter a number: ");
scanf("%d",&number);
OUTPUT:
QUES : 8. Write a C program for a Menu driven calculator using if else statements.
FLOWCHART:
#include<stdio.h>
int main (){
while(1){
int op,a,b,c;
printf(" Menu is :\n");
printf("Enter 1 for addition addition\n ");
printf("Enter 2 for addition substraction\n");
printf("Enter 3 for addition multiplication\n");
printf("Enter 4 for addition devide\n");
printf("Enter 5 for addition modulo\n");
printf("Enter 6 to exit\n");
printf("Enter 'a' ");
scanf("%d",&a);
printf("Enter 'b' ");
scanf("%d",&b);
printf("Enter operation no. ");
scanf("%d ",&op);
if (op == 1 ){
c = a + b;
printf("%d",c);}
else if (op == 2){
c = a - b;
printf("%d",c);}
else if (op == 3){
c = a *b;
printf("%d",c);}
else if (op == 4){
c = a / b;
printf("%d",c);}
else if (op == 5){
c = a % b;
printf("%d",c);}
else if (op == 6){
break;}
else{
printf("Invalid Input\n");} return 0 ;}}
CODE: (using switch case) (ques 9 )
#include<stdio.h>
int main (){
while(1){
int op,num1,num2;
printf(" Menu is :\n");
printf("Enter 1 for addition addition\n ");
printf("Enter 2 for addition substraction\n");
printf("Enter 3 for addition multiplication\n");
printf("Enter 4 for addition devide\n");
printf("Enter 5 for addition modulo\n");
printf("Enter 6 to exit\n");
printf("Enter 'a' ");
scanf("%d",&num1);
printf("Enter 'b' ");
scanf("%d",&num2);
printf("Enter operation no. ");
scanf("%d ",&op);
switch(op) {
case 1:printf("The Addition of %d and %d is: %d\n",num1,num2,num1+num2);
case 2:printf("The subtration of %d and %d is: %d\n",num1,num2,num1-num2);
case 3:printf("The multiplikation of %d and %d is: %d\n",num1,num2,num1*num2);
case 4: if(num2==0) {
printf("The second integer is zero. Divide by zero error.\n") } else {
printf("The Division of %d and %d is : %d\n",num1,num2,num1/num2); }
case 5:printf("The modulo of %d and %d is: %d\n",num1,num2,num1+num2);
case 6:
{printf("bye!!");
break;}
default: {
printf("Input correct operation\n");}
return 0; } }}
OUTPUT:
QUES :10. Write a C program to convert a binary number to a decimal number.
FLOWCHART:
CODE:
#include<stdio.h>
OUTPUT: