0% found this document useful (0 votes)
37 views

C Program

The document contains examples of arithmetic operations, conditional operations, and iterative statements using C programming language. It includes programs to calculate sum, subtraction, multiplication, division; programs to find largest number, even/odd, positive/negative using if/else conditions; and examples of for, while, do-while loops to calculate sum of natural numbers, factorial. The output for each program is also given.

Uploaded by

Vikash. V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

C Program

The document contains examples of arithmetic operations, conditional operations, and iterative statements using C programming language. It includes programs to calculate sum, subtraction, multiplication, division; programs to find largest number, even/odd, positive/negative using if/else conditions; and examples of for, while, do-while loops to calculate sum of natural numbers, factorial. The output for each program is also given.

Uploaded by

Vikash. V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ARITHMETIC OPERATION:

EXERCISE 1:SUM

#include<stdio.h>
main()
{
int a,b,c;
printf("Enter two value:\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("The result is= %d",c);
getch();
}

OUTPUT:
Enter two value:
22
44
The result is= 66

EXERCISE 2:SUB

#include<stdio.h>
main()
{
int a,b,c;
printf("Enter two value:\n");
scanf("%d%d",&a,&b);
c=a-b;
printf("The result is= %d",c);
getch();
}

OUTPUT:
Enter two value:
100
50
The result is= 50
EXERCISE 3 : MULTIPLY

#include<stdio.h>
main()
{
int a,b,c;
printf("Enter two value:\n");
scanf("%d%d",&a,&b);
c=a*b;
printf("The result is= %d",c);
getch();
}

OUTPUT:
Enter two value:
50
50
The result is= 2500

EXERCISE 4: DIVISION

#include<stdio.h>
main()
{
int a,b,c;
printf("Enter two value:\n");
scanf("%d%d",&a,&b);
c=a/b;
printf("The result is= %d",c);
getch();
}

OUTPUT:
Enter two value:
1000
10
The result is= 100

EXERCISE 5: (SUM, SUB,MULTIPLY,DIVISION)

#include<stdio.h>
main()
{
int a,b;
printf("Enter two values\n");
scanf("%d%d",&a,&b);
printf("\n The sum = %d",a+b);
printf("\n The difference = %d",a-b);
printf("\n The product = %d",a*b);
printf("\n The Division = %d",a/b);
getch();
}

OUTPUT:
Enter two values
100
50

The sum = 150


The difference = 50
The product = 5000
The Division = 2

EXERCISE 5:(FAHRENHEIT TO CELSIUS)


#include<stdio.h>
main()
{
float f,c;
printf("enter value fahrenheit \n");
scanf("%f",&f);
c=(f-32)/1.8;
printf(" celsius is %f",c);
getch();
}

OUTPUT:
enter value fahrenheit
98
celsius is 36.666668

EXERCISE 6:(AREA OF CIRCLE)

#include<stdio.h>
main()
{
int r;
float pi=3.14,area;
printf("Enter the radius of circle\n");
scanf("%d",&r);
area=pi*r*r;
printf("Area of circle is %f",area);
getch();
}

OUTPUT:
Enter the radius of circle
8
Area of circle is 200.960007

CONDITIONAL OPERATION:

EXERCISE 1:(IF)

#include<stdio.h>
main()
{
int a,b;
a=10;
b=8;
if(a>b)
{
printf("True");
}
else
{
printf("False");
}
getch();
}

OUTPUT:
True

EXERCISE 2: (IF-ELSE IF)

#include<stdio.h>
main()
{
int a=10;
int b=8;
if(a==b)
{
printf("lavanya");
}
else if(a<b)
{
printf("bala");
}
else
{
printf("aashlin");
}
getch();
}

OUTPUT:
Aashlin

EXERCISE 3: (NESTED IF)

#include<stdio.h>
main()
{
int a=10,b=8;
if(a==b)
{
if(a>b)
{
printf("swetha");
}
else
{
printf("not swetha");
}}
else
{
printf("kavitha");
}getch();
}

OUTPUT:
Kavitha
EXERCISE 4: (BIGGEST/ SMALLEST NUMBER USING IF CONDITION)

#include<stdio.h>
main()
{
int a=20,b=40;
if(a>b)
{
printf("A is bigger value");
}
else
{
printf("B is bigger value");
}getch();
}

OUTPUT:
B is bigger value

EXERCISE 5: (ODD / EVEN NUMBER USING IF CONDITION)

#include<stdio.h>
main()
{
int num;
printf("Enter the number ");
scanf("%d",&num);
if(num%2==0)
{
printf(" Even number");
}else
{
printf("Odd number");
}getch();
}

OUTPUT:
Enter the number 50
Even number

EXERCISE 6: (POSITIVE / NEGATIVE NUMBER USING ELSE IF CONDITION)


#include<stdio.h>
main()
{
int num;
printf("Enter the number ");
scanf("%d",&num);
if(num>0)
{
printf("This is positive number");
}
else if(num==0)
{
printf("This number=0");
}
else
{
printf("This is negative number");
}
getch();
}

OUTPUT:

Enter the number -3


This is negative number

EXERCISE 7: (CALCULATOR APP USING ELSE IF CONDITION)

#include<stdio.h>
main()
{
int num1,num2;
char op;
printf("enter the number1");
scanf("%d",&num1);
printf("enter the number2");
scanf("%d",&num2);
printf("\n Enter operator:");
scanf("\n %c",&op);
if(op=='+')
{
printf("The sum=%d",num1+num2);
}
else if(op=='-')
{
printf("The sum=%d",num1-num2);
}else if(op=='*')
{
printf("The sum=%d",num1*num2);
}else if(op=='/')
{
printf("The sum=%d",num1/num2);
}else
{
printf("number is invalid");
}getch();
}

OUTPUT:

enter the number1 50


enter the number2 50

Enter operator:*
The sum=2500

EXERCISE 8: (LEAP YEAR / NOT LEAP YEAR USING NESTED IF CONDITION)


#include<stdio.h>
main()
{
int year;
printf("Enter the year");
scanf("%d",&year);
if(year%100==0)
{
if(year%400==0)
{
printf("leap year");
}
else
{
printf("not leap year");
}}
else
{
if(year%4==0)
{
printf("leap year");
}else
{
printf("not leap year");
}}getch();
}

OUTPUT:
Enter the year1990
not leap year

EXERCISE 8: (CALCULATOR APP USING SWITH CASE)

#include<stdio.h>
main()
{
int num1,num2;
char op;
printf("enter the number1");
scanf("%d",&num1);
printf("enter the number2");
scanf("%d",&num2);
printf("\n Enter operator:");
scanf("\n %c",&op);
switch(op)
{
case '+':
printf("The sum value=%d",num1+num2);
break;
case '-':
printf("The sum value=%d",num1-num2);
break;
case '*':
printf("The sum value=%d",num1*num2);
break;
case '/':
printf("The sum value=%d",num1/num2);
break;
default:
printf("invalid operator");
break;
}getch();
}
OUTPUT:

enter the number1 55


enter the number2 77

Enter operator:)
invalid operator

