C Program
C Program
AIM:
Write a C program to find largest among three numbers.
ALGORITHM:
Step1: Start
Step2: Read the three integer values in A, B, and C (integer
variables).
Step 3: Check if A is greater than B.
Step 4: If true, then check if A is greater than C.
Step 5: If true, then print ‘A’ as the greatest number.
Step 6: If false, then print ‘C’ as the greatest number.
Step 7: If false, then check if B is greater than C.
Step 8: If true, then print ‘B’ as the greatest number.
Step 9: If false, then print ‘C’ as the greatest number.
Step10: Stop
PROGRAM:
#include <stdio.h>
int main()
{
int A, B, C;
printf(" Enter the number1 = ");
scanf("%d", &A);
printf("\n Enter the number2 = ");
scanf("%d", &B);
printf("\n Enter the number3 = ");
scanf("%d", &C);
if (A > B){
if (A > C){
printf("\n Largest number = %d\n",A);}
else{
printf("\n Largest number = %d \n",C);}}
else if (B > C){
printf("\n Largest number = %d \n",B);}
else{
printf("\n Largest number = %d \n",C);}}
SUM OF DIGITS OF A NUMBER
AIM:
Write a C program to find the sum of individual digits
of a positive number.
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize sum ← 0
Step 4: while(n!=0)
Step 5: r←n%10
Step 6: sum←sum+r
Step 7: n←n/10
Step 8: Print “sum”
Step 9: Stop
PROGRAM:
/*This is a program to find sum of digits*/
#include<stdio.h>
void main()
{
int n,r,sum=0;
printf("ENTER A POSITIVE INTEGER \n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("THE SUM OF INDIVIDUAL
DIGITS OF A POSITIVE
INTEGER IS..%d\n",sum);
}
FIBONACCI SERIES
AIM:
Write a C program to generate the first n terms of the Fibonacci
sequence.
ALGORITHM:
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize f0 ← 0, f1 ← 1, f ← 0
Step 4 :i=0
Step 5 : while(i<=n) do as follows
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
Step 6: If not go to step 7
Step 7: stop
PROGRAM:
#include<stdio.h>
void main()
{
int f0,f1,f,n,i;
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("FIBONACCI SEQUENCE FOR THE FIRST %d
TERMS:\n",n);
i=0;
while (i<n)
{
printf("%d\t",f0); f=f0+f1;
f0=f1;
f1=f;
i=i+1;
}}
BINARY SEARCH
AIM:Write a C program to Implement Binary Search.
ALGORITHM:
Step 1: start
Step 2 : Initialize array a
Step 3 : Read n
Step 4 : Set i=0 repeat step 5,6 until i<n.
Step 5 : Read a[i]
Step 6 : i++
Step 7 : Read s
Step 8 : set first = 0, last = n – 1, middle = (first+last)/2
Step 9 : Repeat step 10 to 15 until first <= last
Step 10 : Check if (a[middle] < s)
Step 11 : Then, Set first = middle + 1
Step 12 : Else if (a[middle] == s) then
Step 13 : Print s and middle+1
Step 14 : Else then last = middle – 1
Step 15 : middle = (first + last)/2
Step 16 : if (first > last) then
Step 17 : Print Not found
Step 19 : Stop
PROGRAM:
#include <stdio.h>
int main()
{
int i, first, last, middle, n, s, a[100];
printf("Enter number of elements in ascending order\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter value to find\n");
scanf("%d", &s);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (a[middle] < s)
first = middle + 1;
else if (a[middle] == s)
{
printf("%d found at location %d.\n", s, middle+1);
break;}
else
last = middle - 1;
middle = (first + last)/2;}
if (first > last)
printf("Not found! %d isn't present in the list.\n", s);
return 0;}
STUDENTS LIST UNSING STRUCTURE
AIM:
Write a C program to implements students list
using structure.
ALGORITHM:
Step 1: Start
Step 2: Initalize char name[50],section, int class
Step 3: Use the function
strcpy(student1.name,”student_name”);
Step 4: Assign student1.class1 and
student1.sectionA
Step 5: Print
student1.name,student1.class,student1.section
Step 6: Stop
PROGRAM:
#include <stdio.h>
#include <string.h>
struct Student
{
char name[50];
int class;
char section;
};
int main()
{
// created variable student1 for structure Student
struct Student student1;
// accessing student1 member and initializing them
strcpy(student1.name,"Student_name");
student1.class = 1;
student1.section = 'A';
// printing values
printf( "Student Name : %s\n",student1.name);
printf( "Student Class : %d\n",student1.class);
printf( "Student Section : %c\
n",student1.section);
return 0;
}