0% found this document useful (0 votes)
55 views36 pages

D C S A: Program To Perform All Arithmetic Operations. N: C: R N

The document contains C program code to perform various arithmetic and logical operations. It includes programs to calculate sum, difference, product, quotient and remainder of two numbers; find the biggest of two numbers; grade student based on marks and averages; find the second biggest of three numbers; perform arithmetic operations based on user choice using switch case; calculate sum of digits of a number using while loop; check if a number is perfect using do-while loop; and calculate sum of squares of natural numbers up to n using for loop. The programs demonstrate the use of different loops and control structures in C language.

Uploaded by

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

D C S A: Program To Perform All Arithmetic Operations. N: C: R N

The document contains C program code to perform various arithmetic and logical operations. It includes programs to calculate sum, difference, product, quotient and remainder of two numbers; find the biggest of two numbers; grade student based on marks and averages; find the second biggest of three numbers; perform arithmetic operations based on user choice using switch case; calculate sum of digits of a number using while loop; check if a number is perfect using do-while loop; and calculate sum of squares of natural numbers up to n using for loop. The programs demonstrate the use of different loops and control structures in C language.

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE

PAGE | 1

AIM: Program to perform all arithmetic operations.

NAME:

CLASS:

REGISTER NO:

Definition of Operator:

Arithmetic Operators:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 2
#include <stdio.h>
#include <conio.h>
main()
{
int num1,num2,sum,dif,prod,quot,rem;
clrscr();
printf("Enter First Number : ");
scanf("%d",&num1);
printf("Enter Second Number : ");
scanf("%d",&num2);
sum = num1 + num2;
dif = num1 - num2;
prod = num1 * num2;
quot = num1 / num2;
rem = num1 % num2;
printf("\nSum = %d",sum);
printf("\nDifference = %d",dif);
printf("\nProduct = %d",prod);
printf("\nQuotient = %d",quot);
printf("\nReminder = %d",rem);
getch();
}
****************************************************
**
Output:
Enter First Number : 45
Enter Second Number : 9

Sum = 54
Difference = 36
Product = 405
Quotient = 5
Reminder = 0
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 3

AIM: Program to print the biggest of two numbers.

NAME:

CLASS:

REGISTER NO:

Simple if:

Syntax:

Flow Chart:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 4
#include <stdio.h>
#include <conio.h>
main()
{
int a,b;
clrscr();
printf("Enter Value of a: ");
scanf("%d",&a);
printf("Enter Value of b: ");
scanf("%d",&b);
if( a > b )
printf("%d is biggest",a);
else
printf("%d is biggest",b);
getch();
}

****************************************************
**
Output:
Enter Value of a: 30
Enter Value of b: 50
50 is biggest

Enter Value of a: 58
Enter Value of b: 38
58 is biggest
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 5

AIM: Program to read student details and print grade and


average grade points

NAME:

CLASS:

REGISTER NO:

Multiple If:

