Assignment On:: C Programming Language Course Code: CSEC 122
Assignment On:: C Programming Language Course Code: CSEC 122
C Programming Language
Course Title: Programming Language and Application l Sessional.
Course Code: CSEC 122
Submitted To:
MOHAMMAD ALAUDDIN
Lecturer,
Department of CSE
Uttara University.
Submitted By:
Shamima Khanum
ID: 2202081010
Department of CSE
Uttara University.
1. Program to find the sum of two float numbers using function
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf(“Enter first number:”);
scanf(“%f”,&a);
printf(“Enter second number:”);
scanf(“%f”,&b);
c=a+b;
printf(“Sum: %.2f”,c);
getch();
Output:
#include<stdio.h>
int main()
{
int a;
printf("Enter any number\n");
scanf("%d",&a);
if((a%2)==0)
printf("Entered number is even");
else
printf("Entered number is odd");
return 0;
}
3. Programs that find the larger and smaller of given three numbers using function
#include <stdio.h>
int main()
{
float num1,num2,num3; printf("Enter tree
numbers\n"); scanf("%f %f
%f",&num1,&num2,&num3); if(num1<num2 &&
num1<num3) {
printf("\n%.2f is smallest",num1);
}
else if(num2<num3)
{
printf("\n%.2f is smallest",num2);
}
else
{
printf("\n%.2f is smallest",num3);
}
if(num1>num2 && num1>num3)
{
printf("\n%.2f is largest",num1);
}
else if(num2>num3)
{
printf("\n%.2f is largest",num2);
}
else{ printf("\n%.2f is largest",num3); }
return 0;
}
4. Program to find out the factorial of number using function
#include<stdio.h>
void factorial(int);
int main()
{
int num;
factorial(num);
return 0;
}
if(num == 0)
{
printf("Factorial of 0 is 1 (!0 = 1)\n");
}
else
{
for(count = 1; count <= num; count++)
{
fact = fact * count;
}
Output:
Enter a positive number to find Factorial
5
#include<stdio.h>
int main()
{
int NUM,i,j,SUM=0;
int main()
{
int Number, Reminder, Sum=0;
while(Number > 0)
{
Reminder = Number % 10;
Sum = Sum+ Reminder;
Number = Number / 10;
}
return 0;
}
7. Program to print all prime numbers less than 100 using function
#include<stdio.h>
#include<math.h>
int isprime (int n);
main()
{
int i;
for(i=1;i<=100;i++)
if(isprime (i))
printf("%d",i);
}
int isprime (int n)
{
int main()
{
int a,b;
printf("Enter a number");
scanf("%d",&a);
for(b=2;b<=a-1;b++)
if(a%b==0)
break;
if(b==a)
else
printf("%d is not a prime number",a);
return 0;
}
9. Program to convert a decimal number to binary number
#include <stdio.h>
#include <math.h>
while (decimalnum!=0)
{
rem = decimalnum%2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
return binarynum;
}
int main()
{
int decimalnum;
printf("Enter a Decimal Number: ");
scanf("%d", &decimalnum);
printf("Equivalent Binary Number is: %ld",
decimalToBinary(decimalnum)); return 0;
Output:
#include<stdio.h>
int fact(int);
int main()
{
int num;
return 0;
}
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Input : 5
Output:
Fibonacci series
0
1
1
2
3
12. Program to convert a binary or octal number to a decimal number using function
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
int decimalnum = 0, temp = 0, remainder;
while (binarynum!=0)
{
remainder = binarynum % 10;
binarynum = binarynum / 10;
decimalnum = decimalnum + remainder*pow(2,temp);
temp++;
}
return decimalnum;
}
int main()
{
long binarynum;
printf("Enter a binary number: ");
scanf("%ld", &binarynum);
Output:
#include <stdio.h>
int main()
{
int a;
if((a % 11 == 0))
{
printf("%d is divisible by 11",a);
}
else
{
printf("%d is not divisible by 11",a);
}
return 0;
}
14. Program to convert a decimal number to binary, octal or decimal number using function
#include <stdio.h>
#include <conio.h>
getch();
return 0;
}
while(n != 0) {
remainder = n%8;
n = n/8;
octal = octal + (remainder*i);
i = i*10;
}
return octal;
}
Output:
int main() {
long x, y, hcf, lcm;
return 0;
}
while (y != 0) {
if (x > y)
x = x - y;
else
y = y - x;
}
return x;
}
16. Program to add the elements of an array
#include<stdio.h>
main()
{
int arr[5],i,sum=0;
for(i=0;i<5;i++)
{
printf("Enter the value for arr[%d] : ",i);
scanf("%d",&arr[i]);
sum+=arr[i];
}
printf("sum = %d\n",sum);
#include<stdio.h>
int main()
{
int Size, i, a[10];
int Even_Count = 0, Odd_Count = 0;
int main()
{
int a[100],size, c, l, L;
l = L = 0;
return 0;
}
19. Program to reverse the elements of an array
#include<stdio.h>
int main()
{
int a[100],reverse[100],i,n;
printf("\nEnter no of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
reverse[i]=a[n-i-1];
}
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system("cls");
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given number is= ");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
21. Program to search for an item in the array
#include<stdio.h>
int main(){
int array1[30];
int i, j, n1, n2;
printf("\nEnter no of elements to array :");
scanf("%d", &n1);
printf("\n\n Enter the Array elements one by one \n");
for (i = 1; i<=n1; i++) {
scanf("%d", &array1[i]);
}
printf("\nEnter the element to be searched :");
scanf("%d", &n2);
for (i = 1; i<=n1; i++)
if(array1[i]==n2){
j=i;
}
printf("\n The Array elements are : \n");
for (i = 1; i<=n1; i++)
printf("\n %d", array1[i]);
printf("\n\n The Location of the searched element %d in the Array is %d", n2,
j);
return(0);
}
22. Program to pass array elements to a function
#include<stdio.h>
int main()
{
float avg;
int marks[] = {99, 90, 96, 93, 95};
avg = findAverage(marks); // name of the array is passed as argument.
printf("Average marks = %.1f", avg);
return 0;
}
int main()
{
int i, j, m, n;
int matrix[10][20];
return 0;
}
#include <stdio.h>
int main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;
Output:
int main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
Output:
1 2
34
The Second matrix is :
5 6
78
The multiplication of two matrices is :
19 22
43 50
26. Program to find the transpose of a matrix
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int main(void)
{
int num, choice, base;
while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Exit. \n");
switch(choice)
{
case 1:
base = 2;
break;
case 2:
base = 8;
break;
case 3:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}
printf("Result = ");
convert_to_x_base(num, base);
printf("\n\n");
}
return 0; // return 0 to operating system
}
// base condition
if (num == 0)
{
return;
}
else
{
rem = num % base; // get the rightmost digit
convert_to_x_base(num/base, base); //
recursive call if(base == 16 && rem >= 10) {
printf("%c", rem+55);
}
else
{
printf("%d", rem);
}
}
#include<stdio.h>
int main()
{
int a=90;
int *ptr1,*ptr2;
ptr1=&a;
ptr2=&a;
*ptr1=7;
*ptr2=6;
printf("The value of a is: %d",a);
return 0;
}
29. Program to understand dynamic memory allocation using malloc () function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
int *element;
/*
returns a void pointer(which is type-casted to int*)
pointing to the first block of the allocated space
*/
element = (int*) calloc(n,sizeof(int));
/*
If it fails to allocate enough space as specified,
it returns a NULL pointer.
*/
if(element == NULL)
{
printf("Error.Not enough space available");
exit(0);
}
for(i = 0; i < n; i++)
{
/*
storing elements from the user
in the allocated space
*/
scanf("%d", element+i);
}
for(i = 1; i < n; i++)
{
if(*element > *(element+i))
{
*element = *(element+i);
}
}
printf("Smallest element is %d", *element);
return 0;
}
30. Program to understand the use of realloc () function
#include<stdio.h>
#include<stdlib.h>
int main( )
{
int i, *ptr;
ptr = (int *)malloc(5 *sizeof(int));
if(ptr == NULL)
{
printf("Memory not available\n");
exit(1);
}
printf("Enter 5 integers : ");
for(i=0; i<5; i++)
scanf("%d", ptr+i);
/*Allocate memory for 4 more integers*/
ptr = (int *)realloc(ptr, 9*sizeof(int));
if(ptr == NULL)
{
printf("Memory not available\n");
exit(1);
}
printf("Enter 4 more integers : ");
for(i=5; i<9; i++)
scanf("%d", ptr+i);
for(i=0; i<9; i++)
printf("%d ", *(ptr+i));
}
31. Program to understand the use of gets ()
int main()
{
char name[50];
printf("=============\n");
printf("%s", name);
return 0;
}
#include<stdio.h>
#include <string.h>
int main()
{
char ch[]={'g', 'e', 'e', 'k', 's', '\0'};
return 0;
}
33. Program to understand the use of strcmp () function
#include <stdio.h>
#include <string.h>
int main () {
char str1[20];
char str2[20];
int result;
if(result > 0) {
printf("ASCII value of first unmatched character of str1 is greater than str2");
} else if(result < 0) {
printf("ASCII value of first unmatched character of str1 is less
than str2"); } else {
printf("Both the strings str1 and str2 are equal");
}
return 0;
}
34. Program to understand the use of strcpy () function
#include <stdio.h>
#include <string.h>
int main () {
char str1[20];
char str2[20];
return 0;
}
#include <stdio.h>
#include <string.h>
int main () {
char str1[50], str2[50];
//destination string
strcpy(str1, "This is my initial string");
//source string
strcpy(str2, ", add this");
return(0);
}
36. Program to display the values of structure numbers
#include <stdio.h>
* StudentData.
*/struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
student.stu_name = "S";
student.stu_id = 1134;
student.stu_age = 22;
student.stu_name); printf("\nStudent Id
}
Output:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
j.workerNo = 100;
#include <stdio.h>
#include <string.h>
struct struct_example
{
int integer;
float decimal;
char name[20];
};
union union_example
{
int integer;
float decimal;
char name[20];
};
void main()
{
struct struct_example s={18,38,"geeksforgeeks"};
u.integer = 183;
u.decimal = 90;
strcpy(u.name, "geeksforgeeks");
printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);
u.decimal = 120;
printf("\ndecimal: %f", u.decimal);
u.integer = 1218;
printf("union data:\n integer: %d\n"
" decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
}
Output:
structure data:
integer: 18
decimal: 38.00
name: geeksforgeeks
union data:
integeer: 18
decimal: 0.00
name:
sizeof structure : 28
sizeof union : 20
union data:
integeer: 1801807207
decimal: 277322871721159510000000000.00
name: geeksforgeeks
union data:
integer: 240
decimal: 120.000000
name: C programming