You are on page 1of 47

1.

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

void main()

int a, b, c;

int ch;

printf("\nEnter two numbers\n");

scanf("%d%d", &a, &b);

printf("\n\n");

printf("\n 1-ADD \n");

printf("\n 2-SUB \n");

printf("\n 3-MULTI \n");

printf("\n 4-DIVIDE \n");

printf("\n 5-MODULO \n");

printf("\nEnter your choice of operation:\n");

scanf("%d", &ch);

switch(ch)

case 1: c=a+b;

break;
case 2: c=a-b;

break;

case 3: c=a*b;

break;

case 4: if(b==0)

printf("\nDivide by zero error!\n");

exit(0);

else

c=(float)a/b;

break;

case 5: c=a%b;

break;

default: printf("\nInvalid choice!\n");

exit(0);

printf("\nThe result is:%d\n",c);

}
2.

#include <stdio.h>

int main()

int x, y, *a, *b, temp;

printf("Enter the value of x and y\n");

scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;

b = &y;

temp = *b;

*b = *a;

*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;

3.

#include<stdio.h>

struct

char name[100];

int rollno;
struct marks

int test1;

int test2;

int test3;

}m;

}s[100];

void main()

int n,i,roll,sum1,sum2,sum3,max1,max2,max3;

float avg1, avg2, avg3;

printf("Enter the number of students\n");

scanf("%d",&n);

printf("Enter details\n");

printf("Roll no\tName\tTest1\tTest2\tTest3\n");

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

scanf("%d%s%d%d%d", &(s[i].rollno), (s[i].name) , &(s[i].m.test1), &(s[


i].m.test2), &(s[i].m.test3));

printf("Enter roll number to search details\n");


scanf("%d",&roll);

printf("Roll no\tName\tTest1\tTest2\tTest3\n");

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

if((s[i].rollno)==roll)

printf("%d\t%s\t%d\t%d\t%d\n", (s[i].rollno),(s[i].name),(s[i].m.test1),(s[i].
m.test2),(s[i].m.test3));

printf("\n");

sum1=0;

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

sum1+=(s[i].m.test1);

avg1=(sum1)/n;

printf("Average of test 1=%.3f\n",avg1);

sum2=0;

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

sum2+=(s[i].m.test2);
}

avg2=(sum2)/n;

printf("Average of test 2=%.3f\n",avg2);

sum3=0;

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

sum3+=(s[i].m.test3);

avg3=(sum3)/n;

printf("Average of test 3=%.3f\n",avg3);

max1=s[0].m.test1;

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

if(max1<(s[i].m.test1))

max1=(s[i].m.test1);

printf("Highest marks in test 1 =%d\n",max1);

max2=s[0].m.test2;

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

{
if(max2<(s[i].m.test2))

max2=(s[i].m.test2);

printf("Highest marks in test 2=%d\n",max2);

max3=s[0].m.test3;

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

if(max3<(s[i].m.test3))

max3=(s[i].m.test3);

printf("Highest marks in test 3=%d\n",max3);

4.

#include <stdio.h>

int main()

int year;

printf("Enter a year: ");


scanf("%d",&year);

if(year%4 == 0)

if( year%100 == 0)

// year is divisible by 400, hence the year is a leap year

if ( year%400 == 0)

printf("%d is a leap year.", year);

else

printf("%d is not a leap year.", year);

else

printf("%d is a leap year.", year );

else

printf("%d is not a leap year.", year);

return 0;

}
5.

#include <stdio.h>

struct complex

int real, img;

};
int main()

struct complex a, b, c;

printf("Enter a and b where a + ib is the first complex number.\n");

scanf("%d%d", &a.real, &a.img);

printf("Enter c and d where c + id is the second complex number.\n");

scanf("%d%d", &b.real, &b.img);

c.real = a.real + b.real;

c.img = a.img + b.img;

printf("Sum of the complex numbers: (%d) + (%di)\n", c.real, c.img);

