0% found this document useful (0 votes)
59 views

C Assignment New

This document contains 11 programs written in C programming language with their corresponding code, output, and notes. The programs cover basic concepts like adding and multiplying numbers, calculating area and volume, temperature conversions, and data types. Each program listing includes the title, code, sample run showing input and output, and relevant points to remember.

Uploaded by

Shreta Mukherjee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

C Assignment New

This document contains 11 programs written in C programming language with their corresponding code, output, and notes. The programs cover basic concepts like adding and multiplying numbers, calculating area and volume, temperature conversions, and data types. Each program listing includes the title, code, sample run showing input and output, and relevant points to remember.

Uploaded by

Shreta Mukherjee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

CS291: C programming Theory/Lab, Stream:EE Name: Shreta

Mukherjee

Univ-Roll: 123200901021

1 Program title Write program to ADD 2 numbers.


CS291 Program code #include <stdio.h>
#include <conio.h>

int main ()
{
int a, b, s;
printf ("Enter a and b");
scanf ("%d %d", &a, &b);
s = a + b;
printf ("sum=%d", s);

getch ();
return 0;
}
Results Enter a and b 3 5
Sum=8
Notes/ point to
be remember
2 Program title Write program to Multiply 2 numbers.
Program code #include <stdio.h>
#include <conio.h>

int main ()
{
int a, b, m;
printf ("Enter a and b");
scanf ("%d %d", &a, &b);
m = a * b;
printf ("multiplication=%d", m);

getch ();
return 0;
}
Results Enter a and b 3 5
multiplication=15
Notes/ point to
be remember
3 Program title Write program to find the area of a circle.
Program code #include <stdio.h>
int main()
{
int r;
float A;
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
printf(" Enter r");
scanf("%d",&r);
A= 3.14*r*r;
printf(" Area of a circle = %f", A);
return 0;
}
Results Enter r 1
Area of a circle = 3.14
Notes/ point to Control string for variable A is %f because float
be remember
4 Program title Write program to find the Amount when p, t, and r, are given.

#include <stdio.h>
#include <conio.h>

int main()
{
float p,r,t,SI,A;
printf(“Enter p,r,t”);
scanf(“%f %f %f”,&p,&r,&t);
SI=(p*r*t)/100;
A=p+SI;
printf(“Amount= %f”,A);
return 0;
}
Enter p r t 100 2 4
Amount=108

5 Program title Write program to find the volume of a cylinder.

Program cod #include<stdio.h>


#include<conio.h>

int main()
{
float r,h,V;
printf(“Enter r and h”);
scanf(“ %f %f”, &r,&h);
V=3.14*r*r*h;
printf(“Volume of cylinder= %f”, V);
return 0;
}

Results Enter r and h 2 4


CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
Volume of cylinder=50.24
Notes/ point to
be remember

6 Program title Write program to find the percentage of marks of X


standard.
Program code #include<stdio.h>
#include<conio.h>

int main()
{
float P,C,M,B,H,G,E,PP;
printf(“Enter P,C,M,B,H,G,E”);
scanf(“ %f %f %f %f %f %f %f”, &P,&C,&M,&B,&H,&G,&E);
PP=(P+C+M+B+H+G+E)/700*100;
printf(“Percentage of marks= %f”, PP);
return 0;
}

7 Program
title C Program To Find Area And Circumference Of Circle

Program #include<stdio.h>
cod int main ()
{

int rad;
float PI = 3.14, area, ci;

printf ("\n Enter radius of circle: "); // \n is represent new line


scanf ("%d", &rad);

area = PI * rad * rad;


printf (" \n Area of circle : %f ", area);

ci = 2 * PI * rad;
printf ("\n Circumference : %f ", ci);

return (0); // int data type return integer value 0


}
Results  Enter radius of circle: 9                                                                                                
                                                              Area of circle : 254.339996                                          
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
                                                        
 Circumference : 56.52000

Notes/ \n is represent new line


point to be
remember

8 Program Print ASCII Value of the Character


title
Program #include<stdio.h>
code void main() // void data type return nothing
{
char ch;
int n;
printf("Enter The Character ");
scanf("%c",&ch);
n=ch;
printf("\n ASCII valu of %c Is =%d ", ch, n);

}
Results Enter The Character A                                                                                                         
ASCII valu of A Is =65    

Notes/ point ASCII was developed from telegraph code. Its first commercial use was as a seven-bit
to be teleprinter code promoted by Bell data services.
remember ASCII Is ( American Standard Code for Information Interchange)

