C Programs
C Programs
Date:
AIM:
ALGORITHM:
Step-4: Prompt the user to enter an integer and read the input the variable"num" using "scanf("%d",&
num)".
Step-6: Prompt the uses to enter a float and read the input the variable"f"using "scanf("%f",&f)".
Step-8: Prompt the user to enter a character and read the input into the variable"ch" using
"scanf("%s",&ch)".
Step-9: Print the entered character using "printf("\n entered character is:%s",ch)".
#include<stdio.h>
#inlcude<conio.h>
Void main()
int num;
float f;
char ch str[100];
clrscr();
scanf(“%d”,&num);
printf(“\nEntered integer:%d”,num);
scanf(“%f”,&f);
printf(“\nEntered float:%f”,f);
scanf(“%s”,&ch);
printf(“\nEntered character:%s”,ch);
getch();
OUTPUT:
Entered integer:2
Entered float:5.4000
Enter the character:S
Entered character:S
RESULT:
Date:
AIM:
ALGORITHM:
Step-3: Inside the main function declare an integer variable called "x".
Step-4: Use that printf function is display the message "please enter the character here".
Step-5: Use the getchar function to read a character from the uses under and store it in the variable"x".
Step-6: Use the putchar function to display the characters stored in the variable"x".
#include<stdio.h>
#include<conio.h>
Void main()
int x;
clrscr();
x=getchar(x);
putchar(x);
getch();
OUTPUT:
RESULT:
Date:
AIM:
ALGORITHM:
Step-3:Clear the screen using to enter the values of "a" and "b".
Step-4: Prompt the user to enter the values 'a' and 'b'.
Step-6: Calculate the sum of 'a' and 'b' and store it in the 'sum' variable.
Step-7:Calculate the subtraction of 'a' and 'b' and store it in the 'sub' variable.
Step-8: Calculate the multiplication of 'a' and 'b' and store it in the 'mult' variable.
Step-9: Calculate the division of 'a' and 'b' and store it in the 'div' variable.
Step-10: Calculate the reminder of 'a' and 'b' and store it in the 'rem' variable.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
int a,b,sum,sub,mult,div,rem;
clrscr();
scanf(“%d %d”,&a,&b);
sum=a+b;
sub=a-b;
mult=a*b;
div=a/b;
rem=a%b;
getc();
return 0;
OUTPUT:
RESULT:
Date:
AIM:
To write the c program to find the greatest number using conditional operators.
ALGORITHM:
Step-4:Read and store the values of 'a' and 'b' from the user.
Step-7: Print the value of 's' as the greatest of the two number.
#include<stdio.h>
#include<conio.h>
void main()
int a,b,s;
clrscr();
scanf(“%d %d”,&a,&b);
s=a>b?a:b;
getch();
OUPUT:
RESULT:
Date:
AIM:
ALGORITM:
Step-3: Assign the 5 to variables a and b and assign the value 10 to variable c.
Step -4: calculate the result of the expression (a=b)(c>b) and store it in the variable result.
Step-5: print the result using printf using format"(a=b)&&(c>b) equal to %d\n", result.
Step -6: calculate the result of expression (a=b)&&(c<b) and store it in variable result.
Step-7: print the value of result using printf using the format "(a=b)&&(c<b) equal to %d\n", result.
Step -8: the result of the expression (a=b)||(c<b) and store it in the variable result
Step -9: print the value of result using the format "(a=b)||(c<b) equal to %d\n", result.
Step-10: calculate the result of the expression (a!=b)||(c<b) and store it in the variable result.
Step -11: print a value of result using the formula"(a!=b)||(c<b) equal to %d\n", result.
Step-12: calculate the result of the expression !(a!=b) and store it in the variable result.
Step -13: print the value of result using the format !(a!=b) equal to %d\n", result.
Step -14: calculate the result of the expression !(a==b) and store if in the variable result
Step -15: print the value of result using format !(a==b) equal to %d\n", result.
#include<stdio.h>
#include<conio.h>
int main()
int a=5,b=6,c=10,result;
clrscr();
result=(a=b)&&(c>b);
result=(a=b)&&(c<b);
result=(a=b)||(c<b);
result=(a!=b)||(c<b);
result=!(a!=b);
result=!(a==b);
getch();
return 0;
OUTPUT:
(a=b)&&(c>b) equals to 1
(a=b)&&(c<b) equals to 0
(a=b)||(c<b) equals to 1
(a!=b)||(c<b) equals to 0
!(a!=b) equals to 1
!(a==b) equals to 0
RESULT:
Date:
AIM:
To write the c program to determine whether an integer is odd or even by using if..else statement.
ALGORITHM:
Step 3: Use the % operator to calculate the remainder when the integer is divided by 2
Step 5: If the remainder is 0 then the integer is even print a message starting that the number is even
Step 6: If the remainder is not 0 then the integer is odd. Print a message starting that the number is odd
#include<stdio.h>
#include<conio.h>
void main()
int num1,rem1;
clrscr();
printf(“Input an integer:”);
scanf(“%d”,&num1);
rem1=num1%2;
if(rem1==0)
else
getch();
return 0;
OUTPUT:
RESULT:
Date:
AIM:
To write a c program to find the greatest number among three input using nested if condition.
ALGORITHM:
Step-3: Ask the user to input the values of the three numbers
Step-6: Compare the numbers using if-else statements to determine the greatest number
Step-7: Print the result starting which number is the greatest among the three.
#include<stdio.h>
#include<conio.h>
void main()
int num1,num2,num3;
clrscr();
scanf(“%d %d %d”,&num1,&num2,&num3);
if(num1>num2)
if(num1>num3)
else
else if(num2>num3)
}
else
{
printf(“\nThe 3rd Number is thr greatest among three.”);
getch();
OUTPUT:
RESULT:
Date:
AIM:
To write a c program to calculatae the marks, percentage and division for student based dddddon their
marks in physics,chemistry and computer application using if else ladder statement.
ALGORITHM:
Step-7: prompt the user for physics , chemistry and computer Application.
Step-8 : Read and store the physics, chemistry and computer Applications marks.
Step-12: print roll number,name, subject marks,total marks, percentage and division.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
int rl,phy,chem,ca,total;
float per;
char nm[20],div[10];
clrscr();
scanf(“%d”,&rl);
scanf(“%s”,nm);
scanf(“%d %d %d”,&phy,&chem,&ca);
total=phy+chem+ca;
per=total/3.0;
if(per>=60)
strcpy(div,”First”);
strcpy(div,”Second”);
strcpy(div,”Pass”);
else
strcpy(div,”Fail”);
getch();
OUTPUT:
ROLL NO.:30
Marks in physics: 80
Marks in chemistry: 90
Percentage= 88.33
Division = First
RESULT:
Date:
AIM:
ALGORITHM:
Step-4: The scanf() function is used to read the user's input and store it in the variable 'tmp'
Step-5:The program uses a series of 'if' and 'else if' statements to determine the temperature range and
prints out the corresponding message for each range.
-If the temperature is less than 0, the program prints "Freezing weather"
-If the temperature is 0 and 10 , the program prints "Very cold weather"
Step-6: If none of the conditions are met , the program does not print any message
#include<stdio.h>
#include<conio.h>
void main()
int tmp;
clrscr();
scanf(“%d”,&tmp);
if(tmp<0)
printf(“\nFreezing weather.”);
else if(tmp<10)
else if(tmp<20)
printf(“\nCold weather.”);
else if(tmp<30)
printf(“\nNormal in temperature.”);
else if(tmp<40)
printf(“\nIts hot.”)
else
getch();
OUTPUT:
RESULT:
Date:
AIM:
To write a c program to take grade as input and providing a corresponding grade as output using switch
case statement.
ALGORITHM:
Step-1: Declare two variable `notes' as character array enough space to store two note.
Step-2: Declare a character variable called `grd' to store the entered grade.
Step-4: Convert the entered grade to upper case using the toupper() function.
Step-5: use a switch statement to check the value of `grd' and assign the corresponding next to the
"notes" Array.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void main()
char notes[15];
char grd;
clrscr();
scanf(“%c”,&grd);
grd=toupper(grd);
switch(grd)
case’E’:
strcpy(notes,”Excellent”);
break;
case’V’:
strcpy(notes,”Very good”);
break;
case’G’:
strcpy(notes,”Good”);
break;
case’A’:
strcpy(notes,”Average”);
break;
case’F’:
strcpy(notes,”Fail”);
break;
default:
break;
getch();
OUTPUT:
RESULT:
Date:
AIM:
ALGORITHM:
Step:5 d=n℅10
Step:6 sum=sum+d
Step:7 n=n/10
#include<stdio.h>
#include<conio.h>
void main()
int n,d,sum=0;
clrscr();
scanf(“%d”,&n);
while(n>0)
d=n%10;
sum=sum+d;
n=n/10;
getch();
OUTPUT:
RESULT:
Date:
AIM:
To write a c program to generate the Fibonacci sequence based on the n number of terms.
ALGORITHM:
Step-1: start with two variable ‘a’ and ‘b' with value 0 and 1 respectively.
Step-2: prompt the user to enter the number of terms, let's call it as n.
Step-3: print the value of 'a' and ' b' as the first two terms of sequence.
-Update the value of 'a' and ' b' by assigning ' b' to 'a' and 'c' to 'b'
#include<stdio.h>
#include<conio.h>
void main()
int a=0,b=1,c,n,I;
clrscr();
scanf(“%d”,&n);
printf(“%d %d”,a,b);
for(i=3;i<=n;i++)
c=a+b;
printf(“%d”,c);
a=b;
b=c;
getch();
OUTPUT:
RESULT:
Date:
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
void main()
int a[30],i,num,largest;
clrscr();
scanf(“%d”,&num);
largest=a[0];
for(i=0;i<num;i++)
if(a[i]>largest)
largest=a[i];
getch();
OUTPUT:
54
34
100
45
RESULT:
Date:
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
int main()
int i,j,mat1[10][10],mat2[10][10],mat3[10][10];
int row1,col1,row2,col2;
clrscr();
scanf(“%d”,&row1);
scanf(“%d”,&col1);
scanf(“%d”,&row2);
scanf(“%d”,&col2);
return 0;
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
{
printf(“\nEnter te Element a[%d][%d]:”,i,j);
scanf(“%d”,&mat1[i][j]);
for(i=0;i<row2;i++)
for(j=0;j<col2;j++)
scanf(“%d”,&mat2[i][j]);
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
mat3[i][j]=mat1[i][j]+mat2[i][j];
for(i=0;i<row1;i++)
for(j=0;j<jcol1;j++)
printf(“%d\t”,mat3[i][j]);
}
Printf(“\n”);
getch();
return 0;
OUTPUT:
4 7
6 7
RESULT:
Date:
AIM:
ALGORITHM:
Step-2: press 1-concatenate,2-compare,3-length of string if press 1 then goto step 3,if press step 2 then
goto step 4,if press 3 then goto step 5.
Step 3: Read the string 1 and string 2 and comparison function by using strcmp built in function is used.
If res=0 the print both string are not equal and then goto the step 4.
Step-4: Read string 1 and string 2 and find concatenation of two string using string handling function.
strcat() and display string and return back to main function.
Step-5: Read string 1 and call function to find the length of string by calling function length.
CODING:
#include<stdio.h>
#include<conio.h>
Void main ( )
Clrscr ( );
Int n , digit;
do
Scanf(“%d”,&n);
Switch(n)
Scanf(“%s”,str1);
Scanf(“%s”,str2);
Break;
scanf(“%s”,str2);
Break;
scanf(“%s”,str1);
Length (str1);
Break;
Break;
Scanf(“%d”,& digit);
} while (digit==0);
Int i;
else
int len;
OUTPUT:
OUTPUT:
Length of string : 6
RESULT:
Date:
AIM:
ALGORITHM:
Step-5:Use a do while loop to repeat the process unit the user decides to stop.
Step-6:Display the menu option,read the user's choice and perform the designed operations based on
the choice using switch statement.
Step-7:Prompt the user if they want to continue,If yes repeat the process,If not exit the program.
#include <stdio.h>
#include <conio.h>
#include <string.h>
Void main( )
int n, digit;
char str1[10],str2[10];
do
scanf("%d",&n);
switch (n)
scanf("%s",str1);
strcpy(str2,str1);
printf("string 2:%s\n",str2);
Break;
scanf("%s",str1);
strcpy(str2,strrev(str1));
Break;
Case 3:printf("enter string :");
scanf("%s",str1);
strcpy(str2,strlwr(str1));
printf("string 2:%s\n",str2);
Break;
scanf("%s",str1);
strcpy(str2,strupr(str1));
printf("string 2:%s\n",str2);
Break;
Default :
printf("wrong choice");
Break;
scanf("%d",&digit);
}while(digit==0);
getch( );
OUTPUT:
press
1-copy
2-reverse
3-lowercase
4-uppercase
Enter your choice:1
enter string:harsh
String 2:harsh
RESULT:
Date:
AIM:
ALGORITHM:
Step-2: declaring the swap Function with two parameter 'a' & 'b'
Step-3: inside the swap function create a temporary variable 'a' & 'b'
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
Int i,j;
clrscr();
scanf(“%d %d”,&i,&j);
swap(i,j);
getch();
int temp;
temp=a;
a=b;
b=temp;
OUTPUT:
Before swapping:7 4
After swapping:4 7
RESULT:
Date:
AIM:
ALGORITHM:
Step-3: Print a message about the unavailability of factorial for negative numbers.
#include<stdio.h>
#include<conio.h>
long factorial(int);
int main()
int num;
long fact;
clrscr();
scanf("%d",num);
if(num<0)
else
fact=factorial(num);
printf("%d!=%d\n",num,fact);
getch();
return 0;
if(num==0)
return 1;
else
return (num*factorial(num-1));
OUTPUT:
5!=120
RESULT:
Date:
AIM:
ALGORITHM :
Step-3:Input the information for each student using a loop from 0 to 3 inside the loop set the roll
number for each student prompt the user to enter the name.read the name using scanf prompt the
user to enter the marks read the marks using scanf.
Step-4:Display the information for each student using a loop from 0 to less than 3 inside the loop print
the roll number ,name,and mark for each student
#include <stdio.h>
#include<conio.h>
struct student
char name[30];
int roll;
int marks;
}s[3];
void main()
int i;
scanf("%d", &s[i].roll);
scanf("%s", s[i].name);
scanf("%d", &s[i].marks);
printf("Displaying information:\n");
{
printf("Roll number: %d\n", s[i].roll);
printf("Name:", s[i].name);
getch();
OUTPUT:
Information of students
Enter marks: 90
Enter marks: 98
Enter marks: 87
Displaying information
Roll number: 1
Name: pinky
Marks: 90
Roll number: 2
Name:ben
Marks: 98
Roll number: 3
Name:rose
Marks: 87
RESULT:
Date:
AIM:
ALGORITHM:
step-4: print the occupied size of the union using 'size of (p)'
step-5: Assign a value "A" to members 'a' of the union 'p' and print its value
Step-6: Assign a value '10' to member 'b' of the union 'p' and print its value
Step-7: Assign a value '12345.6790' to member 'c' of the union 'p' and prints its value
#include <stdio.h>
#include<conio.h>
union pack
char a;
int b;
double c;
};
int main()
union pack p;
clrscr();
p.a='A';
p.b=10;
p.c=12345.6790;
p.a='A';
p.b=10;
p.c=12345.6790;
getch();
}
OUTPUT:
Value of a:A
Value of b:10
Value of c:12345.6790
RESULT:
Date:
AIM:
ALGORITHM:
a, b and c.
Step 3: Declare pointer 'ptra', 'ptr b', 'ptrc' Assign them the addresses of visible 'a', 'b', 'L' .
Step-4: Compare the values pointed by three Pointer to find biggest number, if 'ptra' is greater than
both 'ptrb' and 'ptrc'. then it is The biggest number. if 'ptrb' is greater than both 'ptra'and 'ptrc'.then it is
the biggest number.other wise 'ptrc' is the biggest number
#include <stdio.h>
#include <conio.h>
int main()
int a, b, c;
int *ptra=&a,*ptrb=&b,*ptrc=&c;
clrscr();
if ((*ptra>*ptrb&&*ptra>*ptrc))
else if ((*ptrb>*ptra&&*ptrb>*ptrc))
else
getch();
OUTPUT:
98
78
RESULT:
Date:
AIM:
ALGORITHM:
Step-2:Declare three integer variable ' x ', 'y ' , ' z ' and assign them values of 10,20,30 respectively.
Step-3:Assign three address of 'x', 'y', 'z' the respectively elements of the arrays.
Step-4:Start a loop from 0 to 2 (inclusive) print the value of array [ i ], the value pointer by array [ i ]
and the address stored in array [ i ].
#include<stdio.h>
#include<conio.h>
main()
clrscr();
int*array[3];
int x=10,y=20,z=30;
int i;
array[0]=&x;
array[1]=&y;
array[2]=&z;
getch();
return 0;
OUTPUT:
RESULT:
Date:
AIM:
ALGORITHM:
Step-2:declare a file-FILE*F1.
Step-13:end.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
int I,number;
char c;
FILE*F1;
clrscr();
F1=fopen(“new”,”w”);
for(i=0;i<=20;i++)
scanf(“%d”,&number);
If(number==-1)
break;
putw(number,F1);
fclose(F1);
F1=fopen(“new”,”r”);
while((f1number=getw(F1))!=EOF)
printf9(“%5d”,number);
fclose(F1);
while((number=getw(F1))!=EOF)
if(number%2==0)
printf(“%5d”,number);
fclose(F1);
getch();
OUTPUT:
RESULT:
Date:
AIM:
To create and process student mark list its using file algorithms.
ALGORITHM:
Step-2: Declare a file -FILE*F1 and variable name, rollno, m1,m2,m3,m4,m5, total
Step-3: open a file for writing student details &open ("Data", "w")
Step-6: store student details in name, rollno, m1,m2, m3,m4,m5 using scanf
Step-8: open a file for reading student details fopen(" Data", "r") ;
Step-9: get student details one by one using for loop and mark
Step-10: print the student name, rollno, marks and total mark
Step-11: check the individual subject mark if it below 35 print the result is " FAIL"
#include <stdio.h>
#include <conio.h>
Void main( )
int i,n,rollno,m1,m2,m3,m4,m5,total;
Char name[20];
FILE*F1;
clrscr( );
F1=fopen("data","w");
printf("enter no of students:");
scanf("%d",&n);
scanf("%s%d",name, rollno:");
printf(F1"%s%d",name, rollno);
fprintf(F1"%d%d%d%d%d",m1,m2,m3,m4,m5);
fclose(F1);
printf("\n");
F1=fopen("Data"," ");
for(i=1;i<=n; i++)
fscanf(F1"%s%d",&name, &rollno);
fscanf(F1"%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
Total=m1+m2+m3+m4+m5;
printf("\n");
printf("\n");
printf("\n");
if(m1<35||m2<35||m3<35||m4<35||m5<35)
else
fprintf(stdout"\n result-pass");
printf ("\n");
getch( );
fclose (F1);
}
OUTPUT:
enter no of students :1
Subject 1 82
Subject 2 85
Subject 3 90
Subject 4 94
Subject 5 96
Total : 447
Result – pass.