return 0;

6.

#include <stdio.h>

int main()
{

int n, reverse = 0, t;

printf("Enter a number to check if it is a palindrome or not\n");

scanf("%d", &n);

t = n;

while (t != 0)

reverse = reverse * 10;

reverse = reverse + t%10;

t = t/10;

if (n == reverse)

printf("%d is a palindrome number.\n", n);

else

printf("%d isn't a palindrome number.\n", n);

return 0;

}
7.

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

main()

float a,b,c,disc,root1,root2;

printf("\nEnter the coefficients:\n");

scanf("%f%f%f",&a,&b,&c);

if(a==0||c==0)

printf ("Not Possible\n");

exit(0);

else

disc=b*b-4*a*c; // ‘disc’ indicates discriminant

//Find the distinct roots

if(disc>0)

root1=(-b + sqrt(disc)) / (2*a);


root2=(-b - sqrt(disc)) / (2*a);

printf("\n Roots are real & distinct! \n");

printf("\n The roots are: \n%f\n%f\n",root1,root2);

else if(disc==0) //Find the equal roots

root1=root2= -b / (2*a);

printf("\n Roots are real & equal! \n");

printf("\n The roots are \n%f\n%f\n",root1,root2);

else

//Find the complex roots

root1= -b / (2*a);

root2= sqrt(abs(disc)) / (2*a);

printf("\n The roots are imaginary!\n");

printf("\n The first root is %f + i%f \n",root1,root2);

printf("\n The second root is %f - i%f \n",root1,root2);

}
8.

#include <stdio.h>

int main()

int low, high, i, flag;

printf("Enter two numbers(intervals): ");

scanf("%d %d", &low, &high);

printf("Prime numbers between %d and %d are: ", low, high);

while (low < high)

flag = 0;

for(i = 2; i <= low/2; ++i)

if(low % i == 0)

flag = 1;

break;

if (flag == 0)

printf("%d ", low);


++low;

return 0;

9.

#include<stdio.h>

void main()

int i, n;

float x, sum, t;

printf(" Enter the value for x :");

scanf("%f",&x);

printf(" Enter the value for n :");

scanf("%d",&n);

x=x*3.14159/180;

t=x;

sum=x;

/* Loop to calculate the value of Sine */

for(i=1;i<=n;i++)
{

t=(t*(-1)*x*x)/(2*i*(2*i+1));

sum=sum+t;

printf("The value of Sin(%f) = %.4f",x,sum);

10.

#include<stdio.h>

int main()

int i, j;

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

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

printf("%d\t",i*j);

printf("\n\n");

}
11.

#include<stdio.h>

#include<math.h>

int main()

int i,max,min,range,count,sum=0,a[10],n;

float avg=0;

printf("Enter the size of array\n");

scanf("%d",&n);

printf("Enter %d elements\n",n);

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

scanf("%d",&a[i]);

max=a[0];

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

if(a[i]>max)

max=a[i];

printf("max value is: %d\n", max);

min=a[0];
for(i=1;i<n;i++)

if(a[i]<min)

min=a[i];

printf("min vaue is: %d\n", min);

range=max-min;

printf("the range is %d\n",range);

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

sum=sum+a[i];

printf("The sum is: %d\n", sum);

avg=(sum/n);

printf("The average is: %f\n", avg);

12.

#include<stdio.h>

#include<string.h>

int main()
{

char a[100];

int length;

printf("Enter a string to calculate it's length\n");

gets(a);

length = strlen(a);

printf("Length of the string = %d\n", length);

return 0;

13.

#include <stdio.h>

int main()

char str[100];

char *ptr;

int cntV,cntC;

printf("Enter any string :: ");

scanf("%s",str);

//assign address of str to ptr

ptr=str;
cntV=cntC=0;

while(*ptr!='\0')

if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a' ||*ptr


=='e' ||*ptr=='i' ||*ptr=='o' ||*ptr=='u')
cntV++;