9 Program C Program For Converting Temperature Celsius Into Fahrenheit


title
Program #include<stdio.h>
code
int main()
{

float cel, fa;

printf("\nEnter Temperature in Celsius : ");


scanf("%f", &cel);

fa = (1.8 * cel) + 32;


printf("\nTemperature in Fahrenheit : %f ", fa);

return (0);
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021

}
Results Enter Temperature in Celsius : 35.6                                                                                          
 
                                                         
Temperature in Fahrenheit : 67.599998 

Notes/ point fa = ((9/5) * cel) + 32;


to be
remember

1 Prog C Program For Converting Temperature Fahrenheit Into Celsius


0 ram
title
Progr #include<stdio.h>
am
cod int main()
{

float cel, fa;

printf("\nEnter Temperature in Fahrenheit : ");


scanf("%f", &fa);

cel = (fa-32)*5/9;
printf("\nTemperature in Celsius : %f ", cel);

return (0);
}

Resul Enter Temperature in Fahrenheit : 98.2                                                                                
ts                                                                                                                       
Temperature in Celsius : 36.777779  

Note
s/
point
to be
reme
mber
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021

11 Program C Program To Display Size Of Different Data-type


title
Program #include <stdio.h>
cod int main()
{
printf("int is %d bytes \n", sizeof(int));
printf("long int is %d bytes \n", sizeof(long int));
printf("float is %d bytes \n", sizeof(float));
printf("double is %d bytes \n", sizeof(double));
printf("long double is %d bytes \n", sizeof(long double));
printf("char is %d bytes \n", sizeof(char));
return 0;

}
Results int is 4 bytes                                                                                                                
long int is  8 bytes                                                                                                          
float is    4 bytes                                                                                                           
double is   8 bytes     

long double is  16 bytes                                                                                                      
char is   1 bytes

Notes/ Sizeof() is used to print the size of the data type.


point to
be
remember

12 Program title Write program to find the volume of a cylinder.

Program cod #include<stdio.h>


#include<conio.h>

int main()
{
float r,h,V;
printf(“Enter r and h”);
scanf(“ %f %f”, &r,&h);
V=3.14*r*r*h;
printf(“Volume of cylinder= %f”, V);
return 0;
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
}

Results Enter r and h 2 4


Volume of cylinder=50.24
Notes/ point to
be remember

5 Program title Write program to find the volume of a cylinder.

Program cod #include<stdio.h>


#include<conio.h>

int main()
{
float r,h,V;
printf(“Enter r and h”);
scanf(“ %f %f”, &r,&h);
V=3.14*r*r*h;
printf(“Volume of cylinder= %f”, V);
return 0;
}

Results Enter r and h 2 4


Volume of cylinder=50.24
Notes/ point to
be remember

5 Program title Write program to find the volume of a cylinder.

Program cod #include<stdio.h>


#include<conio.h>

int main()
{
float r,h,V;
printf(“Enter r and h”);
scanf(“ %f %f”, &r,&h);
V=3.14*r*r*h;
printf(“Volume of cylinder= %f”, V);
return 0;
}
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
Results Enter r and h 2 4
Volume of cylinder=50.24
Notes/ point to
be remember

Ref https://www.programmingwithbasics.com/p/list-c-language-programs.html

1 Program Problem :- Write A C Program To Check Number Is Positive Or Negative .Number


3 title should be Entered Through User Then program Print the Output .
(4-5-21)
Program #include <stdio.h>
code
int main()
{
int n; // n may be positive or negative or zero
printf("\n Enter a number:");
scanf("%d",&n);
if(n>0)
{
printf("\n n=%d is positive number",n); // true block
}
else if(n<0)
{
printf("\n n=%d is negative number",n); // False block for 1st if condition but 2nd
condition it is true block
}
else
{
printf("\n n=%d is zero number",n); // false block
}

return 0;
}
Results Enter a number:0                                                                                                            
 n=0 is zero number

Notes/ point
to be Logic :- As we know that if number is greater than 0 ( Zero ) Than number is positive
remember and if number is Less than 0 ( Zero ) than number is Negative and else number is
equal to zero than entered number is Zero .
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
1 Prog Write program to find whether the given number is even or odd.
4 ram
title
Progr #include <stdio.h>
am
cod int main()
{
int num;
printf("\nEnter Any Number To Check Even Or Odd :\n");
scanf("%d", &num);
if (num%2 == 0)
{
printf("%d is EVEN\n", num);
}
else
{
printf("%d is ODD\n", num);
}
return 0;
}
Resul Enter Any Number To Check Even Or Odd :                                                                               
ts 7                                                                                                                     
7 is ODD

