Combined C Questions PDF
Combined C Questions PDF
Program: Write a program to input integer data, test bit value of given
position. (Using Conditional Operator)
#include<stdio.h>
int main()
{
int data;
int bitPosn;
printf("Enter The Data : ");
scanf("%d",&data);
printf("Enter The Bit Posn : ");
scanf("%d",&bitPosn);
((data>>bitPosn)&1)?printf("Set Bit\n"):printf("Clear Bit\n");
}
Program: Write a program to input integer data, test bit value of given
position.
#include<stdio.h>
int main()
{
int data;
int bitPosn;
printf("Enter The Data : ");
scanf("%d",&data);
INPUT: printf("Enter The BitPosn (0 to %lu) : ",sizeof(int)*8-1);
scanf("%d",&bitPosn);
if((bitPosn<0)||(bitPosn>=sizeof(int)*8))
{
printf("Error : Invalid bit Supply\n");
goto INPUT;
}
printf("Bit : %d in data : %d is : %d\n",bitPosn,data,(data>>bitPosn)&1);
}
Program: Write a program to print the binary of given input data.
#include<stdio.h>
int main()
{
int data;
int bitPosn;
printf("Enter The data : ");
scanf("%d",&data);
printf("Enter bit Equt. of : %d\n",data);
bitPosn=31;
TEST: printf("%d",(data>>bitPosn)&1);
if(bitPosn%8==0)
printf(" ");
bitPosn--;
if(bitPosn>=0)
goto TEST;
printf("\n");
}
Program: Write a program to print the type (category) uppercase,
lowercase, Numeric character, Special character by using library function.
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter The Character : ");
scanf("%c",&ch);
isupper(ch)?printf("Uppercase Character\n"):
islower(ch)?printf("Lowercase Charcater\n"):
isdigit(ch)?printf("Digit Character\n"):
printf("Special Character\n");
}
Program: Write a program to print the type (category) uppercase,
lowercase, Numeric character, Special character by using if statement.
#include<stdio.h>
int main()
{
char ch;
printf("Enter The Character : ");
scanf("%c",&ch);
if((ch>='A')&&(ch<='Z'))
printf("Uppercase Character\n");
if((ch>='a')&&(ch<='z'))
printf("Lowercase Character\n");
if((ch>='0')&&(ch<='9'))
printf("Digit\n");
if(!(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))||((ch>='0')&&(ch<='9'))))
printf("Special Character\n");
}
Program: Write a program to print the type (category) uppercase,
lowercase, Numeric character, Special character by using else if statement.
#include<stdio.h>
int main()
{
char ch;
printf("Enter The Character : ");
scanf("%c",&ch);
if((ch>='a')&&(ch<='z'))
printf("Lowercase Character\n");
else if((ch>='A')&&(ch<='Z'))
printf("Uppercase Character\n");
else if((ch>='0')&&(ch<='9'))
printf("Digit\n");
else
printf("Special Character\n");
}
Program: Write a program to scan 3 integers and find the relation between
them. Find which is higher, all three, two of them or only one using only
conditional operator.
#include<stdio.h>
int main()
{
int a;
int b;
int c;
printf("Enter The Integer a : ");
scanf("%d",&a);
printf("Enter The Integer b : ");
scanf("%d",&b);
printf("Enter The Integer c : ");
scanf("%d",&c);
((a>b)&&(a>c))?printf("a is max\n"):
((b>a)&&(b>c))?printf("b is max\n"):
((c>a)&&(c>b))?printf("c is max\n"):
((a==b)&&(a>c))?printf("a and b max\n"):
((b==c)&&(b>c))?printf("b and c max\n"):
((c==a)&&(c>b))?printf("c and a max\n"):
printf("a, b, and c max\n");
}
Program: Write a program to convert lowercase to uppercase and vice
versa.
Method 1:
#include<stdio.h>
int main()
{
char ch;
printf("Enter The Character : ");
scanf("%c",&ch);
if((ch>='A')&&(ch<='Z'))
ch+=32;
else
ch-=32;
printf("Convetred Character is : %c\n",ch);
}
Method 2:
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
printf("Enter The Character : ");
scanf("%c",&ch);
if(isalpha)
{
ch^=32;
printf("Converted Case is : %c\n",ch);
}
else
{
printf("Error : Invalid Input\n");
}
}
Program: Write a program to scan 3 integers and find the complete
relation between them using conditional operator only.
#include<stdio.h>
int main()
{
int a;
int b;
int c;
printf("Enter The Integer a : ");
scanf("%d",&a);
printf("Enter The Integer b : ");
scanf("%d",&b);
printf("Enter The Integer c : ");
scanf("%d",&c);
if((bit>=0)&&(bit<=31))
{
if((data>>bit)&1)
printf("Bit is Set\n");
else
printf("Bit is Clear\n");
}
else
{
printf("Invalid Bit: Enter Bit Again\n");
goto INPUT;
}
}
Program: Write a program to print the binary of an integer value entered.
#include<stdio.h>
int main()
{
int data;
int bit;
printf("Enter The data : ");
scanf("%d",&data);
bit=31;
TEST: if((data>>bit)&1)
printf("1");
else
printf("0");
bit--;
if(bit>=0)
goto TEST;
printf("\n");
}
Program: Write a program to calculate percentage scored by a student,
whose 5 subject marks are entered by user. Find the gradation. (Using
method – if, else if ladder, nested if)
Method 1: Using if Statement –
#include<stdio.h>
int main()
{
float a,b,c,d,e;
float p;
printf("Enter subject 1 marks : ");
scanf("%f",&a);
printf("Enter subject 2 marks : ");
scanf("%f",&b);
printf("Enter subject 3 marks : ");
scanf("%f",&c);
printf("Enter subject 4 marks : ");
scanf("%f",&d);
printf("Enter subject 5 marks : ");
scanf("%f",&e);
p=((a+b+c+d+e)/5);
printf("Scored Percentage : %f\n",p);
printf("Earned Grage : ");
if((p>=0)&&(p<=40))
printf("Fail\n");
if((p>=40)&&(p<=50))
printf("Third Class\n");
if((p>=50)&&(p<=60))
printf("Second Class\n");
if((p>=60)&&(p<=75))
printf("First Class\n");
if((p>=75)&&(p<=100))
printf("Destionction\n");
}
Using else if statement –
#include<stdio.h>
int main()
{
float a,b,c,d,e;
float p;
printf("Enter subject 1 marks : ");
scanf("%f",&a);
printf("Enter subject 1 marks : ");
scanf("%f",&b);
printf("Enter subject 1 marks : ");
scanf("%f",&c);
printf("Enter subject 1 marks : ");
scanf("%f",&d);
printf("Enter subject 1 marks : ");
scanf("%f",&e);
p=((a+b+c+d+e)/5);
printf("Scored Percentage : %f\n",p);
printf("Earned Grade : ");
if((p>=0)&&(p<=40))
printf("Fail\n");
else if((p>=40)&&(p<=50))
printf("Third Class\n");
else if((p>=50)&&(p<=60))
printf("Second Class\n");
else if((p>=60)&&(p<=75))
printf("First Class\n");
else
printf("Distinction\n");
}
Nested if else –
#include<stdio.h>
int main()
{
float a,b,c,d,e,f;
float p;
p=((a+b+c+d+e)/5);
printf("Scored Percentage is : %f\n",p);
printf("Earned Grade : ");
if(p>40)
{
if(p>50)
{
if(p>60)
{
if(p>75)
printf("Destinction\n");
else
printf("1st Class\n");
}
else
printf("2nd Class\n");
}
else
printf("3rd class\n");
}
else
printf("Fail\n");
}
Program: Write a program to print all the character and their respective
ASCII equivalent (Without using loop statement)
#include<stdio.h>
int main()
{
int data=0;
printf("character\tASCII\n");
LOOP: printf("%c\t%d\n",data,data);
data++;
if(data<128)
goto LOOP;
}
*
**
***
****
*****
2)
#include<stdio.h>
int main()
{
int a;
int b;
for(a=1;a<=5;a++,printf("\n"))
{
for(b=1;b<=5-a;b++)
printf(" ");
for(b=1;b<=a;b++)
printf("* ");
}
}
*
**
***
****
*****
3)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=1;j<=5;j++)
(j<i)?printf(" "):printf("* ");
}
*****
****
***
**
*
4)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=1;j<=5;j++)
(j<i)?printf(" "):printf("* ");
}
*****
****
***
**
*
5)
#include<stdio.h>
int main()
{
int i,j,flag=1;
for(i=1;i<=5;i++,printf("\n"))
{
if(i%2==0)
flag=0;
else
flag=1;
for(j=1;j<=i;j++)
{
printf("%d ",flag);
flag=!flag;
}
}
}
1
01
101
0101
10101
6)
#include<stdio.h>
int main()
{
int i,j,flag=65;
for(i=1;i<=5;i++,printf("\n"))
{
for(j=5,flag=65;j>=i;j--)
{
printf("%c ",flag);
flag++;
}
}
}
A B C D E
A B C D
A B C
A B
A
7)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=5;j>=1;j--)
(j>i)?printf(" "):printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=1;j<=5;j++)
(j<=i)?printf(" "):printf("* ");
}
*
**
***
****
*****
****
***
**
*
8)
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++,printf("\n"))
for(j=5;j>=1;j--)
(j>i)?printf(" "):printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=1;j<=5;j++)
(j<=i)?printf(" "):printf("* ");
}
*
**
***
****
*****
****
***
**
*
9)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=1;j<=i;j++)
printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=5;j>i;j--)
printf("* ");
}
*
**
***
****
*****
****
***
**
*
10)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=1;j<=5;j++)
(j<i)?printf(" "):printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=5;j>=1;j--)
(j>i+1)?printf(" "):printf("* ");
}
*****
****
***
**
*
**
***
****
*****
11)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=1;j<=5;j++)
(j<i)?printf(" "):printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=5;j>=1;j--)
(j>i+1)?printf(" "):printf("* ");
}
*****
****
***
**
*
**
***
****
*****
12)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=5;j>=i;j--)
printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=1;j<=i+1;j++)
printf("* ");
}
*****
****
***
**
*
**
***
****
*****
13)
#include<stdio.h>
int main()
{
int a,b,c,n,f=1;
char ch=65;
printf("Enter The Number N : ");
scanf("%d",&n);
for(a=1;a<=n;a++,printf("\n"))
{
f=1;
for(b=1;b<=n;b++)
{
if(b<=(n-a))
printf(" ");
else if(f==1)
{
printf("%c",ch);
f=0;
}
else if(b==n)
{
printf("*%c",ch);
}
else
{
printf("**");
}
}
ch++;
}
}
Output:
Enter The Number N : 5
A
B*B
C***C
D*****D
E*******E
14)
#include<stdio.h>
int main()
{
int a,b,B,n;
int f;
char ch;
printf("Enter The N : ");
scanf("%d",&n);
for(a=0;a<n;a++,printf("\n"))
{
f=1;
ch=65;
for(b=-(n-1);b<n;b++)
{
if(b<0)
B=-b;
else
B=b;
if(B<=a)
{
if(f==1)
{
printf("%c",ch++);
f=0;
}
else
{
printf("*");
f=1;
}
}
else
{
printf(" ");
}
}
}
}
Output:
Enter The N : 5
A
A*B
A*B*C
A*B*C*D
A*B*C*D*E
15)
#include<stdio.h>
int main()
{
int a,b,n;
printf("Enter The N : ");
scanf("%d",&n);
for(a=n;a>0;a--,printf("\n"))
for(b=1;b<=a;b++)
{
if(a%2==0)
printf("*");
else
printf("%d",a);
}
}
Output:
Enter The N : 5
55555
****
333
**
1
Program: Write a program to reverse bits of a given integer number.
#include<stdio.h>
int main()
{
int data;
int bit;
int i,j;
printf("Enter The data : ");
scanf("%d",&data);
for(bit=31;bit>=0;bit--)
printf("%d",(data>>bit)&1);
printf("\n");
for(i=31,j=0;i>j;i--,j++)
{
if(((data>>i)&1) != ((data>>j)&1))
{
data=data^(1<<i);
data=data^(1<<j);
}
}
for(bit=31;bit>=0;bit--)
printf("%d",(data>>bit)&1);
printf("\n");
}
Program: Write a program to count the digits of an integer number.
#include<stdio.h>
int main()
{
int data;
int count=0;
if(y==x)
{
flag=1;
goto END;
}
for(temp=x;temp>1;temp=temp/y)
{
if(temp%y==0)
{
count++;
continue;
}
else
{
flag=1;
break;
}
}
END: if(flag==1)
{
if(x==y)
printf("%d is 1 power of %d\n",x,y);
else
printf("%d is not power of %d\n",x,y);
}
else
printf("%d is %d power of %d\n",x,count,y);
}
Program: Write a program to check inter number is prime or not.
#include<stdio.h>
#include<math.h>
int main()
{
int data;
int i;
int s;
printf("Enter The Data : ");
scanf("%d",&data);
s=sqrt(data);
for(i=2;i<=s;i++)
{
if((data%i)==0)
break;
}
if(i==s+1)
printf("Entered Number is Prime Number\n");
else
printf("Entered Number is Not Prime Number\n");
}
Program: Write a program to print the prime number in given range.
#include<stdio.h>
#include<math.h>
int main()
{
int min,max;
int num;
int i,s;
printf("Enter The Min Number : ");
scanf("%d",&min);
printf("Enter The Max Number : ");
scanf("%d",&max);
for(num=min;num<=max;num++)
{
s=sqrt(num);
for(i=2;i<=s;i++)
{
if((num%i)==0)
break;
}
if(i==s+1)
printf("%d, ",num);
}
printf("\n");
}
#include<stdio.h>
#include<math.h>
int testPrime(int);
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
if(testPrime(data))
printf("%d is Prime\n",data);
else
printf("%d is not-Prime\n",data);
}
int testPrime(int data)
{
int i,s;
s=sqrt(data);
for(i=2;i<=s;i++)
{
if(data%i==0)
break;
}
if(i==(s+1))
return 1;
else
return 0;
}
Program: Write a program to print multiplication table in given range.
#include<stdio.h>
int main()
{
int min,max;
int temp,i,j;
int sum=0;
for(temp=max*10;temp;temp=temp/10)
sum++;
for(i=min;i<=max;i++)
{
printf("%*d : ",sum-1,i);
for(j=1;j<=10;j++)
printf("%*d",sum,i*j);
printf("\n");
}
}
Program: Write a program to check enter number is prime or not.
#include<stdio.h>
#include<math.h>
int testPrime(int);
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
if(testPrime(data))
printf("Entered Number is Prime\n");
else
printf("Enter Number is Not Prime\n");
}
int testPrime(int data)
{
int squre;
int i;
squre=sqrt(data);
for(i=2;i<=squre;i++)
{
if(data%i==0)
break;
}
if(i==(squre+1))
return 1;
else
return 0;
}
Program: Write a program to check weather enter number is palindrome
or not.
#include<stdio.h>
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
if(testRev(data)==data)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
}
int testRev(int data)
{
int rev=0;
while(data)
{
rev=rev*10+data%10;
data/=10;
}
return rev;
}
Program: Write a program list the numbers between min and max, where
sum of digits is 9.
#include<stdio.h>
int sumDigits(int);
int main()
{
int min,max;
int data;
printf("Enter The Min Digit : ");
scanf("%d",&min);
printf("Enter The Max Digit : ");
scanf("%d",&max);
for(data=min;data<=max;data++)
if(sumDigits(data)==9)
printf("%d, ",data);
printf("\n");
}
int sumDigits(int data)
{
int sum=0;
while(data)
{
sum=sum+data%10;
data/=10;
}
return sum;
}
Program: Write a program to make arrangement to list the numbers the
sum of digits, reduce to single digit is 9.
#include<stdio.h>
int main()
{
int min,max;
int data;
int sum=0;
int temp;
printf("Enter The Min Digit : ");
scanf("%d",&min);
printf("Enter The Mix Digit : ");
scanf("%d",&max);
for(data=min;data<=max;data++)
{
temp=data;
while(temp)
{
sum=sum+temp%10;
temp/=10;
}
if(sum>9)
{
temp=sum;
sum=0;
while(temp)
{
sum=sum+temp%10;
temp/=10;
}
}
if(sum==9)
printf("%d, ",data);
}
printf("\n");
}
Program: Write a program to make arrangement to list the numbers if
digits are in sorted order in ascending order.
#include<stdio.h>
int testDigits(int);
int testAsc(int);
int main()
{
int min,max;
int data;
printf("Enter The Min Value : ");
scanf("%d",&min);
printf("Enter The Max Value : ");
scanf("%d",&max);
for(data=min;data<=max;data++)
if(testDigits(data)==9)
if(testAsc(data))
printf("%d, ",data);
}
int testDigits(int data)
{
int sum=0;
while(data)
{
sum=sum+data%10;
data/=10;
}
return sum;
}
int testAsc(int data)
{
int curDig;
int preDig=9;
while(data)
{
curDig=data%10;
if(curDig>preDig)
return 0;
preDig=curDig;
data/=10;
}
if(data==0)
return 1;
else
return 0;
}
Program: Write a program to print the N numbers starting from S.
#include<stdio.h>
#include<math.h>
int testPrime(int);
int main()
{
int num,strt,data;
int cnt=0;
printf("Enter Numbers of prime Number you want : ");
scanf("%d",&num);
printf("Enter Starting Number : ");
scanf("%d",&strt);
for(data=strt;cnt<num;data++)
{
if(testPrime(data))
{
printf("%d, ",data);
cnt++;
}
}
printf("\n");
}
int testPrime(int data)
{
int s,i;
s=sqrt(data);
for(i=2;i<=s;i++)
{
if(data%i==0)
break;
}
if(i==(s+1))
return 1;
else
return 0;
}
Program: Write a program to find the factorial of a number by recursive
method.
#include<stdio.h>
int fact(int);
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
printf("Factorial Of %d is %d\n",data,fact(data));
}
int fact(int data)
{
if(data==0)
return(1);
else
return(data*fact(data-1));
}
Program: Write a program to generate Fibonacci series using recursion.
#include<stdio.h>
int fib(int);
int main()
{
int data,i;
printf("Enter The Data : ");
scanf("%d",&data);
for(i=0;i<data;i++)
printf("%d, ",fib(i));
printf("\n");
}
#include<stdio.h>
int main()
{
int year;
printf("Enter The Year : ");
scanf("%d",&year);
if(year%4==0&&year%100!=0||year%400==0)
printf("Lear Year\n");
else
printf("Not Leap Year\n");
}
Program: Write a program to print the sum of digits using recursive
method.
#include<stdio.h>
int sumDigits(int);
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
printf("Sum of Digit in %d is %d\n",data,sumDigits(data));
}
s2[i]=s1[i];
s2[i]=0;
printf("S2 : %s\n",s2);
}
Program: Write a program to copy string using built in function strcpy.
#include<stdio.h>
#include<string.h>
int main()
{
char str_1[100];
char str_2[100];
printf("Enter The String - 1 : ");
gets(str_1);
strcpy(str_2,str_1);
printf("String - 2 is : %s\n",str_2);
}
if(((str_1[i]>='a')&&(str_1[i]<='z'))||((str_1[i]>='A')&&(str_1[i]>='A')&&(str_1
[i]<='Z')))
continue;
else
{
memmove(str_1+i,str_1+i+1,strlen(str_1+i));
i--;
}
}
printf("Modified String : %s\n",str_1);
}
Program: Write a program to input a string and a string character and
remove all occurrences of the character from the string.
#include<string.h>
#include<stdio.h>
int main()
{
char str[100];
int i;
char ch;
printf("Enter The String : ");
gets(str);
printf("Enter The Character : ");
scanf("%s",&ch);
for(i=0;str[i];i++)
{
if(str[i]==ch)
{
memmove(str+i,str+i+1,strlen(str+i+1)+1);
i--;
}
}
printf("Modified String : %s\n",str);
}
Program: Write a program to print the character position in entered
string.
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Enter The String : ");
gets(str);
char ch;
printf("Enter The Character : ");
scanf("%c",&ch);
if(strchr(str,ch)==NULL)
printf("Not Found\n");
else
printf("Found at index : %u\n",strchr(str,ch)-str);
}
Program: Write a program to print the character position in entered
string.
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
char ch;
char *ptr;
printf("Enter The String : ");
gets(str);
printf("Enter The Characater : ");
scanf("%c",&ch);
ptr=strchr(str,ch);
if(ptr==NULL)
printf("Not Found\n");
else
printf("Found at index : %u\n",ptr-str);
}
Program: Write user defined strchr() Function.
char *user_strchr(const char *str,int ch)
{
int i;
for(i=0;str[i];i++)
{
if(str[i]==ch)
return str+i;
}
return NULL;
}
Program: Write a program to remove extra repetitions of all characters in
a given string.
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
char *ptr;
printf("Enter The String : ");
gets(str);
int i;
for(i=0;str[i];i++)
{
while(ptr=strchr(str+i+1,str[i]))
memmove(ptr,ptr+1,strlen(ptr+1)+1);
}
printf("Modified String : %s\n",str);
}
Program: Write a program to input a string and find out how many
characters are repeated.
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
char *ptr;
char temp[100];
int cnt=0;
printf("Enter The String : ");
gets(str);
strcpy(temp,str);
for(int i=0;temp[i];i++)
{
if(strchr(temp+i+1,temp[i]))
{
cnt++;
while(ptr=strchr(temp+i+1,temp[i]))
memmove(ptr,ptr+1,strlen(ptr+1)+1);
}
}
printf("No. of Characters Repeating : %d\n",cnt);
}
Program: Write user defined strrchr() Function.
char *user_strrchr(const char *str,int ch)
{
int i;
int len=-1;
for(i=0;str[i];i++)
{
if(str[i]==ch)
len=i;
}
if(len>=0)
return str+1;
else
return NULL;
}
Program: Write a program to find the min and max numbers in an array.
#include<stdio.h>
int main()
{
int i=0,n,min;
int arr[5];
for(i=0;i<n;i++)
{
puts("Enter the numbers: ");
scanf("%d",&arr[i]);
}
puts("Elements of Array : ");
for(int j=0;j<5;j++)
printf("%d",arr[j]);
n=arr[0];
for(int j=1;j<5;j++)
{
if(n<arr[j])
n = arr[j];
}
min=arr[0];
int temp;
for(int j=1;j<5;j++)
{
if(min>arr[j])
min = arr[j];
}
printf("Largest no is: %d\n",n);
printf("Smallest No is: %d\n",min);
}
Program: Write a program to insert insertion sort.
#include<stdio.h>
void insert_sort(int *p);
int main()
{
int i=0,n;
int arr[5];
for(i=0;i<5;i++)
{
puts("Enter the numbers:");
scanf("%d",&arr[i]);
}
puts("Before Sort");
for(int j=0;j<5;j++)
printf("%d",arr[j]);
insert_sort(arr);
}
void insert_sort(int *p)
{
int temp;
int j;
for(int i=1;i<5;i++)
{
temp=p[i];
j=i-1;
while(j>=0)
{
if(temp<p[j])
{
p[j+1]=p[j];
j--;
}
else
break;
}
p[j+1]=temp;
}
puts("After Soting:");
for(int x=0;x<5;x++)
printf("%d, ",p[x]);
printf("\n");
}
Program: Write a program to find the highest value from an array using 1
loop.
#include<stdio.h>
int main()
{
int i=0,n;
int arr[5];
for(i=0;i<5;i++) //This loop is only taken for getting array assigned if we
don’t want this for loop we can use arr[]={5,7,9,5,1};
{
printf("Enter The Numbers:");
scanf("%d",&arr[i]);
}
n=arr[0];
for(int j=1;j<5;j++)
{
if(n<arr[j])
n = arr[j];
}
printf("Largest No is: %d\n",n);
}
Program: Write a Program to print binary equivalent of float using union.
#include<stdio.h>
union t
{
float f;
struct st
{
unsigned int mentisa:23; //Bit field of 23 bit for mantissa part.
unsigned int exponant:8; //Bit Field of 8 bit for exponent part.
unsigned int sign:1; //Bit filed of 1 bit for signed part
}row;
}u;
int main()
{
union t v;
puts("Enter Floating Real Value:");
scanf("%f",&v.f); //Getting Real Value Here
printf("Binary Form is: ");
printf("%d ",v.row.sign); //1st Printing Signed bit of Real value
/* For Exponent part */
for(int i=8;i>=0;i--) //loop for printing binary value till 8 bit
{
if((v.row.exponant >> i) & 1) //if exponant of real value (in binary)
is true then print 1
printf("1");
else
printf("0");
}
/* For Mentisa Part */
for(int i=23;i>=0;i--)
{
if((v.row.mentisa >> i) & 1)
printf("1");
else
printf("0");
if(i%8==0)
{
printf(" ");
}
}
printf("\n");
}
Program: To find GCD of two numbers.
#include <stdio.h>
int main(int argc,char *argv[])
{
int a,b,small,i;
a=atoi(argv[1]);
b=atoi(argv[2]);
if(a>b)
small=b;
else
small=a;
for(i=small;i>=1;i--)
{
if((a%i)==0&&(b%i)==0)
{
printf("%d",i);
break;
}
}
return 0;
}
Program: Write a program to find the lcm of two numbers.
#include <stdio.h>
int main(int argc,char *argv[])
{
int a,b,large;
a=atoi(argv[1]);
b=atoi(argv[2]);
if(a>b)
large=a;
else
large=b;
while(1)
{
if((large%a)==0&&(large%b)==0)
{
printf("%d",large);
break;
}
large++;
}
return 0;
}
Program. Write a program to find the area of a circle (area=3.14*r*r),
when diameter is given.
#include <stdio.h>
#define PI 3.14
int main(int argc,char *argv[])
{
float dia,radius,area=0;
dia=atoi(argv[1]);
radius=0.5*dia;
area=PI*radius*radius;
printf("%.2f",area);
return 0;
}
Program: Write a program to check whether given number is strong
number or not.
#include<stdio.h>
int fact_data(int);
int main(int argc, char *argv[])
{
int num,d,n,res=0,i,count=0,x;
n=atoi(argv[1]);
num=n;
x=num;
while(n!=0)
{
n=n/10;
count++;
}
for(i=0;i<count;i++)
{
if(x>0)
{
d=x%10;
res=res+fact_data(d);
x=x/10;
}
}
if(res==num)
{
printf("Strong Number\n");
}
else printf("Not Strong Number\n");
return 0;
}
int fact_data(int x)
{
if(x==0)
return 1;
else
return x*fact_data(x-1);
}
Program: Write a program to print the CPU is little endian or big endian.
#include<stdio.h>
union tag
{
int i;
char ch;
};
int main()
{
union tag var={1};
if(var.ch)
printf("CPU is byte Order is Little Endian\n");
else
printf("CPU is byte Order is Big Endian\n");
}
Program: Write a program to find the 2nd highest digit in a supplied
integer
Note: Use a single loop.
#include<stdio.h>
int main ()
{
int data,rem,largest=0,Sec_large=0;
printf("Enter the Number :");
scanf("%d",&data);
while(data>0)
{
rem=data%10;
if(largest<rem)
{
Sec_large=largest;
largest=rem;
}
else if(rem>=Sec_large)
Sec_large=rem;
data=data/10;
}
printf("The Second Largest Digit is %d\n", Sec_large);
return 0;
}
Program: Write a Program to calculate sum-of-Odd-digits in an integer.
#include<stdio.h>
int sumofodddigits(int);
int main()
{
int data;
printf("Enter a Number: ");
scanf("%d",&data);
printf("Sum of Odd Digits: %d\n",sumofodddigits(data));
}
int main()
{
FILE *fptr;
int i,n;
/*Writing The Data In The File using fscanf()*/
fptr=fopen("student_data.dat","w");
printf("Enter Number Of Records : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter Name : ");
scanf("%s",stu1.name);
printf("Enter The Percentage : ");
scanf("%f",&(stu1.percentage));
fprintf(fptr,"%s%f",stu1.name,stu1.percentage);
}
fclose(fptr);
/*Reading The Data From File Using fscanf()*/
fptr=fopen("student.dat","r");
printf("Name\nPercentage\n");
while(fscanf(fptr,"%s%f",stu2.name,&(stu2.percentage))!=EOF)
printf("%s\t%f\n",stu2.name,stu2.percentage);
fclose(fptr);
}
Program: Write a program to read and write one block of character at a
time.
#include<stdio.h>
struct student
{
char name[20];
int roll;
float percentage;
}stu1,stu2;
int main()
{
FILE *fptr;
int i,n;
/*Writing Data into File Using Fwrite*/
fptr=fopen("student.dat","wb");
printf("Enter Numbers of Record : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter The Name : ");
scanf("%s",stu1.name);
printf("Enter The Roll Number : ");
scanf("%d",&(stu1.roll));
printf("Enter The Percentage : ");
scanf("%f",&(stu1.percentage));
fwrite(&stu1,sizeof(stu1),1,fptr);
}
fclose(fptr);
/*Reading Data From File Using fread()*/
fptr=fopen("student.dat","rb");
printf("Name:\tRoll Number:\tPercentage:\n");
while(fread(&stu2,sizeof(stu2),1,fptr)==1)
{
printf("%s\t",stu2.name);
printf("%d\t",stu2.roll);
printf("%f\t",stu2.percentage);
}
fclose(fptr);
}
Program: Write a program to understand the fseek() function
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
int roll;
float percentage;
}stu1;
int main()
{
FILE *fptr;
int n;
fptr=fopen("student.dat","rb");
if(fptr==NULL)
{
printf("Error in opening file\n");
exit(1);
}
printf("Enter The Record Number to be read : ");
scanf("%d",&n);
fseek(fptr,(n-1)*sizeof(stu1),0); //Skip n-1 record
fread(&stu1,sizeof(stu1),1,fptr); //Read the nth Record
printf("Name\tRollNo\tPercentage\n");
printf("%s\t",stu1.name);
printf("%d\t",stu1.roll);
printf("%f\t",stu1.percentage);
fclose(fptr);
}
Program: Write a program to understand the use of ftell().
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
int roll;
float percentages;
}stu1;
int main()
{
FILE *fptr;
fptr=fopen("student.dat","rb");
if(fptr==NULL)
{
printf("Error in opening File\n");
exit(1);
}
printf("Position pointer in the begning : %ld\n",ftell(fptr));
while(fread(&stu1,sizeof(stu1),1,fptr)==1)
{
printf("Position Pointer : %ld\n",ftell(fptr));
printf("%s\t",stu1.name);
printf("%d\t",stu1.roll);
printf("%f\t",stu1.percentages);
}
printf("Size of File in Bytes is : %ld\n",ftell(fptr));
fclose(fptr);
}
Program: Write a program to understand the use of rewind().
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fptr;
fptr=fopen("student.dat","rb");
if(fptr==NULL)
{
printf("Error in opening File\n");
exit(1);
}
printf("Position Pointer : %ld\n",ftell(fptr));
fseek(fptr,0,2);
printf("posotion Pointer : %ld\n",ftell(fptr));
rewind(fptr);
printf("Position Pointer : %ld\n",ftell(fptr));
fclose(fptr);
}
Program: Write a program to Open and Close a text file.
#include<stdio.h>
int main()
{
FILE *fptr;
char file_name[]="file.txt";
if((fptr=fopen(file_name,"r"))==NULL)
{
printf("Cannot Open : %s\n",file_name);
}
else
{
printf("FILE Open Sucessfully\n");
printf("Ready To Close The FILE\n");
fclose(fptr);
}
}
Program: Write a program to read and write one character at a time
#include<stdio.h>
int main()
{
FILE *fptr;
char ch;
/*Writing Text into File*/
fptr=fopen("text.txt","w");
printf("Enter The Text : ");
/* Press ctrl+d to stop reading character*/
while((ch=getchar())!=EOF)
fputc(ch,fptr);
fclose(fptr);
for(;min<=max;min++)
{
if(testPrime(min))
if(primeDigit(min,digit))
{
printf("Prime Digits are :%d\n",min);
}
}
if(digit%2==0)
printf("No Number present is this range with entered digit\n");
}
int primeDigit(int data, int digit)
{
while(data)
{
if(digit%2==0)
return 0;
if(data%10==digit)
return 1;
data/=10;
}
return 0;
}
while(ptr=strstr(str,str_1))
{
memset(ptr,'*',strlen(str_1)-1);
ptr=(ptr+strlen(str_1)-1);
}
printf("%s\n",str);
}
Program: Write a program to reverse the world which contains 2 or more
than 2 vowels in a supplied string.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char str[50];
scanf("%[^\n]s",str);
char ptr[20]="aeiou";
char ptr1[20]="AEIOU";
char temp[50]="";
char str1[50];
int i,j,n,k,c,c1,c2;
n=strlen(str)-1;
for(j=0,c1=0;j<=n;j++)
if(isupper(str[j]))
{
for(k=0;k<strlen(ptr1);k++)
if(str[j]==ptr1[k])
c1++;
}
for(i=0;i<n;i++){
for(j=0;str[i]!=' '&&i<=n;j++,i++)
str1[j]=str[i];
str1[j]='\0';
if(c1>=2)
{
for(j=0,c=0;j<strlen(str1);j++)
for(k=0;k<strlen(ptr1);k++)
{
if(str1[j]==ptr1[k])
c++;
}
if(c>=1)
rev(str1);
}
else
{
for(j=0,c=0,c2=0;j<strlen(str1);j++)
for(k=0;k<strlen(ptr);k++)
{
if(str1[j]==ptr[k])
c++;
if(str1[j]==ptr1[k])
c2++;
if(str1[j]=='"')
str1[j]='\0';
}
if(c>=2||c2>=1)
rev(str1);
}
strcat(temp,str1);
strcat(temp," ");
}
if(c1>=2||c1>=1)
printf("%s",temp);
else
{
if(temp[strlen(temp)-1]==' ')
temp[strlen(temp)-1]='\0';
printf("ouput=%s",temp);
}
}
for(int i=1;i<=num_1&&i<=num_2;i++)
{
if(num_1%i==0 && num_2%i==0)
gcd=i;
}
lcm=(num_1*num_2)/gcd;
printf("GCD is : %d\n",gcd);
printf("LCM is : %d\n",lcm);
}
Program: Write a program to print all the LEADERS in the array, an
Element is a leader if it is greater than all the elements to its tight side.
For: Example, int the array {16,17,4,3,5,2}, leaders are 17,5, and 2.
#include<stdio.h>
int main()
{
int a[20];
int i,j,n;
printf("Enter The No. To be input : ");
scanf("%d",&n);
if(n<0)
{
printf("Invalid Choice\n");
return 0;
}
for(i=0;i<n;i++)
{
printf("Enter The Element of array : ");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
break;
}
if(j==n)
printf("LADDR is : %d\n",a[i]);
}
}
Program: Write a program to convert temperature form to F or vice versa.
1. For Fahrenheit to Celsius
2. For Celsius to Fahrenheit
#include<stdio.h>
int main()
{
printf("1 - For Fahrenheit to Celsius\n");
printf("2 - For Celsius To Fahrenheit\n");
int choice;
float fahr,celc;
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the Data : " );
scanf("%f",&celc);
fahr=(celc*9/5)+32;
printf("Farenite Data is : %f\n",fahr);
break;
case 2: printf("Enter The Data : ");
scanf("%f",&fahr);
celc=(fahr-32)*5/9;
printf("Centrigrate Data is : %f\n",celc);
break;
}
}
Program: Write a program to implant user-defended atio function.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char str[20];
int i;
printf("Enter The String : ");
scanf("%s",str);
if(isalpha(str[0]))
{
printf("0");
return 0;
}
for(i=0;i<strlen(str);i++)
if((str[i]>='0'&&str[i]<='9')||(str[i]=='+'||str[i]=='-'))
printf("%c",str[i]);
else if(isalpha(str[i]))
return 0;
}
Program: Write a program to print the binary equivalent of float.
#include<stdio.h>
union data
{
int i;
float f;
};
int main()
{
union data v;
int bit;
printf("Enter The data : ");
scanf("%f",&v.f);
printf("The Binary is : ");
for(bit=31;bit>=0;bit--)
{
printf("%d",(v.i>>bit)&1);
}
printf("\n");
}
Program: Write a program to print binary data of double, and give space
for every 8 bits and also it has to work for exponential input.
#include<stdio.h>
union data
{
long long int i;
double d;
};
int main()
{
union data v;
printf("Enter The Data : ");
scanf("%lf",&v.d);
int bit;
printf("Double Equavalnt Of Binary is : ");
for(bit=63;bit>=0;bit--)
{
printf("%lld",(v.i>>bit)&1);
if(bit%8==0)
printf(" ");
}
printf("\n");
}
Program: Write a program to convert little endian to big endian.
#include<stdio.h>
int main()
{
int bit;
int data;
int big=0;
printf("Enter The Data : ");
scanf("%d",&data);
printf("Little Endian : ");
for(bit=31;bit>=0;bit--)
{
printf("%d",(data>>bit)&1);
if(bit%8==0)
printf(" ");
}
printf("\n");
printf("Big Endian : ");
for(bit=7;big<=3;bit--)
{
printf("%d",(data>>bit)&1);
if(bit%8==0)
printf(" ");
if(bit==0)
{
data=data>8;
bit=8;
big++;
}
}
printf("\n");
}
Program: Write a program to print and count palindrome words in given
string.
#include<stdio.h>
#include<string.h>
int testPalindrome(char*);
int main()
{
char str[50];
char str_1[50];
int cnt=0;
printf("Enter The String : ");
scanf("%[^\n]s",str);
int len;
int j;
len=strlen(str);
for(int i=0;i<len-1;i++)
{
for(j=0,i<len;str[i]!=' '&&str[i]!='\0';i++,j++)
{
str_1[j]=str[i];
}
str_1[i]='\0';
if(testPalindrome(str_1))
{
printf("Palindrome is : %s\n",str_1);
cnt++;
}
printf("Palindrome Count is : %d",cnt);
}
printf("\n");
}
/* Error Handling */
if (!fp2)
{
printf("Unable to open temporary file!!\n");
return 0;
}
len=strlen(argv[1]);
copy=malloc(len);
strcpy(copy,argv[1]);
fseek(fp,0,2);
size=ftell(fp);
fseek(fp,0,0); //rewind(fp;
if(newnode==NULL)
{
printf("node not created\n");
}
else
{
/*** initialise the node ****/
printf("enter the roll\n");
scanf("%d",&newnode->roll);
printf("enter the name\n");
scanf("%s",newnode->name);
printf("enter the marks\n");
scanf("%f",&newnode->marks);
//newnode->link=NULL to wriiten if we use
//malloc func for creation of node
if(ptr==NULL)// List is empty
{
ptr=newnode; // making newnode as first node
}
else
{
// if list is already existing , traverse up to last node and link
newnode to last node
for(temp=ptr;temp->link;temp=temp->link);
printf("%d %s %f\n",ptr->roll,ptr->name,ptr->marks);
ptr=ptr->link;
}
}
Program: Write a structure to add data in sorted order in single linked list.
struct Student * Add(struct Student *ptr)
{
struct Student *newnode=NULL,*temp=NULL,*prev=NULL;
/*** creation of the node ****/
newnode=calloc(1,sizeof(struct Student ));
if(newnode==NULL)
{
printf("Node Not Created\n");
}
else
{
/*** initialise the node ****/
printf("Enter The roll : ");
scanf("%d",&newnode->roll);
printf("Enter The Name : ");
scanf("%s",newnode->name);
printf("Enter The Marks : ");
scanf("%f",&newnode->marks);
/* if list is empty or newnode roll is smaller than first node roll ,
adding newnode as first node******/
if(ptr==NULL || newnode->roll < ptr->roll)
{
newnode->link=ptr;
ptr=newnode;
}
else
{ /** if list is empty and newnode roll is greater than first
node roll,then traverse inthe list untill we reach end of the list or until we
come across a value which is greater than newnode roll *********/
prev=ptr;
temp=ptr->link;
while(temp && newnode->roll > temp->roll)
{
prev=temp;
temp=temp->link;
}
prev->link=newnode; //newnode address is assigned to prev
link
newnode->link=temp;
// temp is assigned to newnode link;
/** we are inserting newnode between the prev and temp
nodes****/
}
}
return ptr; // returning first node address
}
Program: Write a program to save the data in single linked list.
void Save(struct Student *ptr)
{
FILE *fp;
if(ptr==NULL)
{
printf("List is empty\n");
return;
}
fp=fopen("Stu","w");
if(fp==NULL)
{
printf("filenot opened\n");
return;
}
while(ptr)
{
fwrite(ptr,1,sizeof(struct Student)-sizeof(struct Student *),fp);
ptr=ptr->link;
}
fclose(fp);
}
Program: Write a program to delete first data in single linked list.
struct Student * Del_first(struct Student *ptr)
{
struct Student *temp=NULL;
if(ptr==NULL)
{
printf("List is empty\n");
return ptr;
}
temp=ptr; // first node address assigned to temp
ptr=ptr->link; // making second node as first
free(temp); // deleting first node
temp=NULL;
return ptr;
}
Program: Write a program to delete all data in single linked list.
struct Student * Del_All(struct Student *ptr)
{ struct Student *temp=NULL;
while(ptr)
{
temp=ptr;
ptr=ptr->link;
free(temp);
temp=NULL;
}
return ptr;
}
Program: Write a program to delete last data in single linked list.
struct Student * Del_last(struct Student *ptr)
{
struct Student *temp=NULL,*prev=NULL;
if(ptr==NULL)
{
printf("list is empty\n");
return ptr;
}
temp=ptr;
while(temp->link)
{
prev=temp;
temp=temp->link;
}
free(temp);
temp=NULL;
if(prev)
prev->link=NULL;
else
ptr=NULL;
return ptr;
}
Program: Write a program to delete roll no data in single linked list.
struct Student *Del_roll(struct Student *ptr)
{
struct Student *temp=NULL,*prev=NULL;
int data;
printf("Enter The Data To Be Deleted : ");
scanf("%d",&data);
if(ptr==NULL)
{
printf("List Is Empty\n");
return ptr;
}
else if(data==ptr->roll) // matching with first node data
{
temp=ptr;
ptr=ptr->link;
free(temp);
temp=NULL;
}
else
{
temp=ptr->link;
prev=ptr;
// traversing until we find the data or we reached the end of the
list
while(temp && temp->roll!=data)
{
prev=temp;
temp=temp->link;
}
if(temp==NULL) // if we reach end of the list
{
printf("%d is not found\n",data);
}
else
{
// if data found, linking prev node with next node of temp
prev->link=temp->link;
free(temp); // deleting temp
temp=NULL;
}
}
return ptr; // returning first node address
}
Program: Write a program to reverse the data in single linked list.
struct Student *Reverse(struct Student *ptr)
{
struct Student *prev=NULL,*cur=NULL,*next=NULL;
if(ptr==NULL) // list is empty
{
printf("SLL is empty\n");
// return ptr;
}
else if(ptr->link==NULL)
{
printf("SLL is having only one node\n");
}
else
{
next=ptr;
while(next)
{
prev=cur;
cur=next;
next=next->link;
cur->link=prev; // linking with prevnode
}
ptr=cur;
}
return ptr;
}
Program: Write a program to add the data in double linked list.
struct Person * Add_beg(struct Person *ptr)
{
struct Person *newnode=NULL;
newnode=calloc(1,sizeof(struct Person));
if(newnode==NULL)
{
printf("Node Not Created\n");
}
else
{
printf("Enter The Age : ");
scanf("%d",&newnode->age);
printf("Enter The Name : ");
scanf("%s",newnode->name);
}
return ptr;
}
Program: Write a program to delete last data in double linked list.
struct Person * Dellast(struct Person *ptr)
{
struct Person *temp=NULL;
if(ptr==NULL)
{
printf("DLL is Empty\n");
}
else
{
for(temp=ptr;temp->link;temp=temp->link);
if(temp->prev)
temp->prev->link=NULL;
else
ptr=NULL;
free(temp);
temp=NULL;
}
return ptr;
}
Program: Write a program to display the data in double linked list.
void Display(struct Person *ptr)
{
if(ptr==NULL)
{
printf("DLL is Empty\n");
}
else
{
while(ptr)
{
Printf(Entered Age is : %d\n”,ptr->age);
printf(“Entered Name Is : %s\n",ptr->name);
ptr=ptr->link;
}
}
}
Program: Write a program to reverse the data in double linked list.
struct Person *Reverse(struct Person *ptr)
{
struct Person *temp=NULL,*temp1=NULL;
if(ptr==NULL)
{
printf("DLL is Empty\n");
}
else if(ptr->link==NULL)
{
printf("DLL is having One Node\n");
}
else
{
temp=ptr;
while(temp)
{
temp1=temp->prev;
temp->prev=temp->link;
temp->link=temp1;
temp=temp->prev; // moving to next node
}
ptr=temp1->prev;
}
return ptr;
}
Program: Write a program to display the reverse data in double linked list.
void RDisplay(struct Person* ptr)
{
if(ptr==NULL)
{
printf("DLL is Empty\n");
}
else
{
for(;ptr->link;ptr=ptr->link);
while(ptr)
{
printf("%d %s\n",ptr->age,ptr->name);
ptr=ptr->prev; // backward traversing
}
}
}
Program: Write a program to add data in circular linked list.
struct Employee * Add(struct Employee *ptr)
{
struct Employee *newnode=NULL,*temp=NULL;
newnode=calloc(1,sizeof(struct Employee));
if(newnode==NULL)
{
printf("Node not Created\n");
}
else
{
printf("Enter The Employ ID : ");
scanf("%d",&newnode->empid);
printf("Enter The Name : ");
scanf("%s",newnode->name);
if(ptr==NULL)
{
// list is empty
ptr=newnode;
newnode->link=ptr;
// newnode->link=newnode
}
else
{
for(temp=ptr;temp->link!=ptr;temp=temp->link);
temp->link=newnode;
newnode->link=ptr;
/***
newnode->link=temp->link
temp->link=newnode
****/
}
}
return ptr;
}
Program: Write a program to delete data in circular linked list.
struct Employee * Del(struct Employee *ptr)
{
int data;
struct Employee *temp=NULL,*temp1=NULL;
printf("Enter The Data To Be Deleted : ");
scanf("%d",&data);
if(ptr==NULL)
{
printf("CLL is empty\n");
}
else if(data==ptr->empid)
{
//matching with first node data
temp=ptr;
ptr=ptr->link;
if(ptr==temp) // only single node
ptr=NULL;
else
{
// making second node or next node address to ptr
for(temp1=temp;temp1->link!=temp;temp1=temp1->link);
temp1->link=ptr;
}
// lastnode link should be updated with lastest first node address
free(temp);
temp=NULL;
}
else
{
temp=ptr;
temp1=ptr->link;
while(temp1!=ptr && data != temp1->empid)
{
temp=temp1;
temp1=temp1->link;
}
if(temp1==ptr)
{
printf("&d is not Found\n",data);
}
else
{
temp->link=temp1->link;
free(temp1);
temp1=NULL;
}
}
return ptr;
}
Program: Write a program to print the data in circular linked list.
void Display(struct Employee *ptr)
{
struct Employee *temp=NULL;
if(ptr==NULL)
{
printf("List is Empty\n");
}
else
{
temp=ptr;
do
{
printf("Employ Id : %d %s\n",temp->empid);
printf(“Employ Name is : %s,”temp->name);
temp=temp->link;
}while(temp!=ptr);
}
}
Assignment-1
<Question_1> What is compiler? What are the compilation stages?
In computing, a compiler is a computer program that translates computer code
written in one programming language (the source language) into another
language (the target language). The name "compiler" is primarily used for
programs that translate source code from a high-level programming language to
a lower-level language (e.g., assembly language, object code, or machine code)
to create an executable program.
Translator:
➢ Check for syntax error
➢ if any error is not found, then converts source code into assembly
code.
Assembler:
➢ convert assembly code into binary code.
Linker:
➢ Links calling function to called function (called function mapping)
- creates _start function, which called main function in our program and _start
also contain proper exit procedure.
NOTE - Giving intermediate filename extension like. i. o. s is necessary, as
they will use by compiler as input. As GCC is not extension independent, it
doesn’t accept input files
with any other extension than it should be having.
NOTE - In GCC function declaration is not strict (undeclared function is not an
error in gcc).
ISO-C has made it a strict rule that any function used in the program must be
declared. No declaration of function results in error.
<Question_4> Object files (test.o) and executable file (test.exe) are binary
files. What is the difference between?
<Question_9> If the following integer variables are declared, then what will
be exact picture of memory occupied.
a) short int s=50;
b) short int s=-5;
c) short int s=32767;
d) short int s=32768;
Solution:
a) short int s=50 = (110010)2
b) short int s=-5 = (011)2
c) short int s=32767 = (0111 1111 1111 1111)2
d) short int s=32768 = (1000 0000 0000)2
<Question_2> What is the range of char, short int, int, long int, float.
Solution:
<Question_3>
main() {
unsigned char a;
a = 0xFF + 1;
printf("%u\n", a);
}
Solution:
test.c: In function ‘main’:
test.c:5:4: warning: unsigned conversion from ‘int’ to ‘unsigned
char’ changes value from ‘256’ to ‘0’ [-Woverflow]
5 | a=0xFF+1;
| ^~~~
<Question_4>
main()
{
int x=10, y=20, z=5, i;
i =x<y<z;
printf("%d\n", i);
}
Solution:
a=1
<Question_5>
#include<stdio.h>
int main()
{
unsigned int x=022;
unsigned int y=0x22;
unsigned int z=022;
printf(“%d\n”,x+y+z);
}
Solution:
Output=70
<Question_6>
#include<stdio.h>
int main()
{
int a=123;
int b=456;
a^=b^=a^=b;
printf("%d %d",a,b);
}
Solution:
a=456
b=123
<Question_8>
#include<stdio.h>
int main()
{
int a=10,b;
b=a>=5?100:200;
printf("b=%d\n",b);
}
Solution:
b=100
<Question_9>
#include<stdio.h>
int main()
{
printf("a=%d\n", -1<<4);
printf("b=%d\n", -1 >>2);
printf("c=%x\n", 1<<10);
printf("d=%x\n", 0xFF<<4);
printf("e=%o\n", 034<<2);
}
Solution:
a=-16
b=-1
c=400
d=ff0
e=160
<Question_10>
#include<stdio.h>
int main()
{
printf("a=%d\n", 1<<4>>4);
printf("b=%d\n", 1<<31>>30);
}
Solution:
a=1
b=-2
<Question_11>
#include<stdio.h>
int main()
{
double f=33.3;
int x;
x=(f>33.3)+(f==33.3);
printf("x=%d\n",x);
}
Solution:
x=1
<Question_12>
#include<stdio.h>
int main()
{
unsigned int res;
res=(64 >>(2+1-2))&(~(1<<2));
printf("res=%d\n",res);
}
Solution:
res=32
<Question_13>
#include<stdio.h>
int main()
{
int z,x=5,y=-10,a=4,b=2;
z=x++ - --y * --b/a;
printf("res=%d\n",z);
}
Solution:
res=7
<Question_14>
#include<stdio.h>
int main()
{
printf("a=%lu\n",sizeof('\n'));
printf("b=%lu\n",sizeof("5"));
printf("c=%lu\n",sizeof(5.0));
printf("d=%lu\n",sizeof(23.4f));
printf("e=%lu\n",sizeof("23.4f"));
}
Solution:
a=4
b=2
c=8
d=4
e=6
<Question_15>
#include<stdio.h>
int main()
{
printf("a=%d\n",(0x11>11)&&(11>011));
printf("b=%d\n",sizeof(-1)>10);
printf("c=%d\n",23.0>23);
}
Solution:
a=1
b=0
c=0
<Question_16>
#include<stdio.h>
int main()
{
int a=-1,b=0, c=5;
char ch='a';
printf("a=%d\n", (ch>=97)&&(ch<=122));
printf("b=%d\n",(a==0) || (b==0) ||(c==0));
printf("c=%d\n", (a<b)&&(b<c));
}
Solution:
a=1
b=1
c=1
<Question_17>
#include<stdio.h>
int main()
{
printf("a=%d\n",-1&3456);
printf("b=%d\n", -1| 456);
printf("c=%d\n", -1 ^ 789);
printf("d=%d\n", 0&789);
printf("e=%d\n",0&234);
}
Solution:
a=3456
b=-1
c=-790
d=0
e=0
<Question_18>
#include<stdio.h>
int main()
{
int bitMask=1;
printf("a=%d\n", 50 & bitMask);
bitMask=32;
printf("b=%d\n",50 & bitMask);
printf("c=%d\n",60 & bitMask);
printf("d=%d\n",70 & bitMask);
bitMask=512;
printf("e=%d\n",525&bitMask);
}
Solution:
a=0
b=32
c=32
d=0
e=512
<Question_19>
#include<stdio.h>
int main()
{
int var=50,bit=3;
printf("a=%d\n",var&(1<<3));
printf("b=%d\n",var&(1<<4));
printf("c=%d\n",var&(1<<5));
}
Solution:
a=0
b=16
c=32
<Question_20>
#include<stdio.h>
int main()
{
int a=2,b=4,c=5;
a+=b*=c-=10;
printf("a=%d b=%d c=%d\n",a,b,c);
}
Solution:
a=-18 b=-20 c=-5
Assignment-4
<Question_1> Write a program to print the type(category) of user supplied
char from stdin. The categories are Numeric/Uppercase/lowercase/special
character.
Solution:
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
puts("Enter a char:");
ch=getchar();
isupper(ch)?printf("ch = %c is Uppercase Character\n",ch):
islower(ch)?printf("ch = %c is Loweercase Character\n",ch):
isdigit(ch)?printf("ch = %c is Numeric character\n",ch):
printf("ch = %c is Special character\n",ch);
}
<Question_2> Write a program to Convert temperature from o C to o F or
vice versa. Your program should show the output as given below. / a.out
1.for centigrade to Fahrenheit
2. for Fahrenheit to centigrade
Enter your choice: 1
Enter temperature in Celsius: 37
37 o C = 98.6 o F
Solution:
#include<stdio.h>
int main()
{
int choice;
float data,cels,fahr;
printf("Choice 1: To convert Data Celcius to Fahrenheit.\n");
printf("Choice 2: To convert Data Fahrenheit to Temp.\n");
INPUT: printf("Enter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Enter Data in celsius : ");
scanf("%f",&data);
fahr=(data*9/5) + 32;
printf("Data in fahrengeit is %f \n:",fahr);
break;
case 2 :
printf("Enter Data in fahrengeit: ");
scanf("%f",&data);
cels=((data-32)*5)/9;
printf("Data in celcius is %f\n:",cels);
break;
default:
printf("Invalid Choice\n");
goto INPUT;
}
}
<Question_3> Write a program to find the highest of three integers.
Note: If two are equal and higher than the third or appropriate result
should print on screen. all the three variables are equal.
Solution:
#include<stdio.h>
int main()
{
int data,rev=0,high=0;
int n;
printf("Enter the data: ");
scanf("%d",&data);
n=data;
while(n!=0)
{
rev=n%10;
if(rev>high)
high=rev;
n=n/10;
}
printf("Highest Number is: %d is %d\n",data,high);
}
<Question_4> Write a program to test the status of a bit in a supplied
integer.
NOTE: user should supply the integer data and bit-position to test.
Solution:
#include<stdio.h>
int main()
{
int data,pos;
printf("Enter the number : ");
scanf("%d",&data);
printf("Enter the position : ");
scanf("%d",&pos);
((data&(1<<pos))==0)?printf("%d bit is clear\n",pos):
printf("%d bit is set\n",pos);
}
<Question_5> Write a program to set/clear/toggle the supplied bit in a
supplied integer.
Solution:
#include<stdio.h>
int printoutput(int);
int setbit(int,int);
int clearbit(int,int);
int togglebit(int,int);
void printMENU();
int main()
{
int data,bit;
char choice;
printf("Enter The data : ");
scanf("%d",&data);
while(1)
{
printMENU();
printf("Enter Your Choice : ");
scanf("%c",&choice);
printf("Enter the Bit : ");
scanf("%d",&bit);
switch(choice)
{
case 's' :
printoutput(setbit(data,bit));
break;
case 'c' :
printoutput(clearbit(data,bit));
break;
case 't' :
printoutput(togglebit(data,bit));
break;
default:
printf("Invalid Choice\n");
break;
}
}
}
int printoutput(int data)
{
printf("\t Data in Decimal : %d\n",data);
printf("\t Data in Binary : ");
for(int i=31;i>0;i++)
{
printf("%d",(data>>i)&1);
if(i%8==0)
printf(" ");
}
}
void printMENU()
{
printf("\nMENU\n\n");
printf("s:\tSet the bit\n");
printf("c:\tClear the bit\n");
printf("t:\tToggle the bit\n");
}
<Question_6> Write a program to swap the two integers.
i)without using third(temporary) variable
ii) you can use temporary variable.
Solution:
i)without using third(temporary) variable
#include<stdio.h>
int main()
{
int a;
int b;
printf("Enter 1st number : ");
scanf("%d",&a);
printf("Enter 2nd number : ");
scanf("%d",&b);
a^=b^=a^=b;
printf("a=%d b=%d\n",a,b);
}
ii) you can use temporary variable.
#include<stdio.h>
int main()
{
int a;
int b;
int temp;
printf("Enter 1st number : ");
scanf("%d",&a);
printf("Enter 2nd number : ");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("a=%d b=%d\n",a,b);
}
<Question_7> Convert the case of a supplied alphabet. If user supplied
lowercase, then convert to uppercase and vice-versa.
Solution:
#include<stdio.h>
int main()
{
char ch;
puts("Enter the Character : ");
ch=getchar();
((ch>=65) && (ch<=90)) ? (ch+=32) : (ch-=32);
printf("ch = %c\n",ch);
}
<Question_8> List the unary operators. Also mention the syntax.
Solution:
1) Unary ‘+’ - c=a+b;
2) Unary ‘-’ - c=a-b;
3) Increment operator ‘++’ - c=a++;
4) Decrement operaror ‘--’ - c=a--;
5) Address of operator ‘&’ - printf(“%d”,&a);
6) sizeof operator ‘sizeof()’ - sizeof(int/float/double);
7) Dereferencing opreator ‘*’ -
8) Logical NOT ‘!” - c!=a;
9) Bitwise not ‘~’ - c=~b;
if(revDigits(data)==data)
printf(“Palindrome\n”);
else
printf(“Not Palindrome\n”);
}
<Question_2>
#include<stdio.h>
int main()
{
int i;
for (i=5; ++i; i -=3)
printf("%d \n",i);
}
Solution:
6
4
2
<Question_3>
#include<stdio.h>
void main()
{
int i=0,j=0;
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(i>1)
continue;
printf(" Hi \n");
}
}
}
Solution:
Hi
Hi
Hi
Hi
Hi
Hi
Hi
Hi
<Question_4>
#include<stdio.h>
int main()
{
int a=0,i=0,b;
for(i=0;i<5;i++)
{
printf("in loop: i=%d\n",i);
a++;
if(i==3)
break;
}
}
Solution:
in loop: i=0
in loop: i=1
in loop: i=2
in loop: i=3
<Question_5>
#include<stdio.h>
int main()
{
int i = 0;
for (i = 0;i < 5; i++)
if (i < 4)
{
printf("Hello\n");
break;
}
}
Solution:
Hello
<Question_6>
#include<stdio.h>
int main()
{
int i=0;
for(i=0; i<20; i++)
{
switch(i)
{
case 0: i+=5;
case 1: i+=2;
case 5: i+=5;
default: i+=4;
break;
}
printf("%d\n", i);
}
return 0;
}
Solution:
16
21
<Question_7>
#include<stdio.h>
int main()
{
int n;
for(n = 7; n!=0; n--)
printf("n = %d\n", n - -);
return 0;
}
Solution:
Infinite Loop of counting numbers
<Question_8>
#include<stdio.h>
int main()
{
unsigned int i=10;
while(i-- >= 0)
printf("i=%u \n",i);
return 0;
}
Solution:
Infinite loop
<Question_9>
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
if (i < 15)
continue;
} while (0);
return 0;
}
Solution:
1
<Question_10>
#include<stdio.h>
int main()
{
int i, j, sum = 0,n;
for(i = 1;i<=n;i++)
for(j=i;j<=i;j++)
sum=sum+j;
printf("%d\n",sum);
}
Solution:
0
<Question_11>
#include<stdio.h>
int main()
{
int i = 0;
do
{
i++;
if (i == 2)
continue;
printf("In while loop ");
} while (i < 2);
printf("%d\n", i);
}
Solution:
In while loop 2
<Question_12>
#include<stdio.h>
int main()
{
int i = 0, j = 0;
for (i; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("inner loop\n");
break;
}
printf("outer loop\n");
}
printf("after loop\n");
}
Solution:
inner loop
outer loop
inner loop
outer loop
after loop
<Question_13>
#include<stdio.h>
int main()
{
int i = 0;
char c = 'a';
while (i < 2)
{
i++;
switch (c)
{
case 'a':
printf("%c ", c);
break;
break;
}
}
printf("after loop\n");
}
Solution:
a a after loop
<Question_14>
#include<stdio.h>
void main()
{
int var=1;
switch(var<<2+var)
{
default:printf("Chennai\n");
case 4: printf("Hyderabad\n");
case 5: printf("Vector\n");
case 8: printf("Bangalore\n");
}
}
Solution:
Bangalore
<Question_15>
#include<stdio.h>
int main()
{
int x = 5, y = 2 ;
char op = '*' ;
switch ( op )
{
default : x += 1 ;
case '+' : x += y ;
case '-' : x -= y ;
}
printf("%d",x);
}
Solution:
6
<Question_16>
#include<stdio.h>
int main()
{
int n=2, sum=1;
switch(n)
{
case 2:sum=sum+2;
printf("sum=%d\n",sum);
case 3:sum*=2;
printf("sum=%d\n",sum);
break;
default : sum=0;
printf("sum=%d\n",sum);
}
}
Solution:
sum=3
sum=6
<Question_17>
#include<stdio.h>
int main()
{
int i=9;
switch(i)
{
printf("hello");
case 9 : printf("abc");
break;
default : printf("def");
}
}
Solution:
abc
<Question_18>
#include<stdio.h>
int main()
{
int i = 65;
switch(i)
{
case 65:
printf("Integer 65");
break;
case 'A':
printf("Char 65");
break;
default:
printf("Bye");
}
}
Solution:
test.c: In function ‘main’:
test.c:10:1: error: duplicate case value
10 | case 'A':
| ^~~~
test.c:7:1: note: previously used here
7 | case 65:
<Question_19>
#include<stdio.h>
int main()
{
int i = 0;
while(i < 3, i = 0, i < 5)
{
printf("%d ",i++);
}
}
Solution:
Infinite loop with 0
<Question_20>
#include<stdio.h>
int main()
{
int x=011,i;
for(i=0;i<x;i+=3)
{
printf("Start ");
continue;
printf("End");
}
return 0;
}
Solution:
Start Start Star
Assignment-7
<Question_1> Write a program to reverse bits of a given integer number.
Solution:
#include<stdio.h>
int main()
{
int data;
int bit;
int i,j;
printf("Enter The data : ");
scanf("%d",&data);
for(bit=31;bit>=0;bit--)
printf("%d",(data>>bit)&1);
printf("\n");
for(i=31,j=0;i>j;i--,j++)
{
if(((data>>i)&1) != ((data>>j)&1))
{
data=data^(1<<i);
data=data^(1<<j);
}
}
for(bit=31;bit>=0;bit--)
printf("%d",(data>>bit)&1);
printf("\n");
}
<Question_2> Write a program to count pair of set bits in a given number.
Solution:
#include<stdio.h>
int setBitPairs(int);
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
printf("Set Bit Pairs are : %d\n",setBitPairs(data));
}
int setBitPairs(int data)
{
int cnt=0,bit=31;
while(bit>0)
{
if((((data>>bit)&1)==1)&&(((data>>--bit)&1)==1))
{
cnt++;
--bit;
}
else
--bit;
}
return cnt;
}
<Question_3> Write a program to print longest series of 1’s in given
integer set bits.
Solution:
#include<stdio.h>
int main()
{
int bin,data,cnt=0,flag=0,bit;
printf("Enter The Data : ");
scanf("%d",&data);
for(bit=31;bit>=0;bit--)
{
bin=(data>>bit)&1;
if(bin==1)
{
cnt++;
}
if(cnt==2)
{
flag++;
cnt=0;
}
if(bin==1)
{
continue;
}
}
for(bit=31;bit>=0;bit--)
{
printf("%d",(data>>bit)&1);
if(bit%8==0)
printf(" ");
}
printf("\n");
printf("Count is : %d\n",flag);
}
<Question_4> Write a program to make arrangement to list the numbers
the sum of digits, reduce to single digit is 9.
Solution:
#include<stdio.h>
int main()
{
int min,max;
int data;
int sum=0;
int temp;
printf("Enter The Min Digit : ");
scanf("%d",&min);
printf("Enter The Mix Digit : ");
scanf("%d",&max);
for(data=min;data<=max;data++)
{
temp=data;
while(temp)
{
sum=sum+temp%10;
temp/=10;
}
if(sum>9)
{
temp=sum;
sum=0;
while(temp)
{
sum=sum+temp%10;
temp/=10;
}
}
if(sum==9)
printf("%d, ",data);
}
printf("\n");
}
<Question_5> In the above program make arrangement, to list only
numbers which are ascending-order-digits.
Solution:
#include<stdio.h>
int testDigits(int);
int testAsc(int);
int main()
{
int min,max;
int data;
printf("Enter The Min Value : ");
scanf("%d",&min);
printf("Enter The Max Value : ");
scanf("%d",&max);
for(data=min;data<=max;data++)
if(testDigits(data)==9)
if(testAsc(data))
printf("%d, ",data);
}
int testDigits(int data)
{
int sum=0;
while(data)
{
sum=sum+data%10;
data/=10;
}
return sum;
}
int testAsc(int data)
{
int curDig;
int preDig=9;
while(data)
{
curDig=data%10;
if(curDig>preDig)
return 0;
preDig=curDig;
data/=10;
}
if(data==0)
return 1;
else
return 0;
}
<Question_6> Write a program to print the factorial of a given integer.
Solution:
#include<stdio.h>
int main()
{
int data,i,fact=1;
printf(“Enter the data: “);
scanf(“%d”,&data);
for(i=data;i;i--)
fact*=i;
printf(‘factorial = %d\n”,fact);
}
else
{
printf("*");
f=1;
}
}
else
{
printf(" ");
}
}
}
}
Output:
Enter The N: 5
A
A*B
A*B*C
A*B*C*D
A*B*C*D*E
5)
#include<stdio.h>
int main()
{
int a,b,c,n,f=1;
char ch=65;
printf("Enter The Number N : ");
scanf("%d",&n);
for(a=1;a<=n;a++,printf("\n"))
{
f=1;
for(b=1;b<=n;b++)
{
if(b<=(n-a))
printf(" ");
else if(f==1)
{
printf("%c",ch);
f=0;
}
else if(b==n)
{
printf("*%c",ch);
}
else
{
printf("**");
}
}
ch++;
}
}
Output:
Enter The Number N : 5
A
B*B
C***C
D*****D
E*******E
6)
#include<stdio.h>
int main()
{
int a,b,n;
printf("Enter The N : ");
scanf("%d",&n);
for(a=n;a>0;a--,printf("\n"))
for(b=1;b<=a;b++)
{
if(a%2==0)
printf("*");
else
printf("%d",a);
}
}
Output:
Enter The N : 5
55555
****
333
**
1
7)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=5;j>=i;j--)
printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=1;j<=i+1;j++)
printf("* ");
}
*****
****
***
**
*
**
***
****
*****
8)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++,printf("\n"))
for(j=1;j<=5;j++)
(j<i)?printf(" "):printf("* ");
for(i=1;i<=4;i++,printf("\n"))
for(j=5;j>=1;j--)
(j>i+1)?printf(" "):printf("* ");
}
Output:
*****
****
***
**
*
**
***
****
*****
Assignment-9
<Question_1> Define function in C. List the types of function. What are the
benefits of using more functions in a C program?
A function is a block of statements that performs a specific task.
Types of functions:
1) Predefined standard library functions
2) User Defined functions
Contains 2 section
1) Test Section: Text section contains binary equivalent
a.out of code contains all user defined function bodies.
2) Data Section: contains global and local variable.
Whenever the function called stack frame is created in stack section. Stack
frame for the function contains local variable defined function, formal
arguments, and function return address.
#include<stdio.h>
int sumofDigits(int);
int main()
{
int data;
printf("Enter The data : ");
scanf("%d",&data);
printf("Sum of Digit of %d is %d\n",data,sumofDigits(data));
}
#include<stdio.h>
int sumDigits(int);
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
printf("Sum of Digit in %d is %d\n",data,sumDigits(data));
}
#include<stdio.h>
int main()
{
int data;
int fact=1;
printf("Enter The Data : ");
scanf("%d",&data);
for(int i=data;i;i--)
fact*=i;
printf("Factorial is : %d\n",fact);
}
ii)using recursion.
#include<stdio.h>
int fact(int);
int main()
{
int data;
printf("Enter The Data : ");
scanf("%d",&data);
printf("Factorial Of %d is %d\n",data,fact(data));
}
int fact(int data)
{
if(data==0)
return(1);
else
return(data*fact(data-1));
}
<Question_10> Print Fibonacci series up to a specified value. Use function
named fib of prototype int fib(int); The function fib should return
Fibonacci value associated with supplied number. For example: fib (1) is 0
fib (2) is 1 fib (5) is 3
#include<stdio.h>
int main()
{
int max;
int a=0,b=1;
int c;
printf("Enter The Max Number : ");
scanf("%d",&max);
printf("0, 1, ");
for(c=a+b;(c=a+b)<=max;a=b,b=c)
printf("%d, ",c);
printf("\n");
}
#include<stdio.h>
int fib(int);
int main()
{
int data,i;
printf("Enter The Data : ");
scanf("%d",&data);
for(i=0;i<data;i++)
printf("%d, ",fib(i));
printf("\n");
}
int counter(int i)
{
static int count=0;
count=count + i;
return(count);
}
int main()
{
int i, j;
for(i=0;i<=5;i++)
j=counter(i);
printf("%d", j);
}
Solution:
test.c:22:5: error: redefinition of ‘main’
22 | int main()
| ^~~~
test.c:7:5: note: previous definition of ‘main’ was here
7 | int main()
| ^~~~
<Question_3>
#include<stdio.h>
int func(int);
int main ()
{
int x,y;
y=5;
x=func(y++);
(x==5)?printf(" True\n "):printf(" False\n " );
}
int func(int z)
{
if(z==6)
return 5;
else
return 6;
}
Solution:
False
<Question_4>
#include<stdio.h>
int f(int);
int main()
{
int x;
x = 3;
f(x);
printf("MAIN\n");
}
int f(int n)
{
printf("F\n");
if (n != 0)
f(n-1);
}
Solution:
F
F
F
F
MAIN
<Question_5>
#include<stdio.h>
int find(int);
int main()
{
int a=5;
a=find(a+=find(a++));
printf("%d\n",a);
}
int find(int a)
{
return(a++);
}
Solution:
11
<Question_6>
#include<stdio.h>
void fn(int,int);
int main()
{
int a=5;
printf("in main: %d %d\n",a++,++a);
fn(a,a++);
}
int main()
{
printf("%d\n",fn(7));
}
Solution:
11
<Question_8>
#include<stdio.h>
int x=5;
void print()
{
printf("%d\n",x--);
}
int main()
{
print();
}
Solution:
5
<Question_9>
#include<stdio.h>
int main()
{
int n=10;
int func(int);
printf("%d\n",func(n));
}
int func(int n)
{
if(n>0)
return(n+func(n-2));
else return 0;
}
Solution:
30
<Question_10>
#include<stdio.h>
int cap(int);
int main()
{
int n;n= cap(6);
printf("%d\n",n);
}
int cap(int n)
{
if(n<=1) return 1;
else return(cap(n-3)+cap(n-1));
}
Solution:
9
<Question_11>
#include<stdio.h>
int main()
{
int num = _a_123(4);
printf("%d", --num);
return 0;
}
int _a_123(int num)
{
return(num++);
}
Solution:
3
<Question_12>
#include<stdio.h>
int function(int, int);
int main()
{
int a = 25, b = 24, c;
printf("%d ", function(a + 2, b + 3));
}
Answer-> 01000001
Explanation -> for j=0 --- 65 & (0x80>>0) - 01000001 (65)
& 10000000 (128)
00000000 (0) i.e. false so 0 will print 1st
For j=1 -- 65 & (0x80>>1) - 01000001 (65)
10000000>>1 ---------- 01000000 (64)
01000000 (64)--- i.e. True so it will print 1
Answer -- 11 20 1
Explanation --- x++ || --y i.e. 10 || 20 (--y Will not checked) but or will only check left to right if
left value is true output is true i.e. 1
So z = 1, x = 11, y =20
Q5. Predict the output?
int main()
{
float a=5.0,b=5.0;
int c;
c = a|b;
printf(“%d\n”,c);
}
Answer - 11
Explanation -- for loop will execute till i<10 so when it break I will be 10 & we use i++ so 11 will be final
output.
Q7. Predict the output?
#include<stdio.h>
union t
{
unsigned char x;
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
};
int main()
{
union t v={0x34};
unsigned temp;
Answer -- 34
Explanation --- All assignment operation has been performed on bit field so no change happens there.
Q8. Output of code?
#include<stdio.h>
int main()
{
printf("%d %d\n",sizeof('a'),sizeof("ab"));
}
Answer - 4 3
Explaination sizeof the character literal is char. In C the type of character literal is integer (int). So
in C the sizeof('a') is 4 for 32bit architecture,
It is also looks like char constant i.e ascii value of ‘a’ is integer i.e 4 byte.
Sizeof(“ab”) -- a b ‘\0’ i.e 1+1+1 === 3
Q9.output of code?
#include<stdio.h>
int main()
{
int arr[] = {10,20,30,40,50};
printf("%d\n",&arr[4]-&arr[1]);
}
Or---
#include<stdio.h>
int main()
{
char str[]="vector";
printf("%d\n",&str[4]-&str[1]);
}
Answer -- 3
Explanation --
0th 1st 2nd 3rd 4th
10 20 30 40 50
1004 1008 1012 1016 1020
AnswerCompiler Error
Explanation we cannot perform bit wise operation on float & double.
Answer Hello
If (i=3) here we used assignment operator so it will always be true.
So it prints “Hello”.
while(8<10) (i=9)
while(9<10) (i=10)
while(10<10) Loop break here, but I will increment so, i=11
Use a for loop when you know the loop should execute n times.
While Loop-
Use a while loop for reading a file into a variable.
External Linkage
In this type the variable/identifier scope can be used within the file as well as inside other / outer
file.
Implemented by “extern” keyword followed by identifier.
Q31 Difference between “/” and “%” and their uses. “%” is used using what data types.
Answer -
For “%”
% is a modulo operator , It give remainder of two numbers on division as result and discard quotient and
gives an integer
Eg. 11%4=3
11/4 => 11–(4*2) => 11–8 = 3, remainder is 3 as it is less than 4.
Application ->
1. It is used as format specifier I.e. (%c/d/x/o/s/f etc)
2. In so many code like prime number, getting reminder of division.
It can be used with char,int but it cannot be used with Float & double
For “/”
“/” is a division operator, it give quotient of two numbers on division as result and gives a floating point
number.
Eg. 11/4=2.75
11/4 => 11–(4*2) => 11–8 = 3 => 3/4= 0.75. so quotient is 2.75
On integer type casting Result will be 2 and on double or float result will be 2.75.
Application--
1. To get last digit of integer.
2. In arithmetic Division.
3. Used in commenting
Prototype –
Void ptr_name;
Eg. Void *ptr;
Example –
int a=20;
char b = ‘x’;
void *ptr = &a;
ptr = &b;
Q33. Why do we use void pointer when we can explicitly type cast a point even when it is
declared using another data type. For example (consider an int pointer we can use this
pointer somewhere else in the program by type casting it to float. So what is the use of a
void pointer when we have this type of facility available in C?)
Answer--
Void pointer --
It is a pointer which can hold the address of any data type.
Speciality of these pointer is it does not take/occupy any kind of memory until we didn’t assigned it.
Above point make this a special & different whereas in other hand other pointer take / occupy memory
when it declared.
I.e. For Normal Pointer---
Int a=10;
Int *ptr; //It takes 4byte memory as it is of int type.
For Void Pointer------
Int a=10;
Void *ptr; //Do not take / occupy memory.
Ptr = &a; //now it take 4byte for int. & implicitly typcasted to int.
Q35. Var pointer, Void pointer, dangling pointer, null pointer: prototypes, difference and
uses?
Answer->
Var Pointer Void Pointer Dangling Pointer NULL Pointer
It is normal pointer. It is a pointer which can It is a case where pointer It is a pointer where we
hold address of any data can use unallocated can avoid the problem of
type. memory location & dangling pointer.
perform unexpected
behaviour.
Used in almost all Can be Used as alternate To avoid dangling or
application in c where we of general pointer. wild behaviour of
use call by reference pointer.
concept.
We might require to No need to type cast it in
maintain type casting of gcc as it implicitly type
pointer for appropriate cast it.
situation. Some time it requires to
typecast.
Prototype- Prototype- Prototype- Prototype-
Data_type *ptr_name; Void *ptr_name; Int a=20; Int a=20;
Int *ptr=&a; Int *ptr = &a;
Free(ptr); Free(ptr);
Here now ptr content Ptr = NULL;
some garbage value & Now this is NULL
act unexpected called pointer.
dangling.
Q37. Difference between static allocation and dynamic allocation along with some
practical examples.
Answer ---
Static Allocation Dynamic Allocation
variables get allocated permanently Variables get allocated only if your
program unit gets active.
Static Memory Allocation is done Dynamics Memory Allocation is done
before program execution. during program execution.
It uses stack for managing the static It uses heap for managing the dynamic
allocation of memory allocation of memory
It is less efficient It is more efficient
there is no memory re-usability there is memory re-usability and
memory can be freed when not required
Once the memory is allocated, the When memory is allocated the memory
memory size cannot change. size can be changed.
we cannot reuse the unused memory This allows reusing the memory
Execution is faster than dynamic Execution is slower than static memory
memory allocation. allocation.
In this allocated memory remains In this allocated memory can be
from start to end of the program. released at any time during the program.
Q38. Type of storage classes available and their uses. Which storage class is used for a
faster programming and y?
Answer --
Storage Classes are used to describe the features of a variable/function. These features basically include
the scope, visibility and life-time.
There are 4 storage class.
Iteration array elements can be navigated using Pointer can give access to array
indexes using [], elements by using pointer arithmetic.
i.e, arr[0],arr[1],etc i.e, array[2] == *(p+2)
Fwrite ---
It refers to binary format. Data stores into Binary or machine language format.
It is also a predefined function.
Fprintf will copy content of str pointer into file pointed by file pointer in binary
format.
Prototype
Size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
Parameters
o Ptr – This is the pointer to the array of elements to be written.
o Size – This is the size in bytes of each element to be written.
o Nmemb – This is the number of elements, each one with a size of size bytes.
o Stream – This is the pointer to a FILE object that specifies an output stream.
This function returns the total number of elements successfully returned as a
size_t object, which is an integral data type. If this number differs from the
nmemb parameter, it will show an error.
Executable Code –
Code is the code in which extra library file are linked.
It is large in size compared to Object cade.
It can be taken after Linker stage.
i.e. gcc filename.c //we will get (.out or .exe) file.
It is in binary format.
It can be used for debugging using objdump & gdb.
Q46. What is DMA?
Answer-
Dynamic Memory Allocation is the term refer to give memory to an variable or pointer.
It totally work on heap area.
In this method variable or pointer get memory block to store the assigned value to that
variable.
There are 4 type of DMA.
1. Malloc --- allocate memory to variable & assign garbage by default.
2. Calloc ---- allocate memory to variable & assign zero by default.
3. Realloc --- allocate or update the memory size of variable. It can be increased or
decreased.
4. Free --- de allocate the allocated memory.
Q50. Prototyoes
Answer
Printf()
Int printf(char *format, arg1, arg2, …);
Printf converts, formats, and prints its arguments on the standard output. It returns the
number of characters printed.
Scanf()
Int scanf(const char *format, …);
Read from stdin.
The function returns the total number of items successfully matched, which can be
less than the number requested. If the input stream is exhausted or reading from it
otherwise fails before any items are matched, EOF is returned.
Strlen()
Int strlen(const char *str);
The strlen() function calculates the length of a given string.The strlen() function is
defined in string.h header file. It doesn’t count null character ‘\0’.
Return: This function returns the length of string passed.
It is defined in the string.h header file.
Strcpy()
Char* strcpy(char* destination, const char* source);
Strcpy() is a standard library function in C/C++ and is used to copy one string to
another. In C it is present in string.h header file and in C++ it is present in cstring
header file.
Return Value: After copying the source string to the destination string, the
strcpy() function returns a pointer to the destination string.
It is defined in the string.h header file.
Strncpy()
Char *strncpy( char *dest, const char *src, size_t n );
At most n bytes of src are copied. If there is no NULL character among the first n
character of src, the string placed in dest will not be NULL-terminated. If the
length of src is less than n, strncpy() writes additional NULL character to dest to
ensure that a total of n character are written.
Return Value: It returns a pointer to the destination string.
It is defined in the string.h header file.
Strcat()
Char *strcat(char *destination, const char *source);
The strcat() function concatenates the destination string and the source string, and
the result is stored in the destination string.
It is defined in the string.h header file.
Return Value : This function returns a pointer to the resulting string dest.
Strncat()
Char *strncat(char *dest, const char *src, size_t n);
This function appends not more than n characters from the string pointed to by src
to the end of the string pointed to by dest plus a terminating Null-character. The
initial character of string(src) overwrites the Null-character present at the end of
string(dest).
Return Value: The strncat() function shall return the pointer to the string(dest).
Strcmp()
Int strcmp(const char *leftStr, const char *rightStr );
Strcmp() is a built-in library function and is declared in <string.h> header file. This
function takes two strings as arguments and compare these two strings
lexicographically.
This function can return three different integer values based on the comparison:
o Zero ( 0 ): A value equal to zero when both strings are found to be identical.
o Greater than zero ( >0 ): A value greater than zero is returned when the first not
matching character in leftStr have the greater ASCII value than the corresponding
character in rightStr.
o Less than Zero ( <0 ): A value less than zero is returned when the first not
matching character in leftStr have lesser ASCII value than the corresponding
character in rightStr.
Strncmp()
Int strncmp( const char* lhs, const char* rhs, size_t count );
The strncmp() function in C++ compares a specified number of characters of two
null terminating strings. The comparison is done lexicographically.
It is defined in <cstring> header file.
The strncmp() function returns a:
o Positive value if the first differing character in lhs is greater than the corresponding
character in rhs.
o Negative value if the first differing character in lhs is less than the corresponding
character in rhs.
o 0 if the first count characters of lhs and rhs are equal.
Memcpy()
Void *memcpy(void *dest, const void * src, size_t n);
Copies n characters from memory area src to memory area dest.
This function returns a pointer to destination, which is str1.
Memmove()
Void *memmove(void *str1, const void *str2, size_t n);
Copies n characters from str2 to str1, but for overlapping memory blocks,
memmove() is a safer approach than memcpy().
This function returns a pointer to the destination, which is str1.
Fprintf ()
Int fprintf(FILE *stream, const char *format, …);
Sends formatted output to a stream.
If successful, the total number of characters written is returned otherwise, a
negative number is returned.
Fscanf()
Int fscanf(FILE *stream, const char *format, …);
Reads formatted input from a stream.
This function returns the number of input items successfully matched and assigned,
which can be fewer than provided for, or even zero in the event of an early
matching failure.
Fputs()
Int fputs(const char *str, FILE *stream);
Writes a string to the specified stream up to but not including the null character.
This function returns a non-negative value, or else on error it returns EOF.
Fgets()
Char *fgets(char *str, int n, FILE *stream);
Reads a line from the specified stream and stores it into the string pointed to by str.
It stops when either (n-1) characters are read, the newline character is read, or the
end-of-file is reached, whichever comes first.
On success, the function returns the same str parameter. If the End-of-File is
encountered and no characters have been read, the contents of str remain
unchanged and a null pointer is returned.
If an error occurs, a null pointer is returned.
Fwrite()
Size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
Writes data from the array pointed to, by ptr to the given stream.
This function returns the total number of elements successfully returned as a size_t
object, which is an integral data type. If this number differs from the nmemb
parameter, it will show an error.
Fread()
Size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
Reads data from the given stream into the array pointed to, by ptr.
The total number of elements successfully read are returned as a size_t object,
which is an integral data type. If this number differs from the nmemb parameter,
then either an error had occurred or the End Of File was reached.
int main()
{
union t v;
puts("Enter Floating Real Value:");
scanf("%f",&v.f); //Getting Real Value Here
printf("Binary Form is: ");
printf("%d ",v.row.sign); //1st Printing Signed bit of Real value