else

cntC++;

//increase the pointer, to point next character

ptr++;

printf("\nTotal number of VOWELS :: %d and CONSONANTS :: %d\n\n",cntV,


cntC);

return 0;

14.

ii. #include<stdio.h>

void main()

{
int i, j;

for(i=5;i>=1;i--)

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

printf("%d",i);

printf("\n");

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

int i,j;

for(i=5;i>=1;i--)

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

printf("%d",i);

printf("\n");

}
}

15.

#include<stdio.h>

int main()

int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;

printf("Enter rows and column for first matrix:");

scanf("%d %d", &r1, &c1);

printf("Enter rows and column for second matrix:");

scanf("%d %d",&r2, &c2);

// Column of first matrix should be equal to column of second matrix and

while (c1 != r2)

printf("Error! column of first matrix not equal to row of second.\n\n");

printf("Enter rows and column for first matrix:");

scanf("%d %d", &r1, &c1);

printf("Enter rows and column for second matrix:");

scanf("%d %d",&r2, &c2);

// Storing elements of first matrix.

printf("\nEnter elements of matrix 1:\n");


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

for(j=0; j<c1; ++j)

printf("Enter elements a%d%d: ",i+1, j+1);

scanf("%d", &a[i][j]);

// Storing elements of second matrix.

printf("\nEnter elements of matrix 2:\n");

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

for(j=0; j<c2; ++j)

printf("Enter elements b%d%d: ",i+1, j+1);

scanf("%d",&b[i][j]);

// Initializing all elements of result matrix to 0

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

for(j=0; j<c2; ++j)

result[i][j] = 0;

// Multiplying matrices a and b and

// storing result in result matrix


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

for(j=0; j<c2; ++j)

for(k=0; k<c1; ++k)

result[i][j]+=a[i][k]*b[k][j];

// Displaying the result

printf("\nOutput Matrix:\n");

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

for(j=0; j<c2; ++j)

printf("%d", result[i][j]);

if(j == c2-1)

printf("\n\n");

return 0;

16.

#include <stdio.h>

#include <string.h>
int main()

char text[20], reverse_text[20];

int i,n, length = 0;

printf("Enter text: ");

gets(text);

for (i = 0; text[i] != '\0'; i++)

length++; //this will calculate the length of given text

//Reverse the original text and store into reverse_text

for (i = length - 1; i >= 0; i--)

reverse_text[length - i - 1] = text[i];

//Check whether reverse_text is same to original text

for (n = 1, i = 0; i < length; i++)

if (reverse_text[i] != text[i])

n = 0;

if (n == 1)
printf("%s is a palindrome.", text);

else

printf("%s is not a palindrome", text);

return 0;

17. a)

#include<stdio.h>

int find_gcd(int,int);

int find_lcm(int,int);

int main()

int num1,num2,gcd,lcm;

printf("\nEnter two numbers:\n");

scanf("%d %d",&num1,&num2);

gcd=find_gcd(num1,num2);

printf("\nGCD of %d and %d is: %d\n",num1,num2,gcd);

if(num1>num2)

lcm = find_lcm(num1,num2);

else

lcm = find_lcm(num2,num1);
printf("\nLCM of %d and %d is: %d\n",num1,num2,lcm);

return 0;