Note
s/
point
to be
reme
mber

1 Prog Write program to check whether a student eligible for vote or not.
5 ram
title
Progr #include <stdio.h>
am
cod int main()
{
int age;
printf("\nEnter age:\n");
scanf("%d", &age);
if (age>=18)
{
printf("You are eligible for voting\n");
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
}
else
{
printf("You are not eligible for voting\n");
}
return 0;
}
Resul Enter age:                                                                                                            
ts 20                                                                                                                    
You are eligible for voting

Note
s/
point
to be
reme
mber
16 Program title C Program For Converting positive or negative number.

Program cod #include <stdio.h>


int main ()
{
int n;
printf("Enter the number");
scanf("%d",&n);
if(n>0)
{
printf("\n n=%d is the poisitive number",n)
}
else if(n<0)
{
printf ("\n n=%d is the negative number",n);
}
return 0;
}
Results
Notes/ point to be
remember

18 Program title C Program For finding largest of 2 numbers using if else

Program cod #include <stdio.h>


CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
int main()
{
int a,b;
printf("Enter a and b number:\n");
scanf("%d %d",&a,&b);
if(a>b)
{
printf("a=%d is the lagest number",a);
}
else if(b>a)
{
printf("b=%d is the largest number",b);

}
return 0;
}
Results
Notes/ point to
be remember

19 Program title C Program For finding largest of 2 numbers using Ternary operator

Program cod #include <stdio.h>

int main()
{
int a,b,l;
printf("\n Enter a and b:");
scanf("%d %d", &a,&b);

l=(a>b)?a:b;

printf("\n The largest number is %d",l);

return 0;
}
Results Enter a and b: 22 11                                                                                              
           
The largest number is 22

Notes/ point to be The syntax (a>b)?a:b;


remember
Conditional operator is the alternative of if else statement.
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
20 Progra C Program For finding largest of 3 numbers using if else
m title
Program #include <stdio.h>
cod
int main()
{
int a,b,c,l;
printf("\n Enter a ,b and c:");
scanf("%d %d %d", &a,&b, &c);

if(a>b)
{
if(a>c)
{
l=a;
}
else
{
l=c;
}
}
else
{
if(b>c)
{
l=b;
}
else
{
l=c;
}

}
printf("\n The largest number is %d",l);

return 0;
}
Results Enter a ,b and c: 22 33 11                                                                                                   
                                                                 The largest number is 33 

Notes/ Nesting of if else


point to
be
rememb
er
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021

2 Program title C Program For finding largest of 3 numbers using conditional operator
1
Program cod #include <stdio.h>

int main()
{
int a,b,c,l;
printf("\n Enter a ,b and c:");
scanf("%d %d %d", &a,&b, &c);

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

printf("\n The largest number is %d",l);

return 0;
}
Results Enter a ,b and c: 11 33 22                                                                                               
    
                                                                 The largest number is 33 

Notes/ point to
be remember