FOR LOOP ITERATIVE STATEMENTS


EXERCISE 1:(USING FOR LOOP SYNTAX)
#include<stdio.h>
main()
{
int i;
for(i=1;i<=5;i++)
{
printf("welcome\n");
}getch();
}

OUTPUT:
welcome
welcome
welcome
welcome
welcome

EXERCISE 2:( SUM OF FIRST N NATURAL NUMBER USING FOR LOOP )

#include<stdio.h>
main()
{
int i,n,sum=0;
printf("Enter the values of N\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("sum=%d",sum);
getch();
}
OUTPUT:
Enter the values of N
55
sum=1540

EXERCISE 3:( FACTORIAL USNIG FOR LOOP )

#include<stdio.h>
main()
{
int i,n,fact=1;
printf("enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("factorial=%d",fact);
getch();
}

OUTPUT:
enter the value of n 5
factorial=120

EXERCISE 4:( SUM OF FIRST N NATURAL NUMBER USING WHILE LOOP )

#include<stdio.h>
main()
{
int i=1,n,sum=0;
printf("Enter the value of n");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("sum= %d",sum);
getch();
}
OUTPUT:
Enter the value of n 8
sum= 36

EXERCISE 5:( FACTORIAL USNIG DO WHILE LOOP )

#include<stdio.h>
main()
{
int i=1,n,fact=1;
printf("enter the value of n");
scanf("%d",&n);
do
{
fact=fact*i;
i++;
}
while(i<=n);
printf("\n factorial=%d",fact);
printf(" \n i value %d",i);
getch();

OUTPUT:
enter the value of n 0
factorial=1
i value 2

You might also like