0% found this document useful (0 votes)
0 views11 pages

C-Programming Solve Answer

The document contains multiple C programming examples covering basic arithmetic operations, area and perimeter calculations for various shapes, control structures like if-else statements, and looping constructs such as for and while loops. It provides code snippets for entering values, performing calculations, and displaying results for different geometric shapes and conditions. Additionally, it explains control structures and their usage in programming flow.

Uploaded by

anshikashaha0
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)
0 views11 pages

C-Programming Solve Answer

The document contains multiple C programming examples covering basic arithmetic operations, area and perimeter calculations for various shapes, control structures like if-else statements, and looping constructs such as for and while loops. It provides code snippets for entering values, performing calculations, and displaying results for different geometric shapes and conditions. Additionally, it explains control structures and their usage in programming flow.

Uploaded by

anshikashaha0
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/ 11

a) Write a c program to enter any two numbers and print the sum of them.

# include <stdio.h>
#include<conio.h>
void main()
{
int a, b, s;
clrscr();
printf(“Enter First Number”);
scanf(“%d”, &a);
printf(“Enter Second Number”);
scanf(“%d”, &b);
s = a + b;
printf (“The sum is %d”, s);
getch();
}
b) Write a c program to calculate the area of a rectangle, where the length is 20 and breadth is 15.
# include<stdio.h>
#include<conio.h>
void main( )
{
int l = 20, b = 15;
int a;
clrscr();
a = l * b;
printf (“The area is %d”, a);
getch();
}
c) WAP to calculate the area of a rectangle in c. [Hints: a = l x b]
# include<stdio.h>
#include<conio.h>
void main()
{
int l, b, a;
printf (“Enter Length: ”);
scanf (“%d”, &l);
printf (“Enter Breadth: “);
scanf (“%d”, &b);
a = l * b;
printf (“The area is %d”, a);
getch();
}
d) Write a program to enter length and breadth of a rectangle and calculate the perimeter. [p = 2 (l + b) ]
# include<stdio.h>
#include<conio.h>
void main ()
{
int l, b, p;
printf (“Enter Length :”);
scanf (“%d”, &l);
printf (“Enter Breadth :”);
scanf (“%d”, &b);
p = 2 * (l + b);
printf (“The perimeter is : %d”, p);
getch();
}
e) WAP to calculate the area of a triangle .
# include<stdio.h>
#include<conio.h>
void main()
{
int b, h, a;
printf (“Enter base and height”);
scanf (“%d %d”, &b, &h);
a = ½ * b * h;
printf (“The area is %d”, a);
getch();
}

