Skip to document

C-Program-codes

C program
Course

Computer Engineering (CSE123)

134 Documents
Students shared 134 documents in this course
Academic year: 2024/2025
Uploaded by:
0followers
1Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

Function programs:

/Write a function that takes an integer number of any digits and return the number reversed the calling function read a number pass it to the function and display the result. / #include<stdio> #include<conio> int reverse(int); main() { int n,r; printf("Enter number:"); scanf("%d",&n); r=reverse(n);/function call by passing argument n i value of n is passed / printf("Reverse number is %d",r); getch(); } int reverse(int n) { int rem,s=0; while(n!=0) { rem=n%10;/ remainder i last digit of n/ s=s10+rem; / multiplying previous sum by 10 i. shifiting its postion and remainder is added / n=n/10; / quotient i to decrease number size by 1 digit */ } return s; }

/*WAp to count and find the sum of all numbers in the array which are exactly divisible by 7 but not by 5 */

#include<stdio>

#include<conio>

int main()

{

int a[20],n,s=0,c=0,i;

printf("How many numbers do you have?:" );

scanf("%d",&n);

printf("Enter elements:");

for(i=0;i<n;i++) /* loop for each element of array */

{

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

if(a[i]%7==0 && a[i]%5!=0) /* checking whether number meets condtion or not */

{

s+=a[i];

c++;

}

}

printf("sum is : %d and count is : %d",s,c);

getch();

return 0;

}

/*WAP to print the Fibonacci series up to n using recursive function. */

#include<stdio>

#include<conio>

void fibo(int,int,int);

int main()

{

int n;

printf("Enter number of terms:");

scanf("%d",&n);

if(a<500) /terminating condition/

{

printf("%d\t",a); /* value of a is displayed till a <500 */

fibo(b,a+b); /*function call inside function fibo so recursive) */

}

}

/*WAP to read two integers from the user it to functions that calculates the HCF and LCM.

Display the result form the main function.

-> to return more than one value use concept of pointer */

#include<stdio>

#include<conio>

void calc(int,int,int*,int*);

int main()

{

int a,b,lcm,hcf;

printf("Enter two numbers:");

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

calc(a,b,&hcf,&lcm);

printf("HCF is %d and LCM is %d.",hcf,lcm);

getch();

}

void calc(int x,int y,int *h,int *l)

{

int i;

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

{

if(x%i==0 && y%i==0)

*h=i;

}

l=(xy)/(*h);

}

/*WAP to display all the prime numbers within the ranges specified by the users using function */

#include<stdio>

#include<conio>

int prime(int);

void main()

{

int n1,n2,i,p;

printf("Enter range:");

scanf("%d%d",&n1,&n2);

for(i=n1;i<=n2;i++) /* loop to check that each number within range are prime or not */

{

p=prime(i); /* sending all numbers from n1 to n2 to function */

if(p==0) /* compares to return value and return 0 if number is prime */

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

}

getch();

}

int prime(int n) /*n=i value */

}

int power(int n,int x)

{

if(x==0)

return 1;

else

return n*power(n,x-1);

}

Practice questions: 1 to read a number and call a function till “yes” is entered by the user. You should count and sum the numbers using static variables in the function and return the average to the main function. 2 to find the sum of digits of an integer recursively.

3 to read integer number and add the individual digits contained in it until the final sum is a single digit. Use recursion to do so.

4 to find ∑ using recursion. ( ∑ )

  1. WAP to evaluate the following series to n terms specified by the user. Here the factorial and must be calculated by recursion.

6 to check whether a positive integer greater than 3 is twin prime or not. A prime number is twin prime if the resulting number after altering (plus or minus)it by 2 also a prime number. Ex: 29 is twin prime because 29 itself is prime and 29+2 =31 is also a prime and 53 is not twin prime because 53 is prime but neither 51 nor 55 is prime.

[hint: make two function checktwinprime() and checkprime()]

  1. WAP to read an integer number,count the even digits and odd digits present in the entered number must write a function for calculating the result and the result must be displayed in main function.

Array and string

/*WAP that takes string from the user and pass it to function. The function finds and returns the number of words in the string the main program display the number of words. ->program logic: ->take string ->pass to function ->caculate number of words in function -> return number of words from functions as result is to display in main function */ #include<stdio> #include<conio> int wcount(char []) void main() { char s[[30]; int l; gets(s); l=wcount(s); printf("NUMber of words %d",l+1); getch(); return 0; } int wcount(char s[]) { int i,l=0; for(s=0;s[i]!='\0';i++) { if(s[i]==' ') l++; } return l; }

/* WAP in C to read a string and display it in reverse order user defined

functions to count the number of characters in it and reverse it */

#include<stdio>

#include<conio>

/* WAp that reads string form user and uses a user defined function to copy the contents of the read string into another character array changing lower case letters to upper case and upper case letters to lower case*/

#include<stdio>

#include<conio>

void process(char [],char []);

int main()

{

char a[50],b[50];

printf("Enter string:");

gets(a);

process(a,b);/*function doesnot return value but changes are seen in main as both a and b are arrays */

printf("The changed string is: %s",b);

getch();

return 0;

}

void process(char a[],char b[])

