100% found this document useful (1 vote)
62 views52 pages

C Program Rcu

C program rcu

Uploaded by

hohojob512
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
62 views52 pages

C Program Rcu

C program rcu

Uploaded by

hohojob512
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 52

C PROGRAMMING LAB

PART A
1.Program to read radius of a circle and to find area and circumference
#include<stdio.h>
void main()
{
float radius, area,circumference;
printf("\nEnter the radius of Circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circumference=2*3.14*radius;
printf("\nArea of Circle : %f", area);
printf("\nCircumference of Circle : %f", circumference);
}

PTES BCA, BELGAUM Page 1


C PROGRAMMING LAB
OUTPUT:
Enter the radius of Circle : 5

Area of Circle : 78.500000


Circumference of Circle : 31.40000

PTES BCA, BELGAUM Page 2


C PROGRAMMING LAB
2.Program to read three numbers and find the biggest of three
#include <stdio.h>
void main()
{
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);

if (n1 >= n2 && n1 >= n3)


printf("%d is the largest number.", n1);

else if (n2 >= n1 && n2 >= n3)


printf("%d is the largest number.", n2);

else
printf("%d is the largest number.", n3);

PTES BCA, BELGAUM Page 3


C PROGRAMMING LAB
OUTPUT:
Enter three numbers: 10
5
25
25 is the largest number.

PTES BCA, BELGAUM Page 4


C PROGRAMMING LAB
3.C program to demonstrate library functions of math.h
#include <stdio.h>
#include <math.h>

void main ()
{
float a=1.8;
float x=100;
printf ("ceil = %lf\n", ceil(a));
printf("floor = %lf\n", floor(a));
printf("The absolute value of %f is %lf\n", a, fabs(a));
printf("log10(%f) = %lf\n", x, log10(x));
printf("Square root of %lf is %lf\n", x, sqrt(x) );
printf("Value 5.0 ^ 3 = %lf\n", pow(5, 3));
printf("The cosine of %lf is %lf degrees\n", x, cos(x));
}

PTES BCA, BELGAUM Page 5


C PROGRAMMING LAB
OUTPUT:
ceil = 2.000000
floor = 1.000000
The absolute value of 1.800000 is 1.800000
log10(100.000000) = 2.000000
Square root of 100.000000 is 10.000000
Value 8.0 ^ 3 = 125.000000
The cosine of 100.000000 is 0.862319 degrees

PTES BCA, BELGAUM Page 6


C PROGRAMMING LAB
4.C Program to generate the factorial of a given number
#include<stdio.h>
void main ()
{
int n, fact = 1;
printf ("Enter a number\n");
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
printf ("The Factorial of %d is %d", n, fact);
}

PTES BCA, BELGAUM Page 7


C PROGRAMMING LAB
OUTPUT:
Enter a number
5
The Factorial of 5 is 120

PTES BCA, BELGAUM Page 8


C PROGRAMMING LAB
5.Program to generate n fibonacii sequence
#include<stdio.h>
void main ()
{
int n1 = 0, n2 = 1, n3, i, number;
printf ("Enter the number of elements:\n");
scanf ("%d", &number);
printf ("\n%d\n%d\n", n1, n2);
for (i = 2; i < number; ++i)
{
n3 = n1 + n2;
printf ("%d\n", n3);
n1 = n2;
n2 = n3;
}
}

PTES BCA, BELGAUM Page 9


C PROGRAMMING LAB
OUTPUT:
Enter the number of elements:
5

0
1
1
2
3

PTES BCA, BELGAUM Page 10


C PROGRAMMING LAB
6.Program to read a number, find the sum of the digits, reverse the number and
check it for palindrome
#include<stdio.h>
void main ()
{
int n, num, digit, rev = 0, sum = 0;
printf ("enter one number\n");
scanf ("%d", &n);
num = n;
while (n != 0)
{
digit = n % 10;
sum += digit;
rev = rev * 10 + digit;
n = n / 10;
}
printf ("Sum of digits of %d is %d\n", num, sum);
printf ("reverse of %d is %d\n", num, rev);
if (num == rev)
printf ("%d is palindrome", num);
else
printf ("%d is not palindrome", num);
}

PTES BCA, BELGAUM Page 11


C PROGRAMMING LAB
OUTPUT:
enter one number
121
Sum of digits of 121 is 4
reverse of 121 is 121
121 is palindrome

enter one number


122
Sum of digits of 122 is 5
reverse of 122 is 221
122 is not palindrome

PTES BCA, BELGAUM Page 12


C PROGRAMMING LAB
7.Program to read numbers from keyboard continuously till the user presses 999
and to find the sum of only positive numbers
#include<stdio.h>
void main()
{
int num,sum=0;
printf("Enter numbers to :\n");
while(1)
{
scanf("%d",&num);
if(num==999)
break;
else
{
if(num>0)
sum+=num;
}
}
printf("Sum of positive numbers is %d",sum);
}

PTES BCA, BELGAUM Page 13


C PROGRAMMING LAB
OUTPUT:
Enter numbers to :
2
4
6
-1
3
-2
999
Sum of positive numbers is 15

PTES BCA, BELGAUM Page 14


C PROGRAMMING LAB
8.Program to read percentage of marks and to display appropriate message
(demonstration of switch Case statement)
#include<Stdio.h>
void main()
{
int score;
printf("Enter score( 0-100 ): ");
scanf("%d", &score);
switch( score / 10 )
{
case 10:
case 9:
printf("Grade: A++");
break;
case 8:
printf("Grade: A");
break;
case 7:
printf("Grade: B");
break;
case 6:
printf("Grade: C");
break;
case 5:
printf("Grade: D");

PTES BCA, BELGAUM Page 15


C PROGRAMMING LAB
break;
case 4:
printf("Grade: E");
break;
default:
printf("Grade: F");
break;
}
}

PTES BCA, BELGAUM Page 16


C PROGRAMMING LAB
OUTPUT:
Enter score( 0-100 ): 97
Grade: A++

Enter score( 0-100 ): 76


Grade: B

PTES BCA, BELGAUM Page 17


C PROGRAMMING LAB
9.Program to find the roots of quadratic equation (Demonstration of else-if ladder)
#include<stdio.h>
#include<math.h>
void main ()
{
float a,b,c,r1,r2,d;
printf ("enter the values of a b c\n");
scanf ("%f%f%f",&a,&b,&c);
d= b*b-4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}
else if (d==0){
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("roots are equal =%f %f", r1, r2);
}
else
printf("Roots are imaginary");

PTES BCA, BELGAUM Page 18


C PROGRAMMING LAB
OUTPUT:
enter the values of a b c
1
-7
10
The real roots = 5.000000 2.000000
enter the values of a b c

5
10
5
roots are equal =-1.000000 -1.000000

PTES BCA, BELGAUM Page 19


C PROGRAMMING LAB
10.Program to read marks scored by a students and find the average of marks
#include<stdio.h>
void main()
{
int i;
float mark[5], sum=0, avg;
printf("Enter Marks obtained in 5 Subjects:\n");
for(i=0; i<5; i++)
{
scanf("%f", &mark[i]);
sum = sum+mark[i];
}
avg = sum/5;
printf("\nAverage Marks = %f", avg);
}

PTES BCA, BELGAUM Page 20


C PROGRAMMING LAB
OUTPUT:
Enter Marks obtained in 5 Subjects:
24
26
27
28
29

Average Marks = 26.799999

PTES BCA, BELGAUM Page 21


C PROGRAMMING LAB
11.Program to remove Duplicate Element in a single dimensional Array

#include <stdio.h>
void main ()
{
int arr[20], i, j, k, size;

printf (" Define the number of elements in an array: ");


scanf (" %d", &size);

printf (" \n Enter %d elements of an array: \n ", size);


for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);
}
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
if ( arr[i] == arr[j])
{
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}

