C Programming Exercises C Programming Using Simple Statements and Expressions
C Programming Exercises C Programming Using Simple Statements and Expressions
PROGRAM:
#include<stdio.h>
main()
{
floatr,area,;
printf(“\nEnter the radius of the circle”);
scanf(“%f”,&r);
area=3.14*r*r;
printf(“\nArea=%f”,area);
}
OUTPUT:
ALGORITHM:
Step 1: Declare the necessary variables a and b as integer and float. Step 2: Read the input of the
integer a.
Step 3: Assign the value of the integer to a float variable and a double variable.
Step 3: Display the value of the float variable and the double variable.
PROGRAM:
#include<stdio.h>
main()
{
float b; int a;
printf("\nEnteran Integer\n"); scanf("%d",&a);
b=(float)a;
printf("\nThe Converted float value is %f",b);
}
OUTPUT:
Enter an Integer 45
The Converted float value is 45.00000
Step-3 Calculate the temperature from Celsius to Fahrenheit using the formula
F= (1.8*c)+32
Step-4 Calculate the temperature from Fahrenheit to Celsius using the formula
C=5/9(F-32)
Step-6 Stop
PROGRAM:
#include<stdio.h>
main()
{
floatcel, fah ,c ,f;
clrscr();
printf(“\nEnter the fahrenheit value:”);
scanf(“%f”,&f);
cel=(5.0/9.0)*(f-32);
printf(“Celsius=%d”,cel);
printf(“\nEnter the Celsius value:”);
scanf(“%f”,&c);
fah=(9.0/5.0)*c+32;
printf(“Fahrenheit=%d”,fah);
getch();
}
OUTPUT:
OUTPUT:
Enter 5 Marks
98+87+92+78+75
RESULT:
Thus the C programming using simple statements and expression are executed and output was
verified
EX.NO :4e Expression Evaluation
DATE:
AIM:
To write a C program to evaluate the given expression
ALGORITHM:
Step-5 Stop
PROGRAM:
#include<stdio.h>
main()
{
inta,b,c;
floatx,y,z;
printf ("Enter the values for a,b,c \n");
scanf("%d,%d,%d", &a,&b,&c);
x = (a * b) – c;
y = (b/c) * a;
z = (a - b) / (c + d)
printf(" The value of x is “,x ”the value of y is “,y ”The value of z is “,z );
}
OUTPUT:
RESULT:
Thus the C programming using simple statements and expression are executed and output was
verified
Scientific problem using decision making and looping
Step-3 Divide the entered year by 4.If there is no remainder it is a leap year else not a leap year.
Step-5 Stop
PROGRAM:
#include<stdio.h>
main()
{
int year;
printf ("Enter the year \n");
scanf("%d", &year);
if (year%4==0)
printf("It is a Leap Year \n");
else
printf("It is Not a Leap Year\n");
}
OUTPUT:
Enter the year 2004
It is a Leap Year
Step-3 Check
If so , Print A is largest
Step-5 Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“Biggest of three No’s”);
printf(“Enter the values of A,B,C”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(b>c))
printf(“\n a=%d is greatest”,a);
elseif(b>c)
{
printf(“\n b=%d is greatest”,b);
}
else
{
printf(“\n c=%d is greatest”,c);
}
getch();
}
OUTPUT:
B = 20 is greatest
EX NO: 5 c Sum Of ‘N’ Natural Numbers using while loop
DATE:
AIM:
To write a c program to find the sum of “N‟ natural numbers for given range.
ALGORITHM:
Step 1: Initialize the sum as 0
Step 2: Read the range as input
Step 3: Initialize a counter with 1
Step 4: Overwrite the sum by adding counter value & sum
Step 5: Increment the counter value by 1
Step 6: Repeat the steps 4 & 5 until the counter is less than or equal to range Step 7: Print the sum
PROGRAM:
#include<stdio.h>
main()
{
int i,n,sum=0;
printf("Enter the range\n"); scanf("%d",&n);
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\nThe sum of first %d numbers is %d\n",n,sum);
}
OUTPUT:
Enter the range 16
The sum of first 16 numbers is 136
AIM:
To write a c program to find the sum of digits for a given number.
ALGORITHM:
Step 1: Declare a integer variable and initialize the sum as 0
Step 2: Read the input
Step 3: Take the remainder of given number while dividing 10
Step 4: Overwrite the sum by adding above remainder with available sum
Step 5: Overwrite the number by divide with 10
Step 6: Repeat the steps 3 to 5 until the number is greater than 0
Step 7: Print the sum
PROGRAM:
#include<stdio.h>
main()
{
int n,i,sum=0;
printf("Enter a Number\n"); scanf("%d",&n);
do
{ i=n%10; sum=sum+i; n=n/10;
}while(n>0);
printf("The Sum of digit is %d\n",sum);
}
OUTPUT:
Enter a Number 5891
The Sum of digit is 23
EX NO: 5e Arithmetic Calculator using switch case
DATE:
AIM:
To write a menu driven c program to implement an Arithmetic Calculator.
ALGORITHM:
Step 1: Get the two numbers
Step 2: Enter the choice
Step 3: Pass the choice into switch case
Step 4: In case 1, add the two numbers and print the result
Step 5: In case 2, subtract the two numbers and print the result
Step 6: In case 3, multiply the two numbers and print the result
Step 7: In case 4, divide the two numbers and print the result
PROGRAM:
#include<stdio.h>
main()
{
int a,b,ch,c;
printf("\nEnter the Number 1:\n"); scanf("%d",&a);
printf("\nEnter the Number 2:\n"); scanf("%d",&b);
printf("\n1.Add\n2.Subtract\n3.Multiply\n4.Divide\n"); printf("\nEnter the
Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: c=a+b;
printf("\n %d + %d = %d\n",a,b,c); break;
case 2: c=a-b;
printf("\n %d - %d = %d\n",a,b,c); break;
case 3: c=a*b;
printf("\n %d * %d = %d\n",a,b,c); break;
case 4: c=a/b;
printf("\n %d / %d = %d\n",a,b,c); break;
}
}
OUTPUT:
Enter the Number 1: 15
Enter the Number 2: 56
1.Add
2.Subtract
3.Multiply
4.Divide
Enter the Choice: 2
15 - 56 = -41
ALGORITHM:
Step 1: Get a number from the user
Step 2: Enter the choice
Step 3: Pass the choice into switch case
Step 4: In case 1,
a. Copy the given number into a variable
b. Initialize a counter to 1 and sum to 0
c. Extract the remainder of given number while dividing 10
d. Multiply the sum by 10
e. Overwrite the sum by adding above remainder with available sum
f. Overwrite the number by divide with 10
g. Repeat the steps a to f until the number is greater than 0
h. Compare the sum and copy of the number
i. If they are equal print as “Palindrome” else print “Not Palindrome”
Step 5: In case 2,
a. Copy the given number into a variable
b. Initialize a counter to 1 and sum to 0
c. Extract the remainder of given number while dividing 10
d. Calculate the value of remainder by assigning power 3
e. Overwrite the sum by adding above result with available sum
f. Overwrite the number by divide with 10
g. Repeat the steps a to e until the number is greater than 0
h. Compare the sum and copy of the number
i. If they are equal print as “Armstrong” else print “Not Armstrong”
Step 6: In case 3,
a. Initialize a flag value with 5
b. Initialize a counter to 2
c. Extract the remainder of given number by dividing with counter value
d. If the remainder is 0 changes the flag value to 0 and go to step g else go to next step.
e. Increment the counter value by 1
f. Repeat the steps a to e until counter is less than or equal to square root of the given
number
g. Check the flag value
h. If flag value is 0 then print as “Prime Number” else print as
“Not Prime”
PROGRAM:
#include<stdio.h>
#include<math.h>
main()
{
int a,i,sum=0,n,ch,m; printf("\nEnter a Number\n");
scanf("%d",&a);
printf("\n1.Palindrome\n2.Armstrong\n3.Prime\n"); printf("\nEnter the
Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: n=a; while(a>0) {
i=a%10;
sum=(sum*10)+i;
a=a/10;
}
if(n==sum)
printf("Given Number is Palindrome\n"); else
printf("Given Number is Not Palindrome\n"); break;
case 2: n=a; do
{ i=a%10; sum=sum+(i*i*i);
a=a/10;
}while(a>0);
if(n==sum)
printf("Given Number is Armstrong\n"); else
printf("Given Number is Not Armstrong\n"); break;
case 3: m=5; n=sqrt(a);
for(i=2;i<=n;i++)
{
if(a%i==0)
{
m=0;
break;
}
}
if(m==0)
printf("Given Number is Prime\n"); else
printf("Given Number is Not Prime\n"); break;
}
}
OUTPUT:
RESULT:
Thus the program carried out using decision making and Looping was executed and output was verified.
SIMPLE PROGRAMMING FOR ONE DIMENSIONAL AND TWO
DIMENSIONAL ARRAYS
DATE
AIM:
To write a C program to generate pascal triangle using array.
ALGORITHM:
Step-2 Enter the no of lines from the user and accordingly create an array.
Step-3 Declare two variables one for outer rows and the other for inner rows.
Step-4 Check that if (j==0||i==j), display a[i][j]=1 or else display the consecutive integer
Step-6 Stop.
PROGRAM:
#include<Stdio.h>
#include<conio.h>
void main()
{
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
printf("%4d",a[i][j]);
printf("\n");
}
getch();
}
OUTPUT:
Enter the number of lines:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
ALGORITHM:
Step 1: Declare an array with necessary size
Step 2: Get the value for total number of elements
Step 3: Initialize an index value to 0
Step 4: Read the input
Step 5: Increment the index value by 1
Step 6: Repeat steps 4 & 5 until counter less than total no. of elements
Step 7: Initialize an index value to 0 and sum to 0
Step 8: Obtain the sum by adding current index array value with available Sum
Step 9: Increment the index value by 1
Step 10: Repeat steps 8 & 9 until index value less than total no. of elements Step 11: Print the sum
PROGRAM:
#include<stdio.h>
main()
{
int i,n,a[10],sum=0;
printf("Enter total no. of Elements\n"); scanf("%d",&n);
printf("Enter Array elements one by one\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
printf("The Sum of Array Elements is %d\n",sum);}
OUTPUT:
Enter total no. of Elements 8
Enter Array elements one by one 15 69 32 10 45 66 32 11
The Sum of Array Elements is 280
AIM:
To write a program to multiply two matrixes.
ALGORITHM:
Step-9 Multiply the A and B matrix and store the element in the C matrix
Step-11 Stop
PROGRAM:
#include<stdio.h>
main()
{
int a[15][15],b[15][15],c[15][15],i,j,k,r,s;
int m,n;
printf(“\nEnter the Rows and Columns of A matrix...”);
scanf(“%d %d”,&m,&n);
printf(“\nEnter the Rows and Columns of B matrix...”);
scanf(“%d %d”,&r,&s);
if(m!=r)
printf(“\nMatrix multiplication cannot be performed”);
else
{
printf(“\nEnter the elements of A matrix”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“\t%d”,&a[i][j]);
}
printf(“\nEnter the elements of B matrix”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“\t%d”,&b[i][j]);
}
printf(“\nThe elements of A matrix”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,a[i][j]);
}
printf(“\n The elements of B matrix”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,b[i][j]);
}
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf(“The multiplication of two matrixes”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,c[i][j]);
}
}
INPUT AND OUTPUT
RESULT:
Thus the simple programming for arrays was executed and output was verified.
SIMPLE PROGRAMMING USING STRING FUNCTION
EX NO: 7a STRING COMPARISION
DATE:
AIM:
To write a program to compares two strings using strcmp function
ALGORITHM:
Step-4 compare first string and second string using strcmp function
Step-5 If the string is equal print the statement two strings are equal otherwise notequal
Step-6 Stop
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
OUTPUT:
Enter the first string
SVCET
Enter the second string
SAI NIKETAN
Entered the strings are not equal
EX NO: 7b String Concatenation
DATE:
AIM:
To write a c program to find the length of given two strings and concatenate them
ALGORITHM:
Step 1: Create two character arrays with necessary size
Step 2: Read the Strings
Step 3: Calculate the string lengths using strlen function
Step 4: Print the string lengths
Step 5: Join the two strings using strcat function
Step 6: Print the concatenated string
PROGRAM:
#include<stdio.h>
#include<string.h>
main()
{
char s[20],s1[20]; printf("Enter a String1\n");
scanf("%s",s); printf("Enter a String2\n");
scanf("%s",s1); strcat(s,s1);
printf("The Concatenated String is %s\n",s);
}
OUTPUT:
Enter a String1 hai
Enter a String2 hello
The Concatenated String is haihello
RESULT:
Thus the C program using string function was executed and output was verified
EX NO: 8a Find the smallest element of array using User defined Functions
DATE:
AIM:
To write a c program to find the smallest element of given array of elements using functions.
ALGORITHM:
Step 1: Create a function small()
Step 2: Inside the function
a. Store the 0th index value into base
b. Initialize a index to 1
c. Compare the array index value with base
d. If the array index value is smaller than base store the array index value into base
e. Increment the index by 1
f. Repeat the steps c & e until index reaches total no. of elements
g. Return the base value
Step 3: Inside the main function
a. Create an integer array with necessary size
b. Get the total number of elements
c. Read the array elements one by one
d. Call the small() function by passing array and no. of elements as arguments
PROGRAM
OUTPUT:
Enter total no. of elements 5
Enter Array Elements one by one 1 98 2 66 0
Array Elements: 1 98 2 66 0
The Smallest element of given array is 0
OUTPUT:
*a = *b;
*b = t;
}
OUTPUT:
RESULT:
Thus the C program with user defined function that includes parameter passing was executed and output
was verified
EX NO:9a Factorial Computation using recursive Function
DATE:
AIM:
To write a program to find the factorial of the given number using recursion
ALGORITHM:
Step-3 Call the recursive function passing the number to the recursive function as an argument.
Step-4 If the entered number is equal to one then return one to main function.
Step-5 If the number is less greater then one then call recursive
Step-7 Stop
PROGRAM:
OUTPUT:
DATE:
AIM:
To write a C Program to generate a Fibonacci series using recursive function.
ALGORITHM:
Step1: Start
Step2: Declare the variables int i, n and function longfibonacci(int).
PROGRAM:
#include<stdio.h>
#include<conio.h>
long fibonacci(int);
void main()
{
int i, n;
clrscr();
printf("\nPRG TO FIND FIBONACCI SERIES USING
RECURSION\n"); printf("\n-----------------------------------------------n");
printf("\nEnter the number of terms required in the series: ");
scanf("%d",&n);
printf("\n\nThe Fibonacci Series up to %d terms are ",n);
for(i=0;i<n;i++)
{
printf("%ld ",fibonacci(i));
}
getch();
}
long fibonacci(int n)
{
if (n==0)
return 0;
OUTPUT:
----------------------------------------
RESULT:
Thus the C program to compute factorial and Fibonacci series using recursive function
was executed and output was verified.
C PROGRAMMING USING STRUCTURES AND UNION
Ex.No 10a EMPLOYEE PAYROLL USING STRUCTURE
DATE:
AIM:
To write a c program to generate employee payroll using structures.
ALGORITHM:
Step-2 Create a Structure named Employee containing records Id,name, Basic Salary, Net Salary, HRA, DA
and Tax.
Step-4 Using the basic salary, DA, HRA, Tax calculate tax and net salary
Step-5 Display the details each employee record containing name, number, basic salary, HRA, DA,
Net Salary and Tax
Step-6 Stop.
PROGRAM:
AIM:
To write a c program to store the book information using union.
ALGORITHM:
Step 1: Create an union book
Step 2: Declare one integer and a character array inside the union for book name and book price
Step 3: Get the book name and price
Step 4: Print the book name and price
Step 5: Get the Book name alone
Step 6: Print the Book name
Step 7: Get the Book Price
Step 8: Print the Book price
PROGRAM:
#include<stdio.h> union book
{
int price;
char bname[20]; };
main()
{
union book b;
printf("Enter the Book Details:\n"); printf("Enter the
Book Name:\n"); scanf("%s",&b.bname);
printf("Enter the Book Price:\n");
scanf("%d",&b.price); printf("BOOK
DETAILS:\n"); printf("%s\t%d\n",b.bname,b.price);
printf("Enter the Book Name:\n");
scanf("%s",b.bname);
printf("Book Name=%s\n",b.bname);
}
OUTPUT
Enter the Book Details:
Enter the Book Name: English
Enter the Book Price: 150
BOOK DETAILS: 150
Enter the Book Name: English
Book Name=English
RESULT:
Thus the C program using structures and unions was executed and output was verified.