2 Prog Write A C Program To Check Year Is Leap Year Or Not Means leap Year Has 366
2 ram Days in Year .
title
Prog #include<stdio.h>
ram int main ()
cod {
//Ghanendra Yadav
int year;

printf ("Enter Any For Check Year: \n");


scanf ("%d", &year);

if ((year % 4) == 0 )
{
printf ("%d Is a Leap Year .\n", year);
}
else
{
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
printf ("%d Is Not a Leap Year .\n", year);

return 0;
}
Resul Enter Any For Check Year:                                                                                                     
ts 2001                                                                                                                          
2001 Is Not a Leap Year .  

Note Logic :- We Know that leap year have 366 Days in a year ,so if year is divide by 4 and
s/ 400 then year is leap year or one more condition if year divide by 100 then Year Is
point Not leap year or Leap Year Comes Every 4 Years Like 
to be 1992 ,1996 ,2000 ,2004 ,2008 ,2012 ,2016 ,2020 These are leap Years. you can
reme modified this code to find next or previous Leap Year .
mber

23 Program title Learn Loop: for()

Program cod // print ten numbers using for loop

#include<stdio.h>
int main ()
{
int i;

for(i=1; i<=10; i++)


{
printf("%d\t",i);
}

return 0;
}
Results
Notes/ point to Syntax: for(initialization; condition; pupation)
be remember

24 Program title Learn Loop: while()

Program cod // print ten numbers using while loop

#include<stdio.h>
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
int main ()
{
int i;

i=1; //initialization // for(i=0; i<=10; i++)


while(i<=10) // condition
{
printf("%d\t",i);
i++; //updation
}

return 0;
}
Results
Notes/ point to while(i<=10) // condition
be remember {
……
i++; //updation
}

25 Program title Learn Loop: do while()

Program cod // print ten numbers using do while loop

#include<stdio.h>
int main ()
{
int i;

i=1; //initialization // for(i=0; i<=10; i++)


do
{
printf("%d\n",i);
i++; //updation
}while(i<=10); // condition

return 0;
}
Results
Notes/ point to do
be remember {
‘’’’’’’’
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
i++; //updation
}while(i<=10); // condition

26 Program title // print the even numbers up to 10 using do while loop

Program cod // print the even numbers up to 10 using do while loop

#include<stdio.h>
int main ()
{
int i;

i=1; //initialization // for(i=0; i<=10; i++)


do
{
if(i%2==0)
printf("%d\",i);
i++; //updation
}while(i<=10); // condition

return 0;
}
Results
Notes/ point to if(i%2==0)
be remember

27 Program title // print the even numbers up to 10 using while loop

Program cod #include<stdio.h>


int main ()
{
int i;

i=1;
while(i<=10)
{
if(i%2==0)
printf("%d",i);
i++;
}
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
return 0;
}
Results
Notes/ point to
be remember

28 Program title // print the even numbers up to 10 using for loop

Program cod #include<stdio.h>


int main ()
{
int i;
for(i=0;i<=10;i++)
{
if(i%2==0)
printf("%d",i);
}

return 0;
}
Results
Notes/ point to
be remember

29 Program title // print the odd numbers up to 10 using for loop

Program cod #include<stdio.h>


int main ()
{
int i;
for(i=0;i<=10;i++)
{
if(i%2!=0)
printf("%d\",i);
}

return 0;
}
Results
Notes/ point to
be remember
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
30 Program title // print the odd numbers up to 10 using while loop

Program cod #include<stdio.h>


int main ()
{
int i;
while(i<=10)
{
if(i%2!=0)
printf("%d\",i);
i++;
}

return 0;
}
Results
Notes/ point to
be remember

31 Program title // print the odd numbers up to 10 using do while loop

Program cod #include<stdio.h>


int main ()
{
int i;
do
{
if(i%2==0)
printf("%d\",i);
i++;
}
while(i<=10);

return 0;
}
Results
Notes/ point to
be remember

32 Program title // print the sum of first 10 numbers using for loop

Program cod // print the sum of first 10 numbers using for loop
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021

#include<stdio.h>
int main ()
{
int i, sum=0;

for(i=1; i<=10; i++)


{
sum=sum+i;

}
printf("The sum is %d\n", sum);
return 0;
}
Results
Notes/ point to
be remember

34 Program title // Write a C program to demonstrate break statement.

Program cod
#include<stdio.h>  
void main ()  
{  
 int i;  
for(i = 0; i<10; i++)  
{  
        printf("%d ",i);  
    if(i == 5)  
    break;  
}  
printf(" i = %d",i);  
      
}  

Results
Notes/ point to
be remember
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
35 Program title // Write a C program to demonstrate continue statement.

Program cod
#include<stdio.h>  
void main ()  
{  
    int i = 0;   
    while(i!=10)  
    {  
        printf("%d", i);   
        continue;   
        i++;  
    }  
}  

Results
Notes/ point to
be remember

36 Program title // Write a C program to print multiplication table using nested


for loop.
Program cod #include <stdio.h>

int main()
{
int j,i;
printf("Enter a number");
scanf("%d",&j);
printf(" Multiplication table: \n");
for(i=1; i<=12; i++)
{
printf("%d * %d = %d \n",i,j,i*j);
}
return 0;
}
Results
Notes/ point to
be remember
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
37 Program title // Write a C program to print multiplication table using nested
while loop.
Program cod #include <stdio.h>

int main()
{
int j,i;
printf("Enter a number: ");
scanf("%d",&j);
printf("Multiplication table: \n");
i=1;
while(i<=10)
{
printf("%d * %d = %d \n",i,j,i*j);
i++;
}
return 0;
}
Results
Notes/ point to
be remember

38 Program title // Write a C program to print multiplication table using nested


do while loop.
Program cod #include <stdio.h>

int main()
{
int j,i;
printf("Enter a number ");
scanf("%d",&j);
printf("Here your multiplication table\n\n");
i=1;
do
{
printf("%d * %d = %d\n",i,j,i*j);
i++;
}
while(i<=10);
return 0;
}
Results
Notes/ point to
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
be remember

39 Program title // Write a C program to print multiplication of 2 numbers using


function.
Program code
#include <stdio.h>
int multiplyNum(int , int);
int main()
{
int num1,num2,product;
printf("Enter the two number ");
scanf("%d %d",&num1,&num2);
product=multiplyNum(num1,num2);
printf("The product of these numbers :%d",product);
getch();
return 0;
}
int multiplyNum(int a, int b)
{
int result=a*b;
return result;
}

Results
Notes/ point to
be remember

40 Program title /* Function with No argument and No Return value Example


*/

Program code #include <stdio.h>


CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
void value(void);
void main()
{
    value();
}
void value(void)
{
    int year = 1, period = 5, amount = 5000,
inrate = 0.12;
    float sum;
    sum = amount;
    while (year <= period) {
        sum = sum * (1 + inrate);
        year = year + 1;
    }
    printf(" The total amount is %f:", sum);
}

Results
Notes/ point to
be remember

41 Program title /* Function with argument but No Return value Example */

Program code #include <stdio.h>


  
void function(int, int[], char[]);
int main()
{
    int a = 20;
    int ar[5] = { 10, 20, 30, 40, 50 };
    char str[30] = "shretamukherjee";
    function(a, &ar[0], &str[0]);
    return 0;
}
  
void function(int a, int* ar, char* str)
{
    int i;
    printf("value of a is %d\n\n", a);
    for (i = 0; i < 5; i++)
{
        printf("value of ar[%d] is %d\n", i,
ar[i]);
    }
    printf("\nvalue of str is %s\n", str);
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
}

Results
Notes/ point to
be remember

42 Program title /* Function with no argument and but Return value Example
*/
Program code #include <math.h>
#include <stdio.h>
  
int sum();
int main()
{
    int num;
    num = sum();
    printf("\nSum of two given values = %d", num);
    return 0;
}
  
int sum()
{
    int a = 50, b = 80, sum;
    sum = sqrt(a) + sqrt(b);
    return sum;
}

Results
Notes/ point to
be remember

43 Program title /* Function with argument and with Return value Example
*/
Program code #include <stdio.h>
#include <string.h>
int function(int, int[]);

int main()
{
int i, a = 20;
int arr[5] = { 10, 20, 30, 40, 50 };
a = function(a, &arr[0]);
printf("value of a is %d\n", a);
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
for (i = 0; i < 5; i++) {
printf("value of arr[%d] is %d\n", i, arr[i]);
}
return 0;
}

int function(int a, int* arr)


{
int i;
a = a + 20;
arr[0] = arr[0] + 50;
arr[1] = arr[1] + 50;
arr[2] = arr[2] + 50;
arr[3] = arr[3] + 50;
arr[4] = arr[4] + 50;
return a;
}
Results
Notes/ point to
be remember

44 Program title // Write a C program to print any 10 numbers.

Program code #include<stdio.h>


int main()
{
int a[10],i;
printf(“Enter the data:\n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i];
printf(“%d\t”,a[i]);
}
return 0;
}
Results
Notes/ point to
be remember

45 Program title // Write a C program to print the sum of any 10 numbers

Program code #include<stdio.h>


CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
int main()
{
int a[10],i,sum;
printf(“Enter the data:\n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i];
sum=sum+a[i];
}
printf(“The sum=%d”,sum);
return 0;
}
Results
Notes/ point to
be remember

46 Program title // Write a C program to print the sum of even numbers upto
100
Program code #include <stdio.h>
int main()
{
int a[100], i,sum;
// printf("Enter the data:\n");
for(i=0;i<100;i++)
{
a[i]=1+i;
}
for(i=0;i<100;i++)
{
if(a[i]%2==0)
sum=sum+a[i];
}
printf("The Sum of even numbers upto 100=%d",sum);
return 0;
}
Results
Notes/ point to
be remember

47 Program title // Write a C program to sort 10 numbers.

Program code #include<stdio.h>


CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
int main ()
{
int i, j, temp, a[10];
//int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23 };
for(i=0;i<10; i++)
{
scanf("%d",&a[i]);
}

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


{
for (j = i + 1; j < 10; j++)
{
if (a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf ("Printing Sorted Element List ...\n");
for (i = 0; i < 10; i++)
{
printf ("%d\t", a[i]);
}
return 0;
}
Results
Notes/ point to
be remember

48 Program title // Write a program to print the elements of a matrix.

Program code int main()


{
int a[3][3] ,i, j;
printf("Enter the elements of matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
}
}
printf("Two dimensional array elements:\n");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
}
return 0;
}
Results
Notes/ point to
be remember

49 Program title // Write a program to perform the following matrix operation:


C=A+B
Program code #include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the elements of matrix A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the elements of matrix B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The addition of matrix C:\n");
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", c[i][j]=a[i][j]+b[i][j]);
}
printf("\n");
}
return 0;
}
Results
Notes/ point to
be remember

50 Program title // Write a program to perform the following matrix operation:


C=A*B
Program code #include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
printf("Enter the elements of matrix A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the elements of matrix B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The multiplication of matrix C:\n");
for(i=0;i<3;i++)
{
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]= c[i][j] + a[i][k] *b[k][j];
}
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Results
Notes/ point to
be remember