int find_gcd(int n1,int n2){

while(n1!=n2){

if(n1>n2)

return find_gcd(n1-n2,n2);

else

return find_gcd(n1,n2-n1);

return n1;

int find_lcm(int n1,int n2){

static int temp = 1;

if(temp % n2 == 0 && temp % n1 == 0)

return temp;

temp++;

find_lcm(n1,n2);

return temp;

b)
#include<stdio.h>

unsigned long long int factorial(unsigned int i)

if(i<=1)

return 1;

return i * factorial(i-1);

int main()

int i = 12;

printf("Factorial of %d is %d\n", i, factorial(i));

return 0;

18.

#include<stdio.h>

int fibonacci(int i)

{
if(i==0)

return 0;

if(i==1)

return 1;

return fibonacci(i-1) + fibonacci(i-2);

int main()

{ int i;

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

printf("%d\t\n", fibonacci(i));

return 0;

19.

#include<stdio.h>

int main()

{
int a[10][10], m, n, i, j, flag=0, temp=0;

printf("Enter the array size\n");

scanf("%d %d", &m, &n);

printf("Enter the array elements\n");

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

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

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

for(j=0;j<n;j++)

if(i>j&&a[i][j]==0)

flag=1;

if(i<j&&a[i][j]==0)

temp=1;

if(flag==1)

printf("\nUpper triangular");

else if(temp==1)
printf("\nLower triangular");

else

printf("\nneither upper nor lower triangular");

20.

#include <stdio.h>

int main()

int num;

/* Input number from user */

printf("Enter any number: ");

scanf("%d", &num);

if(num > 0)

printf("Number is POSITIVE");

if(num < 0)

printf("Number is NEGATIVE");

if(num == 0)
{

printf("Number is ZERO");

return 0;

21. Ascending:

#include<stdio.h>

void main()

int i,j,n,a[100],temp,min;

printf("Enter the number of elements:\n") ;

scanf("%d",&n) ;

printf("The given array is:\n") ;

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

scanf("%d",&a[i]) ;

for(i=0;i<n-1;i++)

min=i;

for(j=i+1;j<n;j++)
{

if(a[min]>a[j])

min=j;

if(min!=i)

temp=a[i];

a[i]=a[min];

a[min]=temp;

printf("The sorted array is:\n");

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

printf("%d\n",a[i]) ;

Descending:

#include<stdio.h>

void main()

int i,j,n,a[100],temp,max;
printf("Enter the number of elements:\n") ;

scanf("%d",&n) ;

printf("The given array is:\n") ;

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

scanf("%d",&a[i]) ;

for(i=0;i<n-1;i++)

max=i;

for(j=i+1;j<n;j++)

if(a[max]<a[j])

max=j;

if(max!=i)

temp=a[i];

a[i]=a[max];

a[max]=temp;

}
printf("The sorted array is:\n");

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

printf("%d\n",a[i]) ;

22.

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

void insert_substring(char*, char*, int);

char* substring(char*, int, int);

int main()

char text[100], substring[100];

int position;

printf("Enter some text\n");

gets(text);

printf("Enter a string to insert\n");

gets(substring);

printf("Enter the position to insert\n");


scanf("%d", &position);

insert_substring(text, substring, position);

printf("%s\n", text);

return 0;

void insert_substring(char *a, char *b, int position)

char *f, *e;

int length;

length = strlen(a);

f = substring(a, 1, position - 1);

e = substring(a, position, length-position+1);

strcpy(a, "");

strcat(a, f);

free(f);

strcat(a, b);

strcat(a, e);

free(e);

char *substring(char *string, int position, int length)

{
char *pointer;

int c;

pointer = malloc(length+1);

if(pointer == NULL)

exit(EXIT_FAILURE);

for(c=0;c<length;c++)

*(pointer+c) = *((string+position-1)+c);

*(pointer+c) = '\0';

return pointer;

OR

#include<stdio.h>

int main()

char a[100], ele;

int i, pos, len=0;

printf("Enter the string\n");

gets(a);

for(i=0;a[i]!='\0';i++)

len++;

printf("\n%d",len);

printf("\nEnter the element to be inserted");


scanf("%c",&ele);

printf("\nEnter the position where it should be inserted\n");

scanf("%d", &pos);

if(pos>len)

printf("Out of string limits\n");

else

for(i=len;i>=pos;i--)

a[i]=a[i-1];

a[pos]=ele;

len++;

printf("\n%s",a);

23.

#include <stdio.h>

void main()

{
char sentence[80];

int i, vowels = 0, special = 0;

printf("Enter a sentence \n");

gets(sentence);

for (i = 0; sentence[i] != '\0'; i++)

if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==

'i' || sentence[i] == 'o' || sentence[i] == 'u') ||

(sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==

'I' || sentence[i] == 'O' || sentence[i] == 'U'))

vowels = vowels + 1;

if (sentence[i] =='t' ||sentence[i] =='\0' || sentence[i] ==' ')

special = special + 1;

printf("No. of vowels in %s = %d\n", sentence, vowels);

24.
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

char ch, input[100], output[100];

int no[26] = {0}, n, c, t, x;

printf("Enter the string\n");

scanf("%s", input);

n = strlen(input);

/** Storing how many times characters (a to z)

appears in input string in an array */

for (c = 0; c < n; c++)

ch = input[c] - 'a';

no[ch]++;

t = 0;

/** Insert characters 'a' to 'z' in output string as many times

as they appear in the input string */

for (ch = 'a'; ch <= 'z'; ch++)


{

x = ch - 'a';

for (c = 0; c < no[x]; c++)

output[t] = ch;

t++;

output[t] = '\0';

printf("%s\n", output);

return 0;

25.

#include <stdio.h>

#include <string.h>

int main()

char string[100];

int c = 0, count[26] = {0}, x;

printf("Enter a string\n");

gets(string);
while (string[c] != '\0') {

/** Considering characters from 'a' to 'z' only and ignoring others. */

if (string[c] >= 'a' && string[c] <= 'z') {

x = string[c] - 'a';

count[x]++;

c++;

for (c = 0; c < 26; c++)

printf("%c occurs %d times in the string.\n", c + 'a', count[c]);

return 0;

26.

#include <stdio.h>

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter %d integers\n", n);


for (c = 0; c < n; c++)

scanf("%d",&array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search)

printf("%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)

printf("Not found! %d isn't present in the list.\n", search);

return 0;
}

27.
Ascending:

#include<stdio.h>
main()

int a[10],n,i,j,temp;

printf("\n Enter the size of n:\n" );

scanf("%d",&n);

printf("\n Enter the array elements: \n");

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

scanf("%d",&a[i]);

// Bubble Sorting

for(i=1;i<n;i++) /* i th smallest number bubbles up to its

right spot in ith iteration */

for(j=0;j<n-i;j++) /* bubbling starts from the “deepest numbers”

and proceeds upwards */

/* element at jth position is “lighter” than the one on top,

therefore jth element bubbles up */

if(a[j]>=a[j+1])
{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

printf("\n The ascending sorted array is: \n" );

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

printf("%d\n",a[i]);

Descending:

#include<stdio.h>

int main()

{ int a[50], n, i, j, temp;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

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

scanf("%d", &a[i]);

for (i = 0 ; i < ( n - 1 ); i++)

{
for (j= 0 ; j < n - i - 1; j++)

if(a[j] < a[j+1])

temp=a[j];

a[j] = a[j+1];

a[j+1] = temp;

printf("The descending sorted array is:\n");

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

printf("%d\n", a[i]);

return 0;

28.
29.

#include <stdio.h>

/*function definition*/

int PowerOf2(int number)

while(number!=1)

if(number%2!=0)

return 0;

number=number/2;

return 1;

int main()

int num;

printf("Enter an integer number: ");

scanf("%d",&num);

if(PowerOf2(num))

printf("%d is a number which is the power of 2.",num);

else

printf("%d is not the power of 2.",num);


return 0;

30.

#include<stdio.h>

main()

{ int n, i;

printf("Enter an Integer Number to Find PRIME or Not : ");

scanf("%d", &n);

for(i=2; i<=n/2; ++i)

if(n%i==0)

break;

if(i>n/2)

printf("%d is PRIME",n);

else

printf("%d is NOT PRIME", n);

You might also like