f) WAP to enter the length of any one side of a square and calculate the area and perimeter in c language.
# include<stdio.h>
#include<conio.h>
void main ()
{
int l, a, p;
clrscr ();
printf (“Enter length”);
scanf (“%d”, &l);
a = l * l;
p = 4 * l;
printf (“The area is %d and the Perimeter is %d”, a, p);
getch();
}
g) WAP to calculate the volume of a box.[v=l*b*h] (Do yourself)
h) WAP to enter the radius of a circle and print the circumference of it. [c = 2 pi r] (Do yourself)
CONTROL STRUCTURE IN C (important)
Normally, the program execution flows top to the bottom lines of the programs. It means program executes one line after
another. But sometime we need to execute other part of the programs or we need to divert the program flow from its
normal execution. In such a case, we need some program statements, which are known as Control Structures. They are
of three Types of control structure : a. sequence b.selection and c. looping
A) Sequence: Program generally flows from top left top right bottom.
1. WAP to enter the length, breadth and height of a box and calculate the total surface area (TSA). [TSA = 2 * (L*B
+ B*H + L*H)]
# include <stdio.h>
#include<conio.h>
void main ()
{
int l, b, h, tsa;
clrscr();
printf (“Enter length, breadth and height”);
scanf (“%d %d %d”, &l, &b, &h);
tsa = 2 * (L*B + B*H + L*H);
printf (“Total surface area is %d”, tsa);
getch ();
}
2. WAP to enter the radius of a circle and calculate the area . [a = pi r 2]
# include <stdio.h>
#include<conio.h>
void main ()
{
float r, a;
printf (“Enter radius”);
scanf (“%f”, &r);
a = 22/7 * r * r;
printf (“The area is %f”, a);
getch();
}
3. ) WAP to enter the radius of a circle and print the circumference of it. [c = 2 pi r]
B) Selection/branching/condition: These type of control structure changes the flow of program execution with or with
out conditoon.
B.1) Conditional Statements: They break the program flow if the condition is matching. Eg. if, if….else and if…..else….if
if condition
This condition is used for a single condition and single block of statements.
Syntax:
if (condition)
{
Block of statements'
}
Example:
1. WAP to enter the marks of n E gl
# include <stdio.h>
#include<conio.h>
void main ( )
{
float e;
printf (“Enter English Marks \n”);
scanf (“%f”, &e);
if (e >= 40)
{
Printf (“Pass”);
}
getch ( );
}
Note: The above program states only about pass. If the marks is less than 40, it displays blank.
2. WP A to en
# include <stdio.h>
#include<conio.h>
void main ( )
{
float a;
printf (“Enter age \n”);
scanf (“%f”, &a);
if (a >= 18)
{
Printf (“You can vote”);
}
getch ( );
}
If….else condition
This condition is used for a double conditions and double blocks of statements.
Syntax:
if (condition)
{
Block of statements'
}
else
{
Block of statements'
}
Example:
1. WP A to en
# include <stdio.h>
#include<conio.h>
void main ( )
{
float e;
printf (“Enter English Marks \n”);
scanf (“%f”, &e);
if (e >= 40)
{
printf (“Pass”);
}
else
{
printf (“Pass”);
}
getch ( );
}
xE mp
a l
“He an c not
# include <stdio.h>
#include<conio.h>
void main ( )
{
float e;
printf (“Enter age\n”);
scanf (“%f”, &a);
if (a >= 18)
{
printf (“You can vote”);
}
else
{
printf (“You cant vote”);
}
getch ( );
}
Example 3: WAP to check whether given number if odd or even
# include <stdio.h>
#include<conio.h>
void main ( )
{
int n;
printf (“Enter number\n”);
scanf (“%d”, &n);
if (n%2 == 0)
{
printf (“%d is even”,n);
}
else
{
printf (“%d is odd”,n);
}
getch ( );
}
Example 4: WAP to find greatest among two number.
# include <stdio.h>
#include<conio.h>
void main ( )
{
int a,b;
printf (“Enter two number\n”);
scanf (“%d %d”, &a,&b);
if (a>b)
{
printf (“%d is greatest”,a);
}
else
{
printf (“%d is greatest”,b);
}
getch ( );
}
else if statement: This statement is used for the multiple conditions and multiple blocks of statements.
Syntax:
if (condition 1)
{
Statement Block 1;
}
else if (condition 2)
{
Statement Block 2;
}
..
..
else
{
default staement
}

x
E mp
a ss than 0, l
ot ”. her
# include <stdio.h>
#include<conio.h>
void main ( )
{
int n;
printf("Enter number");
scanf("%d",&n);
if (n > 0 )
{
printf (“Positive”);
}
else if (n < 0 )
{
printf (“Negative”);
}
else
{
printf (“Zero”);
}
getch ( );
}
Example 2: WAP to enter the percentage and print the division
# include <stdio.h>
#include<conio.h>
void main ()
{
float p;
clrscr ( );
printf (“Enter percentage:”);
scanf (“%f”, &p);
if (p > = 80 && p<=100)
printf (“Distinction”);
else if (p>=60 && p<80)
printf (“First”);
else if (p>=50 && p<60)
printf (“Second”);
else if ( p>=40 && p<50)
printf (“Third”);
else
printf("fail");
getch ();
}
Example 3: WAP to enter 3 number and print greatest number
# include <stdio.h>
#include<conio.h>
void main ( )
{
int a,b,c;
printf("Enter number");
scanf("%d %d %d",&a,&b,&c);
if (a>b && a>c)
{
printf (“%d is greatest”,a);
}
else if (b>a && b>c)
{
printf (“%d is greatest”,b);
}
else
{
printf (“%d is greatest”,c);
}
getch ( );
}
B.2) Unconditional Statements: They break the program flow with out any condition. Eg. goto statement
Syntax
label:
goto label;
OR
goto label;
label:
Program example of goto [This program allows user to only enter value under 100]
# include <stdio.h>
#include<conio.h>
void main ()
{
float p;
clrscr ( );
label:
printf (“Enter percentage:”);
scanf (“%f”, &p);
if (p > 100)
{
printf (“Invalid input”);
goto label;
}
getch();
}

