C Assignment New
C Assignment New
Mukherjee
Univ-Roll: 123200901021
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
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;
}
Univ-Roll: 123200901021
Volume of cylinder=50.24
Notes/ point to
be remember
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;
ci = 2 * PI * rad;
printf ("\n Circumference : %f ", ci);
Univ-Roll: 123200901021
Circumference : 56.52000
}
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)
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
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
}
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
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
}
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;
}
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
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.
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
int main()
{
int a,b,l;
printf("\n Enter a and b:");
scanf("%d %d", &a,&b);
l=(a>b)?a:b;
return 0;
}
Results Enter a and b: 22 11
The largest number is 22
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
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);
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;
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
#include<stdio.h>
int main ()
{
int i;
return 0;
}
Results
Notes/ point to Syntax: for(initialization; condition; pupation)
be remember
#include<stdio.h>
CS291: C programming Theory/Lab, Stream:EE Name: Shreta
Mukherjee
Univ-Roll: 123200901021
int main ()
{
int i;
return 0;
}
Results
Notes/ point to while(i<=10) // condition
be remember {
……
i++; //updation
}
#include<stdio.h>
int main ()
{
int i;
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
#include<stdio.h>
int main ()
{
int i;
return 0;
}
Results
Notes/ point to if(i%2==0)
be remember
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
return 0;
}
Results
Notes/ point to
be remember
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
return 0;
}
Results
Notes/ point to
be remember
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;
}
printf("The sum is %d\n", sum);
return 0;
}
Results
Notes/ point to
be remember
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
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
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
Results
Notes/ point to
be remember
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
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;
}
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
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]);
}
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
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
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
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
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
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
FILE *fptr;
fptr = (fopen("C:\\ studentdata.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
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)
FILE *fptr;
fptr = (fopen("C:\\ studentdata.txt", "a"));
if(fptr == NULL)
{
printf("Error!");
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