{

int i;

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

{

if(a[i]>='A' && a[i]<='Z')

b[i]=a[i]+32; /content of a is copied to b by changing to lowercase/

else if(a[i]>='a' && a[i]<='z')

b[i]=a[i]-32; /content of a is copied to b by changing to uppercase/

else

b[i]=a[i]; /characters of a is copied to b if space,numbers,other symbols are encountered/

}

b[i]='\0';

}

/* WAp that reads string form user and uses a user defined function to copy the contents of the read string into

another character array changing lower case letters to upper case and upper case letters to lower case. USe pointer to process string*/

// just change a[i] to *(a+i)

#include<stdio>

#include<conio>

void process(char *,char *);

int main()

{

char a[50],b[50];

printf("Enter string:");

scanf("%[^\n]",a); /*takes input until next line is entered by user */

process(a,b);/*function doesnot return value but changes are seen in main as both a and b are arrays */

printf("The changed string is: %s",b);

getch();

return 0;

ulchwok

pulchowk*/

#include<stdio>

#include<conio>

int main()

{

int i,j,k;

char a[]="pulchowk";

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

{

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

putchar(' '); /* single quote */

for(k=7-i;k<8;k++)

{

putchar(a[k]);

}

putchar('\n'); /*single quote for character */

}

getch();

return 0;

}

/*WAP in c to re-arrange the alphabets of a given string in alphabetical order

using user defined function. USe pointer to process tha string .Your program should

convert the string"cOmpUtEr into cEmOprtU" */

#include<stdio>

#include<conio>

#include<string>

#include<ctype>

//void ascending(char s[40])

void ascending(char *s)

{

int i,j,n;

char t;

n=strlen(s);

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

{

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

{

/* if(toupper(s[i])>toupper(s[j])) //using array notation

{ t=s[i];

s[i]=s[j];

s[j]=t;} */

if(toupper((s+i))>toupper((s+j))) //using pointer notation

{

t=*(s+i);

(s+i)=(s+j);

*(s+j)=t;

}

}

printf("Enter string:");

gets(str);

count(str,&vc,&cc,&nc,&oc);

printf("vowels:%d \t consonants: %d \t numeric character:%d\t others:%d ",vc,cc,nc,oc);

getch();

return 0;

}

void count(char s[],int *v,int *c,int *n,int *o)

{

int i;

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

{

if(toupper(s[i])=='A' || toupper(s[i])=='E'|| toupper(s[i])=='I'|| toupper(s[i])=='O'|| toupper(s[i])=='U')

*v=*v+1;

else if(toupper(s[i])>='A' && toupper(s[i])<='Z')

*c=*c+1;

else if(s[i]>='0' && s[i]<='9')

*n=*n+1;

else

*o=*o+1;

}

}

/*WAP to insert a string into another string in the location specified by the user. Read the string in the main program and pass them to function along with the inserting position and return the resulting string */

#include<stdio>

#include<conio>

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

//void insert(char [],char [],int);

main()

{

char m[50],s[50];

int n;

printf("Enter main string: ");

gets(m);

printf("Enter string to insert :");

gets(s);

printf("Enter position:");

scanf("%d",&n);

insert(m,s,n);

printf("New string:");

puts(m);

getch();

return 0;

}

void insert(char *m,char *s,int p)

//void insert(char m[],char s[],int p)

printf("Enter name of five persons:");

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

gets(name[i]);

sort(name);

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

printf("%s\t",name[i]); //puts(name[i]);

getch();

}

void sort(char name[][10])

{

int i,j;

char temp[10];

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

{

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

{

if(strcmp(name[i],name[j])>0)

{

strcpy(temp,name[j]);

strcpy(name[j],name[i]);

strcpy(name[i],temp);

}

}

}

}

/*WAP that passes array to a function and print the largest and smallest element */

#include<stdio>

#include<conio>

void func(int[],int);

int main()

{

int i,a[20],n;

printf("Enter elements number:");

scanf("%d",&n);

printf("Enter elements:");

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

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

func(a,n);//function call passing array and element number

getch();

}

void func(int a[],int n)

{

int max=a[0],min=a[0],i;

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

{

if(max<a[i])

max=a[i];

Was this document helpful?

C-Program-codes

Course: Computer Engineering (CSE123)

134 Documents
Students shared 134 documents in this course
Was this document helpful?
1 Prepared by: Er. Prakash Kafle
Function programs:
/*Write a function that takes an integer number of any digits and return the number
reversed.IN the calling function read a number pass it to the function and display the
result. */
#include<stdio.h>
#include<conio.h>
int reverse(int);
main()
{
int n,r;
printf("Enter number:");
scanf("%d",&n);
r=reverse(n);/*function call by passing argument n i.e value of n is passed */
printf("Reverse number is %d",r);
getch();
}
int reverse(int n)
{
int rem,s=0;
while(n!=0)
{
rem=n%10;/* remainder i.e last digit of n*/
s=s*10+rem; /* multiplying previous sum by 10 i.e. shifiting its postion and
remainder is added */
n=n/10; /* quotient i.e to decrease number size by 1 digit */
}
return s;
}
/*WAp to count and find the sum of all numbers in the array which are exactly divisible by
7 but not by 5 */
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],n,s=0,c=0,i;