C) Looping: Loop is the process of repeating any block of statements up to the given number of times or until the given
condition is not over.
There are three types of Loop: 1. for loop 2. while loop and 3. do while loop
FOR : Syntax of for loop… loop
for (initialization; condition; increment/decrement)
{
Statements (s);
}
Examples:
a) 1, 2, 3, 4, … … .100.
# include <stdio.h>
#include<conio.h>
int main()
{
int i;
for (i=1;i<=100;i++)
{
printf("%d\n",i);
}
return 0;
}
b) 1, 3, 5, ,7 … … ….9
c) 2, 4, 6, 8, … … … 100.
d) . 5, 10, ,15 … … … ….50
# include <stdio.h>
#include<conio.h>
int main()
{
int i,s;
for (i=1;i<=10;i++)
{
s=5*i;
printf("%d\n",s);
}
return 0;
}
WAP to calculate sum of n-natural number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,i,s=0;
printf("Enter any number\t");
scanf("%ld",&n);
for (i=1;i<=n;i++)
{
s=s+i;
}
printf("Sum is %d\n",s);
return 0;
}
WAP to calculate product of n-natural number. (Factorial of a given number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,i,p=1;
printf("Enter any number\t");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
p=p*i;
}
printf(“Product is %d\n”,p);
return 0;
}
WAP to generate fibonaci series. [ 0,1, 2,3 5 8 ….01 t
# include <stdio.h>
#include<conio.h>
int main()
{
int i,a=0,b=1,c;
printf(“%d%d””,a,b);
for (i=1;i<=10;i++)
{
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
return 0;
}
WAP to generate 1 2 4 7 11 ...............10th term
# include <stdio.h>
#include<conio.h>
int main()
{
int i,a=1,g=1;
for (i=1;i<=10;i++)
{
printf(“%d”,a);
a=a+g;
g=g+1;
}
return 0;
}
WAP to generate 1 2 5 10 17 ...............10th term
# include <stdio.h>
#include<conio.h>
int main()
{
int i,a=1,g=1;
for (i=1;i<=10;i++)
{
printf(“%d”,a);
a=a+g;
g=g+2;
}
return 0;
}
WAP to generate 3 12 27 48 ................10th term
# include <stdio.h>
#include<conio.h>
int main()
{
int a,i;
for (i=1;i<=10;i++)
{
a=3*I*I;
printf(“%d”,a);
}
return 0;
WHILE Loop (Entry Control loop)
Syntax:
while(condtion)
{
Block of statements;
}
WAP to calculate sum of n-natural number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,s=0,i=1;
printf("Enter any number\t");
scanf("%d",&n);
while (i<=n)
{
s = s+i;
i=i+1;
}
printf("Sum of digits is %ld\n",s);
return 0;
}
WAP to calculate product of digits of a given number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,p=1,i=1;
printf("Enter any number\t");
scanf("%d",&n);
while (i<=n)
{
p = p*i;
i=i+1;
}
printf(“Product of digits is %d\n”,p);
return 0;
}
WAP to generate 1 2 4 7 11 ...............10th term
# include <stdio.h>
#include<conio.h>
int main()
{
int i=1,a=1,g=1;
while (i<=10)
{
printf(“%d”,a);
a=a+g;
g=g+1;
i=i+1;
}
return 0;
}
WAP to calculate sum of digits of a given number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,s=0,r;
printf("Enter any number\t");
scanf("%d",&n);
while (n!=0)
{
r = n%10;
s = s+r;
n = n/10;
}
printf("Sum of digits is %d\n",s);
return 0;
}
WAP to calculate product of digits of a given number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,p=1,r;
printf("Enter any number\t");
scanf("%d",&n);
while (n!=0)
{
r = n%10;
p = p*r;
n = n/10;
}
printf(“Product of digits is %d\n”,p);
return 0;
}
WAP to reverse a given number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,p=0,r;
printf("Enter any number\t");
scanf("%d",&n);
while (n!=0)
{
r = n%10;
p = p*10+r;
n = n/10;
}
printf(“Reverse is %d\n”,p);
return 0;
}
WAP to check whether given number is palindrome or not.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,p=0,r,z;
printf("Enter any number\t");
scanf("%d",&n);
z=n;
while (n!=0)
{
r = n%10;
p = p*10+r;
n = n/10;
}
if (z==p)
{
printf(“%d is palindrome \n”,z);
}
else
{
printf(“%d is not palindrome \n”,z);
}
return 0;
}
WAP to check whether given number is armstrong or not. [153 is Armstrong]
# include <stdio.h>
#include<conio.h>
int main()
{
int n,s=0,r,z;
printf("Enter any number\t");
scanf("%d",&n);
z=n;
while (n!=0)
{
r = n%10;
s = s+pow(r,3);
n = n/10;
}
if (z==s)
{
printf(“%d is armstrong \n”,z);
}
else
{
printf(“%d is not armstrong \n”,z);
}
return 0;
}
DO Loop
Syntax:
do
{
Block of statements;
}while(condition);
WAP to calculate sum of n-natural number.
# include <stdio.h>
#include<conio.h>
int main()
{
int n,s=0,i=1;
printf("Enter any number\t");
scanf("%d",&n);
do
{
s = s+i;
i=i+1;
}while (i<=n);
printf("Sum of digits is %ld\n",s);
return 0;
}

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