PTES BCA, BELGAUM Page 22


C PROGRAMMING LAB
size--;
j--;
}
}
}

printf (" \n Array elements after deletion of the duplicate elements: ");
for ( i = 0; i < size; i++)
{
printf (" %d \t", arr[i]);
}
}

PTES BCA, BELGAUM Page 23


C PROGRAMMING LAB
OUTPUT:
Define the number of elements in an array: 5

Enter 5 elements of an array:


3
4
2
3
1
Array elements after deletion of the duplicate elements: 3 4 2 1

PTES BCA, BELGAUM Page 24


C PROGRAMMING LAB
Part B
1.Program to Swap Two Numbers
#include<stdio.h>
void main()
{
int A,B, temp;
printf("Enter value of A ");
scanf("%d", &A);
printf("Enter value of B ");
scanf("%d", &B);
printf("\nBefore swapping A= %d B= %d\n\n",A,B);
temp = A;

A = B;

B = temp;

printf("After swapping A= %d B= %d\n", A,B);


}

PTES BCA, BELGAUM Page 25


C PROGRAMMING LAB
OUTPUT:
Enter value of A 5
Enter value of B 8

Before swapping A= 5 B= 8

After swapping A= 8 B= 5

PTES BCA, BELGAUM Page 26


C PROGRAMMING LAB
2.Program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters.