51 Program title // Write a program to perform the following matrix operation:


C=A*B
Program code #include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
printf("Enter the elements of matrix A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the elements of matrix B:\n");
for(i=0;i<3;i++)
{
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The multiplication of matrix C:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]= c[i][j] + a[i][k] *b[k][j];
}
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Results
Notes/ point to
be remember

52 Program title // Write a program to Write to a text file


Program code #include<stdio.h>

int main()
{
int num;
FILE *fptr;
fptr = fopen ("C :\\ users\\SHRETA
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
MUKHERJEE\\cfileprogl.txt","w");
if(fptr==NULL)
{
printf("Error!");
exit(1);
}
printf("Enter number : ");
scanf("%d",&num);

fprintf(fptr,"%d",num);
fclose(fptr);

return 0;
}
Results
Notes/ point to
be remember

53 Program title // Write a program to Read from a text file


Program code #include<stdio.h>
int main()
{
int num;
FILE *fptr;

if ((fptr = fopen("C :\\ users\\SHRETA


MUKHERJEE\\cfileprogl.txt","r"))==NULL;
if(fptr==NULL)
{
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%d",&num);

printf("Value of n %d",num);
fclose(fptr);
getch()
return 0;
}
Results
Notes/ point to
be remember
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021

54 Program title // Write a program to print


the record of five
students: Student ID, Student Name and
marks.
Program code #include <stdio.h>
int main()
{
char name[50];
int marks, i, num;

printf("Enter number of students: ");


scanf("%d", &num);

FILE *fptr;
fptr = (fopen("C:\\ studentdata.txt", "w"));
if(fptr == NULL)
{
printf("Error!");

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


{
printf("For student%d\nEnter name: ", i+1);
scanf("%s", name);

printf("Enter marks: ");


scanf("%d", &marks);

fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);


}

fclose(fptr);
return 0;
}

Results Name: SS
Marks=44

Name: FF
Marks=55

Name: HH
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
Marks=99

Name: NN
Marks=66

Name: MM
Marks=77
Notes/ point to FILE *fptr;
be remember fptr = (fopen("C:\\ studentdata.txt", "w"));
if(fptr == NULL)

55 Program title // Write a file program to store


the record of five
students: Student ID, Student Name and
marks.
Program code #include <stdio.h>
int main()
{
char name[50];
int marks, i, num;

printf("Enter number of students: ");


scanf("%d", &num);

FILE *fptr;
fptr = (fopen("C:\\ studentdata.txt", "a"));
if(fptr == NULL)
{
printf("Error!");

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


{
printf("For student%d\nEnter name: ", i+1);
scanf("%s", name);

printf("Enter marks: ");


scanf("%d", &marks);

fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);


}
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee

Univ-Roll: 123200901021
fclose(fptr);
return 0;
}

Results Name: SS
Marks=44

Name: FF
Marks=55

Name: HH
Marks=99

Name: NN
Marks=66

Name: MM
Marks=77
Notes/ point to FILE *fptr;
be remember fptr = (fopen("C:\\ studentdata.txt", "a"));
if(fptr == NULL)

Ref https://www.programmingwithbasics.com/p/list-c-language-programs.html

You might also like