0% found this document useful (0 votes)
13 views31 pages

C Pro Word

The document contains algorithms and corresponding C programs for various tasks including calculating the sum and average of three numbers, checking if a number is positive or negative, finding the largest among three numbers, and generating Fibonacci series. It also includes programs for sorting arrays, checking for palindromes, generating prime numbers within a range, and implementing built-in string functions. Each section outlines the algorithm followed by the C code implementation and sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views31 pages

C Pro Word

The document contains algorithms and corresponding C programs for various tasks including calculating the sum and average of three numbers, checking if a number is positive or negative, finding the largest among three numbers, and generating Fibonacci series. It also includes programs for sorting arrays, checking for palindromes, generating prime numbers within a range, and implementing built-in string functions. Each section outlines the algorithm followed by the C code implementation and sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Algorithm

1. Start.

2. Read three numbers: a , b , c

3. Calculate the sum: sum = a+b+c

4. Calculate the average: avg=sum/3

5. Display the sum and average.

6. Stop.

Output

Enter the numbers : 4 7 2

Sum of three number is : 13.00

Average of this three numbers : 4.333


1. Program to find sum and average of three numbers

#include<stdio.h>

int main()

float a , b , c , sum , avg ;

printf("Enter the numbers :\n");

scanf("%f %f %f", &a , &b , &c );

sum = a + b + c ;

printf("Sum of three number is : % .2f \n",sum);

avg = sum / 3 ;

printf("Average of this three numbers :% .3f ",avg);

return 0;

}
Algorithm

1. Start.

2. Declare variable to hold the size.

3. Calculate the size of different data types using sizeof() operator.

4. Display the size of different data types.

5. Stop.

Output

Size of int data type is : 4 bytes

Size of float data type is : 4 bytes

Size of char data type is : 1 bytes

Size of double data type is : 8 bytes


2. Program to print the size of all fundamental data types.

#include<stdio.h>

int main()

int a,b,c,d,e;

a=sizeof(int);

b=sizeof(float);

c=sizeof(char);

d=sizeof(double);

printf("size of int data type is : %d bytes\n",a);

printf("size of float data type is : %d bytes\n",b);

printf("size of char data type is : %d bytes\n",c);

printf("size of double data type is : %d bytes\n",d);

return 0;

}
Algorithm

1. Start.

2. Input three numbers: a , b , c

3. Compare the numbers using the conditional (ternary) operator.

4. Display the greatest number.

5. Stop.

Output

Enter the first number

Enter the second number

Enter the third number

The greatest number is : 8


3. Program to find largest among three numbers using conditional operator.

#include<stdio.h>

int main()

int a , b , c , result;

printf("Enter the first number\n");

scanf("%d",&a);

printf("Enter the second number\n");

scanf("%d",&b);

printf("Enter the third number\n");

scanf("%d",&c);

result=(a>b)?(a>c?a:c):(b>c?b:c);

printf("The greatest number is : %d",result);

return 0;

}
Algorithm

1. Start.

2. declare an integer variable a.

3. Read the input value into a.

4. check condition

i. if a > 0

Print the number is positive.

ii. if a < 0

Print the number is negative.

iii. if a == 0

Print to enter a nonzero number.

5. Stop.

output

Enter any number

The number is positive

OR

Enter any number

-6

The number is negative


4. Program to check a number is positive or negative using if statement.

#include<stdio.h>

int main()

int a;

printf("Enter any number \n");

scanf("%d", &a);

if(a>0)

printf("The number is positive");

if(a<0)

printf("The number is negative \n");

if(a==0)

printf("Please enter a non-zero number");

return 0;

}
Algorithm

1. Start.

2. Declare an integer variable.

3. Prompt the user to enter the mark.

4. Read the mark.

5. Check the conditions to determine the grade

i. if mark>=90 Print ”Grade A”.

ii. if mark>=80 Print ”Grade B”.

iii. if mark>=70 Print ”Grade C”.

iv. if mark>=60 Print ”Grade D”.

v. else Print ”Failed”

6. Stop.

Output

Enter the marks :

56

You are failed

Enter the marks :

98

The grade is A

Enter the marks :

79

The grade is B

Enter the marks :

65

The grade is C

Enter the marks :

57

The grade is D
5. Program to print the grade of a student using nested if.

#include<stdio.h>

int main()
{
int mark;
printf("Enter the marks : \n");
scanf("%d",&mark);
if(mark>=90)
printf("The grade is A");
else
{
if(mark>=80)
printf("The grade is B");
else
{
if(mark>=70)
printf("The grade is C");
else
{
if(mark>=60)
printf("The grade is D");
else
printf("You are failed");
}
}
}
return 0;
}
Algorithm

1. Start.

2. Declare variables num1, num2, result.

3. Read two numbers.

4. Display the list of operations.

1. Addition

2. Subtration

3. Multiplication.

4. Division.

5. Modulus.

5. Read the user choice.