#include<stdio.h>
void main()
{
char str[20];
int i, vowels, consonants, digits, spaces, symbols;
vowels = consonants = digits = spaces = symbols = 0;
printf("Enter the string\n");
gets(str);
for(i=0; str[i]!='\0';i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
vowels++;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
consonants++;
}
else if(str[i]>='0' && str[i]<='9')

PTES BCA, BELGAUM Page 27


C PROGRAMMING LAB
{
digits++;
}
else if (str[i]==' ')
{
spaces++;
}
else
{
symbols++;
}
}
printf("\nVowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
printf("\nSymbols: %d", symbols);

PTES BCA, BELGAUM Page 28


C PROGRAMMING LAB
OUTPUT:
Enter the string
aeivc 34@#
Vowels: 3
Consonants: 2
Digits: 2
White spaces: 1
Symbols: 2

PTES BCA, BELGAUM Page 29


C PROGRAMMING LAB
3. Program to Reverse a string without using built in function.

#include <stdio.h>
void main()
{
char str[10], Rev[10];
int i, j=0,count=0;
printf("\n Please Enter any String :");
scanf("%s",str);

while (str[count] != '\0')


count++;
for (i = count-1; i >= 0; i--)
{
Rev[j++] = str[i];
}
Rev[j] = '\0';
printf("\n String after Reversing = %s", Rev);

PTES BCA, BELGAUM Page 30


C PROGRAMMING LAB
OUTPUT:
Please Enter any String :ptes

String after Reversing = setp

PTES BCA, BELGAUM Page 31


C PROGRAMMING LAB
4. Program to find the length of a string without using built in function.

#include <stdio.h>
void main()
{
char str[10];
int count=0;
printf("\n Please Enter any String :");
gets(str);

while (str[count] != '\0')


count++;
printf("\n String length = %d", count);

PTES BCA, BELGAUM Page 32


C PROGRAMMING LAB
OUTPUT:
Please Enter any String :ptes bca

String length = 8

PTES BCA, BELGAUM Page 33


C PROGRAMMING LAB
5. Program to demonstrate string functions.

#include <stdio.h>
#include <string.h>

void main()
{
char str1[20] = "Peopletree",str2[20]="BCA";
printf("Length of string %s is: %ld\n",str1,strlen(str1));
printf("Length of string %s is: %ld\n",str2,strlen(str2));
printf("First string after concatenation: %s\n", strcat(str1,str2));
printf("First string after copying: %s\n", strcpy(str1,str2));

if (strcmp(str1, str2) ==0)


printf("string 1 and string 2 are equal\n");
else
printf("string 1 and 2 are not equal\n");
}

PTES BCA, BELGAUM Page 34


C PROGRAMMING LAB
OUTPUT:
Length of string Peopletree is: 10
Length of string BCA is: 3
First string after concatenation: PeopletreeBCA
First string after copying: BCA
string 1 and string 2 are equal

PTES BCA, BELGAUM Page 35


C PROGRAMMING LAB
6. Program to read, display and to find the trace of a square matrix.

#include<stdio.h>
void main( )
{
int a[10][10], m,i,j, sum;
printf ("\n Enter order of the square matrix :") ;
scanf ("%d", &m);
printf ("\n Enter the matrix \n");
for( i=0; i<m;i++)
for ( j=0; j<m; j++)
scanf ("%d", &a[i ][ j ]);
printf("\nGiven matrix is\n");
for( i=0; i<m;i++)
{
for ( j=0; j<m; j++)
printf("%d\t", a[i ][ j ]);
printf("\n");
}
sum = 0;
for ( i=0; i<m; i++)
sum = sum + a[i ][ i ];
printf ("\n trace of the matrix = %d", sum);
}

PTES BCA, BELGAUM Page 36


C PROGRAMMING LAB
OUTPUT:
Enter order of the square matrix :3

Enter the matrix


1
2
3
4
5
6
7
8
9

Given matrix is
1 2 3
4 5 6
7 8 9

trace of the matrix = 15

PTES BCA, BELGAUM Page 37


C PROGRAMMING LAB
7. Program to perform addition and subtraction of Matrices.

#include<stdio.h>

void main()
{
int m, n, i, j, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix\n");


for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);

printf("Enter the elements of second matrix\n");


for (i = 0; i < m; i++)
for (j = 0 ; j < n; j++)
scanf("%d", &second[i][j]);

printf("Addition of entered matrices:-\n");


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

PTES BCA, BELGAUM Page 38


C PROGRAMMING LAB
sum[i][j] = first[i][j] + second[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}

printf("Subtraction of entered matrices:-\n");


for (i = 0; i < m; i++)
{
for (j = 0 ; j < n; j++)
{
sum[i][j] = first[i][j] - second[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
}

PTES BCA, BELGAUM Page 39


C PROGRAMMING LAB
OUTPUT:
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
2
2
2
2
Enter the elements of second matrix
1
1
1
1
Addition of entered matrices:-
3 3
3 3
Subtraction of entered matrices:-
1 1
1 1

PTES BCA, BELGAUM Page 40


C PROGRAMMING LAB
8. Program to read, display and multiply two m x n matrices using functions.
#include <stdio.h>

// function to get matrix elements entered by the user


void read(int matrix[10][10], int row, int column)
{
printf("\nEnter elements: \n");
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
scanf("%d", &matrix[i][j]);
}
}
}
void multiply(int fir[10][10],int sec[10][10],int res[10][10],int r1, int c1, int r2, int
c2)
{
// Multiplying first and second matrices and storing it in result
for (int i = 0; i < r1; ++i)
{
for (int j = 0; j < c2; ++j)
{
for (int k = 0; k < c1; ++k)
{

PTES BCA, BELGAUM Page 41


C PROGRAMMING LAB
res[i][j] += fir[i][k] * sec[k][j];
}
}
}
}
void display(int res[][10], int row, int column)
{
printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
printf("%d ", res[i][j]);
}
printf("\n");
}
}
void main()
{
int first[10][10], second[10][10], result[10][10]={0}, r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);

PTES BCA, BELGAUM Page 42


C PROGRAMMING LAB
read(first, r1, c1);

// get elements of the second matrix


read(second, r2, c2);

// multiply two matrices.


multiply(first, second, result, r1, c1, r2, c2);

// display the result


display(result, r1, c2);

PTES BCA, BELGAUM Page 43


C PROGRAMMING LAB
OUTPUT:
Enter rows and column for the first matrix: 2 2
Enter rows and column for the second matrix: 2 2

Enter elements:
2222

Enter elements:
2222

Output Matrix:
8 8
8 8

PTES BCA, BELGAUM Page 44


C PROGRAMMING LAB
9. Program to check a number for prime by defining isprime( ) function.

#include <stdio.h>
int isprime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
void main()
{
int num,res=0;
printf("Enter a number: ");
scanf("%d",&num);

if(isprime(num))
printf("\n%d is a prime number",num);
else
printf("\n%d is not a prime number",num);
}

PTES BCA, BELGAUM Page 45


C PROGRAMMING LAB
OUTPUT:
Enter a number: 12
12 is not a prime number

Enter a number: 13
13 is a prime number

PTES BCA, BELGAUM Page 46


C PROGRAMMING LAB
10. Program to demonstrate student structure to read & display records of n
students.

include <stdio.h>
struct student
{
char firstName[50];
int roll;
int marks;
} s[10];

void main()
{
int i,n;
printf("Enter no. of students:");
scanf("%d",&n);
printf("\nEnter information of students:\n");

// storing information
for (i = 0; i < n; i++)
{
printf("Enter the roll no. : ");
scanf("%d",&s[i].roll);
printf("Enter name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");

PTES BCA, BELGAUM Page 47


C PROGRAMMING LAB
scanf("%d", &s[i].marks);
}
printf("\n\nDisplaying Information:");

// displaying information
for (i = 0; i < n; i++)
{
printf("\nRoll number: %d\n",s[i].roll);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %d", s[i].marks);
printf("\n");
}

PTES BCA, BELGAUM Page 48


C PROGRAMMING LAB
OUTPUT:
Enter no. of students:2

Enter information of students:


Enter the roll no. : 1
Enter name: aman
Enter marks: 17
Enter the roll no. : 2
Enter name: amit
Enter marks: 20

Displaying Information:
Roll number: 1
First name: aman
Marks: 17

Roll number: 2
First name: amit
Marks: 20

PTES BCA, BELGAUM Page 49


C PROGRAMMING LAB
11. Program to demonstrate the difference between structure & union.
#include <stdio.h>

struct Employee1
{
char emp_name[20];
int emp_no;
float salary;
};

union Employee2
{
char emp_name[20];
int emp_no;
float salary;
};

void main()
{
struct Employee1 s;
union Employee2 u;
printf("Size of structure =%ld\n",sizeof(s));
printf("Size of union =%ld\n",sizeof(u));
printf("\n Enter the name ,number and salary of employee1\n");
scanf("%s %d %f",s.emp_name,&s.emp_no,&s.salary);

PTES BCA, BELGAUM Page 50


C PROGRAMMING LAB
printf("\n Employee1 name =%s\n",s.emp_name);
printf("Employee1 number =%d\n",s.emp_no);
printf("Employee1 salary =%f\n",s.salary);
printf("\n\n enter name of employee2 =");
scanf("%s",u.emp_name);
printf("Employee2 name =%s\n",u.emp_name);
printf("\nenter number of employee2 =");
scanf("%d",&u.emp_no);
printf("Employee2 number =%d",u.emp_no);
printf("\n enter salary of employee2 =");
scanf("%f",&u.salary);
printf("\n Employee2 salary =%f",u.salary);
}

PTES BCA, BELGAUM Page 51


C PROGRAMMING LAB
OUTPUT:
Size of structure =28
Size of union =20

Enter the name ,number and salary of employee1


amit 1 10000

Employee1 name =amit


Employee1 number =1
Employee1 salary =10000.000000

enter name of employee2 =aman


Employee2 name =aman

enter number of employee2 =2


Employee2 number =2
enter salary of employee2 =12000

Employee2 salary =12000.000000

PTES BCA, BELGAUM Page 52

You might also like