Syntax:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 6
#include <stdio.h>
#include <conio.h>
main()
{
int rno,s1,s2,s3,tot;
char name[20];
float avg;
clrscr();
printf("Enter Roll no and Name: ");
scanf("%d%s",&rno,&name);
printf("Enter Sub1, Sub2, Sub3 Marks: ");
scanf("%d%d%d",&s1,&s2,&s3);
tot = s1 + s2 + s3;
avg = tot / 3.0;
if(avg >= 90 && s1 >= 40 && s2 >= 40 && s3 >=
40)
printf("%s got grade 'O' [Out Standing] \n
Grade Points Average =
%f",name,avg);
else if(avg >= 80&&s1 >= 40 &&s2 >= 40&&s3 >=
40)
printf("%s got grade 'A+' [Excellent] \n
Grade
Points Average = %f",name,avg);
else if(avg >= 70&&s1 >= 40&&s2 >= 40&&3 >= 40 )
printf("%s got grade 'A' [Ver Good] \n Grade

Points Average = %f",name,avg);


else if( avg >= 60&&s1 >= 40&&s2 >= 40&&s3 >= 40
)
printf("%s got grade 'B+' [Good] \n Grade
Points Average = %f",name,avg);
else if( avg >= 55&&s1 >= 40&&s2 >= 40&&s3 >= 40
)

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 7
printf("%s got grade 'B' [Above Average] \n
Grade Points Average =
%f",name,avg);
else if(avg >= 50&&s1 >= 40&&s2 >= 40&&s3 >=
40 )
printf("%s got grade 'C' [Average] \n Grade
Points Average = %f",name,avg);
else if(avg >= 40&&s1 >= 40&&s2 >= 40&&s3 >=
40 )
printf("%s got grade 'D' [Pass] \nGrade
Points
Average = %f",name,avg);
else
printf("%s got grade 'F' [Fail]",name);
getch();
}
****************************************************
**
Output:
Enter Roll no and Name: 101 Sireesha
Enter Sub1, Sub2, Sub3 Marks: 89 92 91
Sireesha got grade 'O' [Out Standing]
Grade Points Average = 90.66

Enter Roll no and Name: 102 Prasanna


Enter Sub1, Sub2, Sub3 Marks: 64 72 61
Prasanna got grade 'B+' [Good]
Grade Points Average = 65.66
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 8

AIM: Program to print second biggest number in the given 3


numbers.

NAME:

CLASS:

REGISTER NO:

Nested if:

Flow Chart:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
PAGE | 9
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter 3 Numbers : ");
scanf("%d%d%d",&a,&b,&c);
if( a > b && a > c )
{
if( b > c )
printf("Second Biggest = %d",b);
else
printf("Second Biggest = %d",c);
}
else if( b > a && b > c )
{
if( a > c )
printf("Second Biggest = %d",a);
else
printf("Second Biggest = %d",c);
}
else
{
if( a > b )
printf("Second Biggest = %d",a);
else
printf("Second Biggest = %d",b);
}
getch();
}
****************************************************
**
Output:
Enter 3 Numbers : 12 15 18
Second Biggest = 15

Enter 3 Numbers : 15 12 18
Second Biggest = 15

Enter 3 Numbers : 12 18 15

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 10
Second Biggest = 15
****************************************************
**
AIM: Program to perform arithmetic operations according to
user choice.

NAME:

CLASS:

REGISTER NO:

Switch Case:

Syntax:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 11
#include <stdio.h>
#include <conio.h>
main(){
int num1, num2, res, ch;
printf("Enter Value One: ");
scanf("%d",&num1);
printf("Enter Value Two: ");
scanf("%d",&num2);
printf("1. Add \n2. Sub \n3. Mult \n4. Div
\nEnter
Your Choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
res = num1 + num2;
printf("Sum = %d",res);
break;
case 2:
res = num1 - num2;
printf("Difference = %d",res);
break;
case 3:
res = num1 * num2;
printf("Product = %d",res);
break;
case 4:
res = num1 / num2;
printf("Quotient = %d",res);
break;
default:
printf("Wrong Choice");
}
}
****************************************************
**
Output:
Enter Value One: 52
Enter Value Two: 34
1. Add
2. Sub
3. Mult

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 12
4. Div
Enter Your Choice: 2
Difference = 18

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 13

AIM: Program to print sum of all digits of the given positive


number.

NAME:

CLASS:

REGISTER NO:

While loop definition:

Syntax: Flow Chart:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 14

#include <stdio.h>
#include <conio.h>
main()
{
int num, dig, sum;
clrscr();
printf("Enter a Number: ");
scanf("%d",&num);
dig = 0;
sum = 0;
while( num != 0 )
{
dig = num % 10;
sum = sum + dig;
num = num / 10;
}
printf("Sum of all digits = %d",sum);
getch();
}
****************************************************
**
Output:
Enter a Number: 8645
Sum of all digits = 23
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 15

AIM: Program to check whether the given number is perfect


number or not.

NAME:

CLASS:

REGISTER NO:

Do while loop definition:

Syntax: Flow Chart:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 16

#include <stdio.h>
#include <conio.h>
main()
{
int num, i, fsum;
clrscr();
printf("Enter a Number: ");
scanf("%d",&num);
i = 1;
fsum = 0;
do
{
if( num % i == 0 )
fsum = fsum + i;
i++;
}while( i < num );
if( fsum == num )
printf("%d is Perfect Number",num);
else
printf("%d is not Perfect Number",num);
getch();
}

****************************************************
**
Output:
Enter a Number: 28
28 is Perfect Number

Enter a Number: 38
38 is not Perfect Number
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 17

AIM: Program to print sum of 1 + 22 + 32 + .. + n2 series.

NAME:

CLASS:

REGISTER NO:

For loop definition:

Syntax:

Important Points:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 18
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int n, i, sum;
clrscr();
printf("Enter Value of n: ");
scanf("%d",&n);
for( i = 1, sum = 0; i <= n; i++)
{
sum = sum + pow(i,2);
}
printf("Sum of powers of natural numbers up to
%d
= %d",n,sum);
getch();
}

****************************************************
**
Output:
Enter Value of n: 6
Sum of powers of natural numbers up to 6 = 91
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 19

AIM: Program to print a triangle with stars.

NAME:

CLASS:

REGISTER NO:

Nested Loop Definition:

Syntax:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 20
#include <stdio.h>
#include <conio.h>
main()
{
int n, lines, count;
clrscr();
printf("Enter no of lines: ");
scanf("%d",&n);
for( lines = 1; lines <= n; lines++)
{
for( count = 1; count <= n - lines; count++)
putch(' ');
for( count = 1; count <= 2 * lines - 1;
count++)
putch('*');
printf("\n");
}
getch();
}

****************************************************
**
Output:
Enter no of lines: 10

*
***
*****
*******
*********
***********
*************
***************
*****************
*******************

****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 21

AIM: Program to search for the given number in a set of


numbers.

NAME:

CLASS:

REGISTER NO:

Array Definition:

Array Declaration:

Array Initialization:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 22

#include <stdio.h>
#include <conio.h>
main()
{
int num[20], n, i, snum, pos;
clrscr();
printf("Enter How Many Numbers You Want: ");
scanf("%d",&n);
printf("Enter %d Numbers: ",n);
for( i = 0; i < n; i++)
scanf("%d",&num[i]);
printf("Enter Number to Search: ");
scanf("%d",&snum);
pos = -1;
for( i = 0; i < n; i++)
{
if( num[i] == snum )
{
pos = i + 1;
break;
}
}
if( pos != -1 )
printf("Number Found at %d Position",pos);
else
printf("Number Not Found");
getch();
}

****************************************************
**
Output:
Enter How Many Numbers You Want: 7
Enter 7 Numbers: 5 8 2 9 4 6 3
Enter Number to Search: 9
Number Found at 4 Position

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 23

Enter How Many Numbers You Want: 7


Enter 2729 Numbers: 5 8 2 9 4 6 3
Enter Number to Search: 1
Number Not Found
****************************************************
**
AIM: Program to perform matrix addition.

NAME:

CLASS:

REGISTER NO:

2D Array Definition:

2D Array declaration:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 24
#include <stdio.h>
#include <conio.h>
main()
{
int A[10][10], B[10][10], C[10][10];
int r1,c1,r2,c2,i,j;
clrscr();
printf("Enter Matrix A Size : ");
scanf("%d%d",&r1,&c1);
printf("Enter elements into Matrix A : \n");
for( i = 0; i < r1; i++){
for( j = 0; j < c1; j++)
{
printf("Enter A[%d][%d] : ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("Enter Matrix B Size : ");
scanf("%d%d",&r2,&c2);
printf("Enter elements into Matrix B : \n");
for( i = 0; i < r2; i++){
for( j = 0; j < r2; j++)
{
printf("Enter B[%d][%d] : ",i,j);
scanf("%d",&B[i][j]);
}
}
printf("Matrix A : \n");
for( i = 0; i < r1; i++)
for( j = 0; j < c1; j++)
printf("%d\t",A[i][j]);
printf("\n");
printf("Matrix B : \n");
for( i = 0; i < r2; i++)
for( j = 0; j < c2; j++)
printf("%d\t",B[i][j]);
printf("\n");
if( r1 == r2 && c1 == c2)
{
for( i = 0; i < r1; i++) {
for( j = 0; j < c1; j++)

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 25
{
C[i][j] = A[i][j] + B[i][j];
}
}
}
printf("Matrix Addition : \n");
for( i = 0; i < r1; i++)
for( j = 0; j < c2; j++)
printf("%d\t",C[i][j]);
printf("\n");
}
else
printf("Matrix Addition Not Possible");
getch();
}
****************************************************
**
Output:
Enter Matrix A Size: 2 2
Enter elements into Matrix A
Enter A[0][0] : 1
Enter A[0][1] : 2
Enter A[1][0] : 3
Enter A[1][1] : 4
Enter Matrix B Size: 2 2
Enter elements into Matrix B
Enter B[0][0] : 5
Enter B[0][1] : 6
Enter B[1][0] : 7
Enter B[1][1] : 8
Matrix A :
1 2
3 4
Matrix B :
5 6
7 8
Matrix Addition :
6 8
10 12
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 26

AIM: Program to check whether the given string is palindrome


or not.

NAME:

CLASS:

REGISTER NO:

String Manipulation Functions:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 27
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char str[20];
int len,i,flag;
clrscr();
printf("Enter a string: ");
scanf("%s",str);
len = strlen(str);
flag = 0;
i = 0;
while( i < len )
{
if( str[i] != str[len-1-i] )
{
flag = 1;
break;
}
i++;
}
if( flag == 0 )
printf("%s is palindrome string",str);
else
printf("%s is not a palindrome string",str);
getch();
}
****************************************************
**
Output:
Enter a string: racecar
racecar is palindrome string

Enter a string: program


program is not a palindrome string
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 28

AIM: Program to print factorial of a number using call by value


mechanism.

NAME:

CLASS:

REGISTER NO:

Function:

Call by value:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 29
#include <stdio.h>
#include <conio.h>
int factorial(int n);
main()
{
int num, fact;
clrscr();
printf("Enter a Number : ");
scanf("%d",&num);
fact = factorial(num);
printf("Factorial of %d = %d",num,fact);
getch();
}
int factorial(int n)
{
int f = 1;
while( n != 0 )
{
f = f * n;
n--;
}
return(f);
}
****************************************************
**
Output:
Enter a Number : 5
Factorial of 5 = 120
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 30

AIM: Program to swap given numbers using call by reference


mechanism.

NAME:

CLASS:

REGISTER NO:

Call by reference:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 31
#include <stdio.h>
#include <conio.h>
void swap(int *,int *);
main()
{
int a,b;
clrscr();
printf("Enter Value of a: ");
scanf("%d",&a);
printf("Enter Value of b: ");
scanf("%d",&b);
printf("Before Swap : \n");
printf("a = %d\nb = %d",a,b);
swap(&a,&b);
printf("\nAfter Swap : \n");
printf("a = %d\nb = %d",a,b);
getch();
}
void swap(int *m,int *n)
{
int k;
k = *m;
*m = *n;
*n = k;
}
****************************************************
**
Output:
Enter Value of a: 20
Enter Value of b: 40
Before Swap :
a = 20
b = 40
After Swap :
a = 40
b = 20
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 32

AIM: Program to print factorial of a number using recursive


function.

NAME:

CLASS:

REGISTER NO:

Recursive Function:

Advantages of function recursion:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 33
#include <stdio.h>
#include <conio.h>
int factorial(int n);
main()
{
int num, fact;
clrscr();
printf("Enter a Number : ");
scanf("%d",&num);
fact = factorial(num);
printf("Factorial = %d",fact);
getch();
}
int factorial(int n)
{
int f;
if( n == 0 || n == 1 )
f = 1;
else
f = n * factorial( n - 1 );
return(f);
}
****************************************************
**
Output:
Enter a Number : 5
Factorial = 120
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 34

AIM: Program to write into a file and then read from it


character by character.

NAME:

CLASS:

REGISTER NO:

File Definition:

Function used in working with files:

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 35
#include <stdio.h>
#include <conio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("myfile.txt","w");
if( fp == NULL )
{
printf("No sufficient memory to create
file");
exit(0);
}
printf("Enter Information and '@' to stop :
\n");
do
{
ch = getche();
fputc(ch,fp);
}while( ch != '@' );
printf("\nFile Creation Completed");
fflush(fp);
fclose(fp);
fp = fopen("myfile.txt","r");
if( fp == NULL )
{
printf("File Not Existed");
exit(1);
}
printf("\nInformation in the file : \n");
do
{
ch = fgetc(fp);
putch(ch);
}while( ch != EOF );
fclose(fp);
getch();
}

SRI SAI DEGREE COLLEGE - BOBBILI


DEPARTMENT OF COMPUTER SCIENCE
P A G E | 36

****************************************************
**
Output:
Enter Information and '@' to stop :
Files concept in C is used to store information
permanently in computer. @
File Creation Completed

Information in the file :


Files concept in C is used to store information
permanently in computer. @ P
****************************************************
**

SRI SAI DEGREE COLLEGE - BOBBILI

You might also like