6. Use switch statement to perform the corresponding operation .

Case 1: Calculate the sum as num1+num2.

Case 2: Calculate the difference as num1-num2.

Case 3: Calculate the product as num1*num2.

Case 4: Calculate the quotient as num1/num2;

Case 5: Calculate the remainder as num1%num2.

Default: Display error message “wrong choice”.

7. Display the result.

8.End

Output

Enter two numbers

3 6

The operations are

1. Addition

2. Subtraction

3. Division

4. Multiplication

5. Modulus

Enter your choice 1

Answer = 9
6. Program to perform arithmetic operations using switch statement.

#include <stdio.h>

int main()

int num1, num2,n,result;

printf(" enter two numberss \n");

scanf ("%d %d", &num1,&num2);

printf("The operations are\n");

printf(" 1.Addition\n 2.Subtraction\n 3.Division \n 4.Multiplication \n 5.Modulus\n");

printf("Enter your choice");

scanf("%d",&n);

switch(n) {

case 1: result=num1+num2;

printf("Answer = %d", result);

break;

case 2: result=num1-num2;

printf("Answer = %d", result);

break;

case 3: result=num1*num2;

printf("Answer = %d", result);

break;

case 4: result=num1/num2;

printf("Answer = %d", result);

break;

case 5: result=num1%num2;

printf("Answer = %d", result);

break;

default: printf("Wrong choice");

break;

}}
Algorithm

1. Start.

2. Read the coefficients a, b and c.

3. Calculate the discriminant d=b*b - 4ac.

4. Determine the roots as

If d>0: The equation has two distinct roots

Root1=(-b+sqrt(d))/2a

Root2=(-b-sqrt(d))/2a

If d=0: The equation has one real root

Root1= -b/2a

If d<0: The equation has imaginary root.

5. Display the roots.

6. End

Output

Enter the values for a,b,c

-4

The rests are real and same 2.000000 2.000000


7. Program to find roots of a quadratic equation.

#include<stdio.h>

#include<math.h>

int main()

int a,b,c,desc;

float root1, root2;

printf("Enter the values for a,b,c");

scanf("%d %d %d",&a,&b,&c);

desc=b*b-4*a*c;

if(desc>0)

root1=-b+sqrt(desc)/2*a;

root2=-b-sqrt(desc)/2*a;

printf("The roots are real and different: %f %f", root1, root2);

else if(desc==0)

root1=root2=-b/2*a;

printf("The roots are real and same %f %f",root1,root2);

else

printf("Roots are imaginary");

}
Alrorithm

1. Start.

2. Read an integer n.

3. Set fact=1.

4. For I from 2 to n do

Fact=fact*I;

5. Display the value of fact.

6. End.

Output

Enter an integer: 5

Factorial of 5 = 120
8. Program to find factorial of a given number.

#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

for (i = 1; i <= n; ++i) {

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

}
Algorithm

1. Start.

2. Set a = 0 and b = 1.

3. Read n (the number of terms) to generate the Fibonacci series.

4. Print First two numbers.

5. For i from 3 to n

i. Calculate c = a + b

ii. Print c

iii. Update a = b

iv. Update b = c

6. End.

output

Enter the limit 8

The series is: 0 1 1 2 3 5 8 13


9. Program to generate Fibonacci series.

#include <stdio.h>

int main() {

int i,n,a=0,b=1,next;

printf("enter the limit ");

scanf("%d",&n);

printf("the series is: ");

printf("%d %d",a,b);

for(i=2;i<n;i++)

next=a+b;

a=b;

b=next;

printf(" %d ", next);

return 0;

}
Algorithm

1. Start.

2. Read the integer n, representing the number of elements.

3. Create an array arr of size n.

4. Read array elements.

5. Set sum = 0

6. For each index I from 0 to n-1, do

Add arr[i] to sum.

7. Display the sum.

8. End.

output

Enter the number of elements: 5

Enter 5 numbers:

Sum of the numbers: 25


10. Program to find sum of n numbers using array.

#include <stdio.h>

int main() {

int n, i, sum = 0;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d numbers:\n", n );

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

sum += arr[i];

printf("Sum of the numbers: %d\n", sum);

return 0;

}
Algorithm

1. Start.

2. Read the size of array.

3. Read the array elements.

4. Compare the adjacent elements

5.if a[i]<a[i+1]

Swap them

6. Repeat step 4 and 5 until end of the array.

7. Print the array elements.

8. End.

Output

Enter the limit 5

Enter 5 numbers

The sorted array in ascending order:

6
11. Program to sort n numbers using array.

#include <stdio.h>

int main()

int i, j, a, n, array [30];

printf("Enter the limit \n");

scanf("%d", &n);

printf("Enter %d numbers\n",n);

for(i = 0; i < n; ++i)

scanf("%d", &array[i]);

for(i = 0; i < n; ++i)

for(j=i+1; j<n; ++j)

if(array[i]>array[j])

