Nothing Special   »   [go: up one dir, main page]

C Language Code

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

*write a function slen(str)that will return the length of a string without using built-in function.

Ans:

#include<stdio.h>

int slen(char str[]);

void main()

char str[100];

printf("\n enter string:");

fgets(str,100,stdin);

printf("\n length is :%d",slen(str));

int slen(char str[])

int length=0;

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

length++;

return length-1;

*write the function countvowel(str)that will count the number of vowels in a given string and written
that count.

Ans:

#include<stdio.h>

#include<string.h>
int countvowel(char str[]);

void main()

char str[80];

printf("\n enter a string:");

fgets(str,80,stdin);

printf("\n number of vowels:%d",countvowel(str));

int countvowel(char str[])

int count=0;

int i;

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

if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||
str[i]=='u'||str[i]=='U')

count++;

return count;

*write the function reversecase(str)that will change the case of letter in a given string str.

Ans:

#include<stdio.h>

#include<string.h>

void reversecase(char str[]);


int main()

char str[80];

printf("\n enter a string:");

gets(str);

reversecase(str);

printf("\n uppercase:%s",str);

void reversecase(char str[])

int c=0;

while(str[c]!='\0')

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

str[c]=str[c]-32;

c++;

*write the function replacechar(str,ch1,ch2)that will replace all occurances of character ch1 in a
string str by ch2 and diplay the string in main function.

Ans:

#include<stdio.h>

#include<string.h>

void replacechar(char *str,char ch1,char ch2)


{

int i=0;

for(i=0;str[i];i++)

if(str[i]==ch1)

str[i]=ch2;

int main()

char str[80],ch1,ch2;

printf("\n enter a string:");

gets(str);

printf("\n enter character:");

ch1=getchar();

getchar();

printf("\n enter character character to replace %c:",ch1);

ch2=getchar();

printf("\n before replace:%s",str);

replacechar(str,ch1,ch2);

printf("\n after replace:%s",str);

}
*write the function replacewhitespcae(str,ch)that will replace all occurance of whitespace character
in a string str by given character ch.

Ans:

#include<stdio.h>

#include<string.h>

void replacewhitespace(char str[],char ch)

int i=0;

for(i=0;i<str[i];i++)

if(str[i]==' ')

str[i]=ch;

void main()

char str[80],ch;

printf("\n enter a string:\n");

gets(str);

printf("\n enter charcter replace %c:",str);

ch=getchar();

replacewhitespace(str,ch);

printf("\n string after replace the whitespace character:\n %s",str);

*write the function is palindrom(str)that will return true or false depending upon the given string is
palindrom or not.

Ans:
#include <stdio.h>

#include <string.h>

int isPalindrome(char* str)

int i, j;

int n = strlen(str);

for (i = 0, j = n - 1; i < n / 2; i++, j--)

if (str[i] != str[j])

return 0;

return 1;

int main()

char str[100];

printf("Enter a string: ");

scanf("%s", str);

if (isPalindrome(str))

printf("The string is a palindrome\n");

else

printf("The string is not a palindrome\n");

return 0;
}

*write a program to count the number of digit in the given number.

Ans:

#include<stdio.h>

int main()

int n,count=0;

printf("\n enter number:");

scanf("%d",&n);

while(n!=0)

count++;

n=n/10;

printf("\n count the number of digits in the given number:%d",count);

*write a program to find the sum of n natural numbers.

Ans:

#include<stdio.h>

#include<math.h>

int sum(int n);

int main()

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


return 0;

int sum(int n)

if(n==1)

return 1;

int nm=sum(n-1);

int N=nm+n;

return N;

*write a program to find the factorial of n numbers.

Ans:

*write a c program to display the quotient and remainder of the division of two variable.

Ans:

#include<stdio.h>

void main()

int n,m;

printf("\n enter 2 number:");

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

int quotient,remiander;

printf("\n quotient=%d,remiainder=%d",n/m,n%m);

}
*write a program to display the size of the different datatypes.

Ans:

#include<stdio.h>

void main()

int inttype;

float floattype;

char chartype;

double doubletype;

printf("\n size of int:%d bytes",sizeof(inttype));

printf("\n size of float:%d bytes",sizeof(floattype));

printf("\n size of char:%d bytes",sizeof(chartype));

printf("\n size of double:%d bytes",sizeof(doubletype));

*write a c program to swap the value of two variable using bitwise operator(^).

Ans:

#include<stdio.h>

void main()

int a,b;

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

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

a=a^b;

b=b^a;

a=b^a;

printf("\n a=%d,b=%d after swap.",a,b);

}
*write the c program which illustrate the bitwise left shift and right shift operator.

Ans:

#include<stdio.h>

void main()

int n=100,i;

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

printf("\n left shift %d",n<<i);

printf("\n ");

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

printf("\n right shift %d",n>>i);

*accept dimension of the cylinder and print the surface area and volume.

Ans:

#include<stdio.h>

void main()

float radius,height;

float s_a,v;

printf("\n enter radius and height:");

scanf("%f%f",&radius,&height);
s_a=2*3.14*radius*(radius+height);

v=3.14*radius*radius*height;

printf("\n surface area=%f,\n volume=%f ",s_a,v);

*write a c program to display the fibonacci series.

Ans:

#include<stdio.h>

#include<conio.h>

void main()

int a,b,c,i,n;

a=0;

b=1;

printf("\n enter number:");

scanf("%d",&n);

printf("\n fibonacci series:");

printf("\n %d \n %d",a,b);

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

c=a+b;

a=b;

b=c;

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

getchar();

*write c program to accept number from user that is n then print all the odd number from n to 1.
Ans:

#include<stdio.h>

int main()

int n;

printf("\n enter number:");

scanf("%d",&n);

for(int i=n;i>=1;i--)

if(n%2!=0)

printf("\n odd numbers upto n:%d",i);

i=i-1;

*write a c program to swap two numbers using swap function.

Ans:

#include<stdio.h>

#include<conio.h>

void swap(int *,int *);

void main()

int x,y;

printf("\n enter two numbes:");

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

printf("\n after swap x=%d,y=%d",x,y);

void swap(int *a,int *b)

int temp;

temp=*a;

*a=*b;

*b=temp;

*write a c program to accept n numbers and display the fibonacci series.

Ans:

#include<stdio.h>

void main()

int i,n,fibonacci[30];

printf("\n enter a number:");

scanf("%d",&n);

fibonacci[0]=0;

fibonacci[1]=1;

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

fibonacci[i]=fibonacci[i-1]+fibonacci[i-2];

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

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

}
or

#include<stdio.h>

void main()

int c[100],n;

printf("\n enter number:");

scanf("%d",&n);

c[0]=0;

c[1]=1;

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

c[i]=c[i-1]+c[i-2];

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

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

*write a c program to accept n number from user and display all even number from n to 1.

Ans:

#include<stdio.h>

#include<conio.h>

void main()

int n;

printf("\n enter a number:");


scanf("%d",&n);

for(int i=n;i>=1;i--)

if(n%2==0)

printf("\n even number:%d",i);

i=i-1;

getch();

*write a c program to print a table of a given number.

Ans:

#include<stdio.h>

#include<conio.h>

void main()

int n,i=1;

printf("\n enter number:");

scanf("%d",&n);

jmp:if(i<=10)

printf("\n %d",i*n);

i++;

goto jmp;

*write a c program to display 1 to 10 numbers on the separate line.


Ans:

#include<stdio.h>

void main()

int i=1;

jmp:if(i<=10)

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

i++;

goto jmp;

*write a c program to print table of the number from 1 to 20 horizontally.

Ans:

#include <stdio.h>

int main() {

int i, j;

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

for (j = 1; j <= 20; j++) {

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

printf("\n");

return 0;
}

or

#include<stdio.h>

void main()

int i,n=1;

while(n<=20)

i=1;

while(i<=10)

printf("\n %4d",i*n);

i++;

n++;

printf("\n");

*write a c program to print multiplication table of the number from 1 to 20 vertically.

Ans:
#include<stdio.h>

void main()

int i,n=1;

while(n<=10)

i=1;

while(i<=20)

printf("\n %4d",i*n);

i++;

n++;

printf("\n");

or

#include <stdio.h>

int main() {

int i, j;

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

for (j = 1; j <= 20; j++) {

printf("%d x %d = %d\n", i, j, i * j);

printf("\n");

}
return 0;

*write a c program to print the sum of the digits of the given number.

Ans:

#include<stdio.h>

void main()

int n,d,sum=0;

printf("\n enter number:");

scanf("%d",&n);

while(n!=0)

d=n%10;

sum=sum+d;

n=n/10;

printf("\n sum=%d",sum);

*write the c program to count the number of digits in the given number.

Ans:

#include<stdio.h>

void main()

{
int n,count=0;

printf("\n enter number:");

scanf("%d",&n);

while(n!=0)

count++;

n=n/10;

printf("\n count=%d",count);

*write a program to display the all divisors of the given number.

Ans:

#include<stdio.h>

void main()

int n,i=1;

printf("\n enter a number:");

scanf("%d",&n);

while(i<=n)

if(n%i==0)

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

i++;

*write a program to check the given number is prime number or not.


Ans:

#include<stdio.h>

void main()

int n,i=1,count=0;

printf("\n enter a number:");

scanf("%d",&n);

while(i<=n)

if(n%i==0)

count++;

i++;

if(count==2)

printf("\n prime number.");

else

printf("\n not prime.");

*write a program to check the given number is perfect number or not.

Ans:

#include<stdio.h>

int main()

int n,i,sum=0;

printf("\n enter a number:");

scanf("%d",&n);

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

{
if(n%i==0)

sum=sum+i;

if(sum==n)

printf("\n %d perfect number.",n);

else

printf("\n %d is not perfect number.",n);

*write a program to accept a number and display it's reverse.

Ans:

#include<stdio.h>

void main()

int n,rev=0,d;

printf("\n enter a number:");

scanf("%d",&n);

while(n!=0)

d=n%10;

rev=rev*10+d;

n=n/10;

printf("\n reverse number=%d",rev);

*write a program to check the given number is palindrom or not.

Ans:
#include<stdio.h>

void main()

int n,u,rev=0,d;

printf("\n enter a number:");

scanf("%d",&n);

u=n;

while(n!=0)

d=n%10;

rev=rev*10+d;

n=n/10;

if(u==rev)

printf(" %d is a palindrom",u);

else

printf(" %d is not palindrom",u);

*write a program to count the number of zero's in the given number.

Ans:

#include<stdio.h>

void main()

int n,count=0;

printf("\n enter a number:");

scanf("%d",&n);

while(n==0)
{

count++;

n=n/10;

printf("\n zero's count=%d",count);

*write a c program to accept n number of integer and display their sum and also display their
average.

Ans:

#include <stdio.h>

void main()

int n,m,sum=0;

float avg;

printf("\n how elements to read:");

scanf("%d",&n);

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

printf("\n enter number:");

scanf("%d",&m);

sum=sum+m;

printf("\n sum=%d",sum);

printf("\n average =%f",(float)sum/n);

*write a program to accept n integer and display their greatest and smallest.

Ans:
#include <stdio.h>

int main() {

int n, i, num, min, max;

printf("Enter the number of integers: ");

scanf("%d", &n);

printf("Enter the first integer: ");

scanf("%d", &num);

min = max = num;

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

printf("Enter the next integer: ");

scanf("%d", &num);

if (num < min) {

min = num;

if (num > max) {

max = num;

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

printf("The smallest value is: %d\n", min);

return 0;

}
*write a program to accept the integer and display it's digits on separate line.

Ans:

#include<stdio.h>

void main()

int n,d;

printf("\n enter number:");

scanf("%d",&n);

while(n>0)

d=n%10;

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

n=n/10;

*Write a program to check whether the a input number is armstrong or not.

Ans:

#include<stdio.h>

int main()

int n,r,sum=0,temp;

printf("enter the number=");

scanf("%d",&n);

temp=n;

while(n>0)

r=n%10;

sum=sum+(r*r*r);
n=n/10;

if(temp==sum)

printf("armstrong number ");

else

printf("not armstrong number");

return 0;

or

#include<stdio.h>

int main()

int m,d,s=0,t;

printf("\n enter number:");

scanf("%d",&m);

t=m;

while(m>0)

d=m%10;

s=s+(d*d*d);

m=m/10;

if(t==s)

printf("\n %d is armstrong number.",t);

}else{

printf("\n %d is not armstrong number.",t);

}
return 0;

*write a program to calculate x power y.

Ans:

#include<stdio.h>

int main()

int a,b,n=1;

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

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

for(int i=0;i<b;i++)

n=n*a;

printf("\n %d to the power %d is :%d",a,b,n);

return 0;

*write a program to calculate multiplication of two numbers without using(*)sign.

Ans:

#include<stdio.h>

int main()

int m,n,output=0;

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

scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)

output=output+m;

printf("\n multiplication of two number is:%d",output);

return 0;

*write a program to generate following triangle up to n lines.

1 2

1 2 3

Ans:

#include <stdio.h>

int main() {

int n, i, j;

printf("Enter the number of lines: ");

scanf("%d", &n);

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

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

printf("%d ", j);

printf("\n");

return 0;

}
*write a program to generate following pattern.

Aa

Aa Bb

Aa Bb Cc

Aa Bb Cc Dd

Ans:

#include<stdio.h>

int main()

int n;

char ch='A';

printf("\n enter numbers:");

scanf("%d",&n);

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

ch='A';

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

printf("%c%c ",ch,ch+32);

ch++;

printf("\n ");

return 0;

*write a program to sort 1d array element in ascending order.


Ans:

#include<stdio.h>

int main()

int n,arr[100];

printf("\n enter number of element:");

scanf("%d",&n);

printf("\n enter element of an array:");

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

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

int t;

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

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

if(arr[j]>arr[j+1])

t=arr[j];

arr[j]=arr[j+1];

arr[j+1]=t;

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

printf("\n sorted array:%d",arr[i]);

You might also like