0% found this document useful (0 votes)
19 views30 pages

C Pro Word - Organized

The document contains a series of C programming examples that demonstrate various basic programming concepts, including arithmetic operations, conditional statements, loops, and array manipulations. Each example includes code snippets along with sample inputs and outputs, covering topics like finding sums, averages, factorials, and generating Fibonacci series. Additionally, it showcases string manipulation, prime number generation, and matrix operations.
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)
19 views30 pages

C Pro Word - Organized

The document contains a series of C programming examples that demonstrate various basic programming concepts, including arithmetic operations, conditional statements, loops, and array manipulations. Each example includes code snippets along with sample inputs and outputs, covering topics like finding sums, averages, factorials, and generating Fibonacci series. Additionally, it showcases string manipulation, prime number generation, and matrix operations.
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/ 30

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;

}
Output

Enter the numbers : 4 7 2

Sum of three number is : 13.00

Average of this three numbers : 4.333


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;

}
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


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;

}
Output

Enter the first number

Enter the second number

Enter the third number

The greatest number is : 8


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;

}
output

Enter any number

The number is positive

OR

Enter any number

-6

The number is negative


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;
}
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
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;

}}
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
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");

}
Output

Enter the values for a,b,c

-4

The rests are real and same 2.000000 2.000000


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;

}
Output

Enter an integer: 5

Factorial of 5 = 120
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;

}
output

Enter the limit 8

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


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;

}
output

Enter the number of elements: 5

Enter 5 numbers:

Sum of the numbers: 25


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;

}
Output

Enter the limit 5

Enter 5 numbers

The sorted array in ascending order:

6
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;

}
Output

enter the string mam

The string mam is not palindrome

enter the string sam

The string sam is not palindrome


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;

}
Output

Enter the range:

15

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


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

#include <stdio.h>

#include <string.h>

int main() {

char str1[] = " SREE NARAYANA COLLEGE OF MANAGEMENT STUDIES ";

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, " PERIYA");

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;

}
Output:

Length of str1 is : 27

After copying

str2: SREE NARAYANA COLLEGE OF MANAGEMENT STUDIES

After concatenation

str2: SREE NARAYANA COLLEGE OF MANAGEMENT STUDIES PERIYA

SREE NARAYANA COLLEGE OF MANAGEMENT STUDIES is less than SREE NARAYANA COLLEGE OF
MANAGEMENT STUDIES PERIYA
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;

}
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

You might also like