a = array[i];

array[i]=array[j];

array[j] = a;

printf("The sorted array in ascending order: \n");

for (i = 0; i < n; ++i)

printf("%d\n", array[i]);

return 0;

}
Algorithm

1. Start.

2. Read the string s.

3. Set flag = 0

4. Determine the length of the string.

5. For I from 0 to length/2 -1:

If str[i] is not equal to str[length-i-1]:

i. Set flag = 1

ii. Break the loop

6. If flag is 1,

Print “The string str is not a palindrome”

Else

Print “The string str is a palindrome”

7.End

Output

enter the string mam

The string mam is not palindrome

enter the string sam

The string sam is not palindrome


12. Program to check a given string is palindrome or not.

#include<stdio.h>

#include <string.h>

int main() {

char str[20];

int i, length, flag=0;

printf("enter the string ");

scanf("%s", str);

length=strlen(str);

for(i = 0; i < length/2; i++)

if(str[i] != str[length-i-1])

flag=1;

break;

if(flag)

printf("The string %s is not palindrome",str);

else

printf("The string %s is palindrome",str);

return 0;

}
Algorithm

1. Start.

2. Read num

3. Check if num is divisible by any number from 2 to num-1.

4. If it is divisible, then num is not prime.

5. Else num is prime.

6. print the prime numbers.

7. End.

Output

Enter the range:

15

The prime numbers in between the range 1 to 15 : 2 3 5 7 11 13


13. Program to generate prime numbers with in a range.

#include<stdio.h>

int main()

Int i, num, n, count;

printf ("Enter the range:\n ");

scanf ("%d", &n);

printf ("\n The prime numbers in between the range 1 to %d : " , n);

for (num = 1; num <=n; num ++) {

count = 0;

for (i=2; i<=num/2;i++)

if (num%i==0) {

count++;

break;

If (count==0 && num! = 1)

printf("%d ",num);

return 0;

}
Algorithm:

1. Start.

2. Create two strings str1 and str2.

3. Calculate the lengh of the string using strlen() and print.

4. Copy string str1 to str2 using strcpy().

5. Print str2 after copying.

6. Join the string str1 and “MANJESHWARAM” using strcat().

7. Print the string after concatenation.

8. compare the strings str1and str2 using strcmp().

9.print the result of string comparison

10. Stop.

Output:

Length of str1 is : 27

After copying

str2: COLLEGE OF APPLIED SCIENCE

After concatenation

str2: COLLEGE OF APPLIED SCIENCE MANJESHWARAM

COLLEGE OF APPLIED SCIENCE is less than COLLEGE OF APPLIED SCIENCE MANJESHWARAM


14. Program to implement any five built-in string function.

#include <stdio.h>

#include <string.h>

int main() {

char str1[] = "COLLEGE OF APPLIED SCIENCE";

char str2[40]=" ";

printf("Length of str1 is : %d\n", strlen(str1));

strcpy(str2, str1);

printf("After copying\n str2: %s\n", str2);

strcat(str2, " MANJESHWARAM");

printf("After concatenation\n str2: %s\n", str2);

int result = strcmp(str1, str2);

if (result == 0)

printf("%s is equal to %s\n",str1,str2);

else if (result < 0)

printf("%s is less than %s\n",str1,str2);

else

printf("%s is greater than %s\n",str1,str2);

return 0;

}
Algorithm:

1. Start.

2. Read the number of rows and columns.

3. Read the elements of two matrices.

4. Calculate the sum of two matrices.

5. Print the sum of matrices.

6. Stop.

Output:

enter the number of row : 2

enter the number of columns : 2

Enter element of ist matrix :

Enter elements a[1 1] : 1

Enter elements a[1 2] : 2

Enter elements a[2 1] : 3

Enter elements a[2 2] : 4

Enter element of 2nd matrix:

Enter elements b[1 1] : 1

Enter elements b[1 2] : 2

Enter elements b[2 1] : 3

Enter elements b[2 2] : 4

sum of two matrices:

2 4

6 8
15. Program to perform any matrix operation.

#include<stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("enter the number of row : ");
scanf("%d",&r);
printf("enter the number of columns : ");
scanf("%d",&c);
printf("\nEnter element of 1st matrix : \n");
for ( i = 0 ; i<r ; ++i)
for( j = 0 ;j<c; ++j)
{
printf("Enter elements a[%d %d] : ", i + 1 ,j + 1 ) ;
scanf("%d",&a[i][j]);
}
printf("Enter element of 2nd matrix: \n");
for (i = 0 ; i<r; ++i)
for( j = 0 ; j<c; ++j)
{
printf("Enter elements b[%d %d] : ", i + 1 , j + 1 ) ;
scanf("%d",&b[i][j]);
}
for (i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
printf("\nsum of two matrices: \n");
for ( i = 0 ;i<r; ++i)
for ( j = 0 ;j<c; ++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
printf("\n");
}
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy