C Program
C Program
Ans.-
#include<stdio.h>
#include<conio.h>
int main(void)
Output: -
1.b) Write a C program to add two numbers(2 and 6) and display its
sum.
#include <conio.h>
int main()
int a,b,c;
a=2, b=6;
c=a+b;
return 0;
getch();
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
printf("Enter the value of x\n");
scanf("%d",&x);
printf("Enter the value of y\n");
scanf("%d",&y);
z=x;
x=y;
y=z;
printf("Swap of two number: x=%d , y=%d",x,y);
return 0;
getch ();
}
Output: -
Enter the value of x
10
Enter the value of y
30
Swap of two number: x=30 , y=10
1.e) Write a C program to perform swap the value of two variables with
& without using third variables.
Ans.) // Swap 2 number without using third variables
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
printf("Enter the 1st value of x\n");
scanf("%d",&x);
printf("Enter the 2nd value of y\n");
scanf("%d",&y);
x=x+y;
y=x-y;
x=x-y;
printf("Swap of two number: x=%d , y=%d",x,y);
return 0;
getch ();
}
Output: -
Enter the 1st value of x
20
Enter the 2nd value of y
40
Swap of two number: x=40 , y=20
1.f) Write a C program to check whether a given number is odd or even.
Ans.)
// Check whether a given number is Odd or Even
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
printf("Enter the value.\n");
scanf("%d",&x);
y=x%2;
if(y==0)
printf("\n %d is Even number.",x,y);
else
printf("\n %d is Odd number.",x,y);
return 0;
getch();
}
Output: -
Enter the value.
10
10 is Even number.
NEXT INPUT🡪
Enter the value.
21
21 is Odd number.
1.g) Write a C program to check leap year or not.
Ans.) // To check leap year or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d",&year);
if(year%400==0)
printf("%d is a leap year.\n",year);
else if(year%100==0)
printf("%d is not a leap year.\n",year);
else if(year%4==0)
printf("%d is a leap year.\n",year);
else
printf("%d is not a leap year.\n",year);
return 0;
getch();
}
Output: -
Enter a year to check if it is a leap year
1986
1986 is not a leap year.
NEXT INPUT🡪
Enter a year to check if it is a leap year
2124
2124 is a leap year.
ASSIGNMENT – 2.
2.a) Write a C program to find the roots of a quadratic equation.
Ans.)
#include<stdio.h>
#include<math.h>
int main(void)
{
int a,b,c,d;
float x1,x2;
printf("Enter the value a\n");
scanf("%d",&a);
printf("Enter the value b\n");
scanf("%d",&b);
printf("Enter the value c\n");
scanf("%d",&c);
d=(b*b-4*a*c);
if(d==0)
{
printf("Both roots are real.\n");
x1=(-b)/(2.0*a);
x2=x1;
printf("First Root Root1=%f\n",x1);
printf("Second Root Root2=%f\n",x2);
}
else if(d>0)
{
printf("Boot roots are real and diff\n");
x1=(-b+sqrt(d))/(2.0*a);
x2=(-b-sqrt(d))/(2.0*a);
printf("First Root Root1=%f\n",x1);
printf("Second Root Root2=%f\n",x2);
}
else
printf("Root are imaginary;\n No solution\n");
}
Output: -
ASSIGNMENT – 3.
3.a.) Write a C program to check whether given number is Armstrong
number or not.
Ans.)
#include<stdio.h>
#include<conio.h>
int main(void)
int a,n,b=0,t;
scanf("%d",&n);
t=n;
while(n>0)
a=n%10;
b=b+a*a*a;
n=n/10;
n=n/10;
}
if(b==t)
{
printf("Armstrong number");
else
getch();
Output: -
153
Armstrong number
NEXT INPUT🡪
145
Output: -
Enter the number:
145
145
The number is a Strong Number
NEXT INPUT🡪
Enter the number:
45
144
The number is not Strong Number
3.c.) Write a C program to generate the first n terms of the Fibonacci
series.
Ans.)
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b,c,i,n;
a=0;
b=1;
printf("\nEnter n for how many times generate series:\n");
scanf("%d",&n);
printf("\nFIBONACCI SERIES\n");
printf(" %d %d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf(" %d",c);
}
getch();
}
Output: -
/ / Generate the first n terms of Fibonacci Series
Enter n for how many times generate series
12
FIBONACCI SERIES
0 1 1 2 3 5 8 13 21 34 55 89 144 233
ASSIGNMENT – 5.
5.a.) Write a C program to add two numbers using pointers.
Ans.)
#include<stdio.h>
int main()
{
int a,b,*p,*q,sum;
printf("Enter the 1st value.\n");
scanf("%d",&a);
printf("Enter the 2nd value.\n");
scanf("%d",&b);
p=&a;
q=&b;
sum=*p+*q;
printf("Sum of the numbers = %d\n",sum);
return 0;
}
Output: -
Enter the 1st value. Enter the 1st value.
54 NEXT 115
Enter the 2nd value. INPUT🡪 Enter the 2nd value.
62 987
Sum of the numbers = 116 Sum of the numbers = 1102
5.b.) Write a C program to swap value of two variables using pointer.
Ans.)
#include<stdio.h>
int main()
{
int a,b, *p,*q,temp;
printf("Enter the value of a.\n");
scanf("%d",&a);
printf("Enter the value of b.\n");
scanf("%d",&b);
p=&a;
q=&b;
temp=*p;
*p=*q;
*q=temp;
printf("Swap a = %d , b = %d",a,b);
return 0;
}
Output: -
Enter the value of a.
50
Enter the value of b.
30
Swap a = 30 , b = 50
5.c.) Write a C program to find the sum of all the elements of an array
using pointers.
Ans.)
#include<stdio.h>
int main()
{
int arr[50],i,n;
int *ptr=&arr[0],sum=0;
printf("Enter the size of elements:\n");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the arr[%d] = ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
sum+=*ptr;
ptr++;
}
printf("\n");
printf("Sum of array elements. = %d\n",sum);
return 0;
}
Output: -
Enter the size of elements:
8
Enter the arr[0] = 10
Enter the arr[1] = 20
Enter the arr[2] = 30
Enter the arr[3] = 40
Enter the arr[4] = 50
Enter the arr[5] = 60
Enter the arr[6] = 70
Enter the arr[7] = 80
Sum of all array elements. = 360
5.d.) Write a C program to input and print array elements using pointers.
Ans.)
#include<stdio.h>
int main()
{
int arr[100],i,n;
int *ptr=&arr[0];
printf("Enter the size of elements n:\n");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the arr[%d]= ",i);
scanf("%d",ptr);
*ptr++;
}
ptr=&arr[0];
printf("\n");
printf("DISPLAY ALL THE NUMBERS TAKEN BY USER.\n");
for(i=0;i<n;i++)
{
printf("%d\t",*(ptr+i));
}
return 0;
}
Output: -
Enter the size of elements n:
7
Enter the arr[0]= 10
Enter the arr[1]= 20
Enter the arr[2]= 30
Enter the arr[3]= 40
Enter the arr[4]= 50
Enter the arr[5]= 60
Enter the arr[6]= 70
#include<stdio.h>
#include<stdlib.h>
int main()
{
int* ptr;
int n,i,x;
printf("Enter size of elements:\n");
scanf("%d",&n);
printf("Entered number of elements:%d\n",n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
for(i=0;i<n;++i)
{
printf("Enter the elements.\n");
scanf("%d",&x);
ptr[i]=x;
}
printf("The elements of the array are: ");
for(i=0;i<n;++i)
{
printf("%d ",ptr[i]);
}
}
return 0;
}
Output: -
Enter size of elements:
4
Entered number of elements: 4
Memory successfully allocated using malloc.
Enter the elements.
1
Enter the elements.
2
Enter the elements.
3
Enter the elements.
4
The elements of the array are: 1 2 3 4
5.e.) Write a C program to Display array elements using calloc () &
malloc () function.
Ans.) / / malloc () function.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int* ptr;
int n,i,x;
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Entered number of elements:%d\n",n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
for(i=0;i<n;++i)
{
printf("Enter the elements.\n");
scanf("%d",&x);
ptr[i]=x;
}
printf("The elements of the array are: ");
for(i=0;i<n;++i)
{
printf("%d ",ptr[i]);
}
}
return 0;
}
Output: -
Enter number of elements:
5
Entered number of elements: 5
Memory successfully allocated using malloc.
Enter the elements.
5
Enter the elements.
9
Enter the elements.
4
Enter the elements.
7
Enter the elements.
6
The elements of the array are: 5 9 4 7 6
ASSIGNMENT – 6.
6.a.) Write a C program of find both the largest and smallest number
from a given array.
Ans.)
#include<stdio.h>
int main()
{
int arr[100],n,i,largest,smallest;
printf("Enter the number of elements you want to insert :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("\n");
printf("DISPLAY ALL THE ELEMENTS TAKEN BY USERS\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
largest=arr[0];
smallest=arr[0];
for(i=1;i<n;i++)
{
if(arr[i]>largest)
{
largest=arr[i];
}
if(arr[i]<smallest)
{
smallest=arr[i];
}
}
printf("\n");
printf("\nLargest element is : %d",largest);
printf("\nSmallest element is : %d",smallest);
return 0;
}
Output: -
Enter the number of elements you want to insert:
5
Enter element 1: 66
Enter element 2: 59
Enter element 3: 95
Enter element 4: 84
Enter element 5: 40
3 5
7 9
1 5
9 3
for(j=0;j<c1;j++)
{
scanf("%d",&mat_1[i][j]);
}
}
printf("\n");
printf("After taking input of 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",mat_1[i][j]);
}
printf("\n");
}
printf("\n");
printf("UPPER TRIANGULAR OF THE MATRIX\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(i<=j)
{
printf("%d ",mat_1[i][j]);
}
else
{
printf("%d ",0);
}
}
printf("\n");
}
return 0;
}
Output: -
Enter the number of rows number: 3
Enter the number of column number: 3
for(j=0;j<c1;j++)
{
scanf("%d",&mat_1[i][j]);
}
}
printf("\n");
printf("After taking input of 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",mat_1[i][j]);
}
printf("\n");
}
printf("\n");
printf("LOWER TRIANGULAR OF THE MATRIX\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(i>=j)
{
printf("%d ",mat_1[i][j]);
}
else
{
printf("%d ",0);
}
}
printf("\n");
}
return 0;
}
Output: -
Enter the number of rows number: 3
Enter the number of column number: 3
#include<stdio.h>
#include<string.h>
int main()
{
char str[]={"MADAM"};
int l=0;
int h=strlen(str)-1;
while(h>1)
{
if(str[l++]!=str[h--])
{
printf("%s\nIs not a palindrome.\n",str);
return 0;
}
}
printf("%s\nIs a palindrome.\n",str);
return 0;
}
Output: -
MADAM
Is a palindrome.
7.b) Write a C program to count the number of Words, Digits, Vowel &
Consonant in a give text.
Ans.)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
char line[150];
int vowels,consonant,digit,space;
vowels=consonant=digit=space=0;
printf("Enter a line of string:\n");
fgets(line,sizeof(line),stdin);
for(int i=0;line[i]!='\0';++i)
{
line[i]=tolower(line[i]);
if(line[i]=='a'||line[i]=='e'||line[i]=='i'||line[i]=='o'||line[i]=='u')
{
++vowels;
}
else if((line[i]>='a'&&line[i]<='z'))
{
++consonant;
}
else if(line[i]>='0'&&line[i]<='9')
{
++digit;
}
else if(line[i]==' ')
{
++space;
}
}
printf("Vowels:%d",vowels);
printf("\nConsonants:%d",consonant);
printf("\nDigit:%d",digit);
printf("\nWhite space:%d",space);
return 0;
}
Output: -
Output: -
Pointer Example Program: Find or Calculate Length of String
Enter Any string[below 50 chars]:
I feel happy when I completed my lab assignment
Length of String: 47
7.d) Write a C program to copy one string to another using pointer.
Ans.)
#include<stdio.h>
void copy_string(char*,char*);
main()
{
char source[100],target[100];
printf("Enter source string.\n");
gets(source);
copy_string(target,source);
printf("Target string is \"%s\"\n",target);
return 0;
}
void copy_string(char *target,char *source)
{
while(*source)
{
*target=*source;
source++;
target++;
}
*target='\0';
}
Output: -
Enter source string.
Iswar Chandra Vidyasagar Polytechnic
Target string is “Iswar Chandra Vidyasagar Polytechnic”