0% found this document useful (0 votes)
31 views10 pages

CP - Assignment 3

The document contains 9 questions about writing C programs for various tasks like printing ASCII values, checking if a point lies inside a circle, calculating size of variables, checking voting eligibility, finding leap years, calculating roots of quadratic equations, reversing digits of a number, and creating a menu driven calculator using if-else and switch case statements. For each question, it provides the question, flowchart, code and output.

Uploaded by

lokeshjoshimj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

CP - Assignment 3

The document contains 9 questions about writing C programs for various tasks like printing ASCII values, checking if a point lies inside a circle, calculating size of variables, checking voting eligibility, finding leap years, calculating roots of quadratic equations, reversing digits of a number, and creating a menu driven calculator using if-else and switch case statements. For each question, it provides the question, flowchart, code and output.

Uploaded by

lokeshjoshimj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

QUES : 1. Write a C program to print ASCII value of a character.

FLOWCHART:

CODE: #include<stdio.h>

int main (){

char ch ;

printf("Enter a character : ");

scanf("%c",&ch);

printf ("The ASCII value is of %c is %d .",ch,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.

(Hint: Use sqrt() and pow( ) functions).

FLOWCHART:

CODE: #include<math.h>

int main (){

int x,y,r,sum,h,k,p,X,Y;

printf ("Enter center h,k : ");

scanf("%d,%d",&h,&k);

printf ("Enter x ,y, r respectively : ");

scanf("%d%d%",&x,&y,&r);

X = (x-h);

Y = (y-k);

sum = pow(X,2)+ pow (Y,2);

p = (pow(r,2));

if (p == sum ){

printf("on the circle");}

else if (p < sum ) {

printf("outside the circle");}

else {

printf("inside the circle");

}}

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

for (int i = 1;number > 0;i++){


k = number%10;
sum = sum*10 + k;
number = number/10;}
printf(" Reverse of the number is %d
:",sum);
return 0;
}

OUTPUT:
QUES : 8. Write a C program for a Menu driven calculator using if else statements.

FLOWCHART:

CODE: (using if else)

#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>

int main (){


int num,decimal_num = 0, base = 1,
rem;
printf (" Enter a binary number \n");
scanf (" %d", &num);
while ( num > 0)
{ rem = num % 10;
decimal_num = decimal_num + rem
* base;
num = num / 10;
base = base * 2; }
printf (" \n The decimal number is
%d \t", decimal_num);
return 0;}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy