Some simple C programs

Posted Jul 07, 2009 by t2thomas / comments 0 comments / Print / Font Size Decrease font size Increase font size

Some Simple C programs: feel free to ask questions in the comments section, I will answer as quickly as I see them

>>Click here for more simple programs in C (2) (pointers and structures and number theory)>>

1. Write a program that prompts the user to enter a telephone number in the form (xxx) xxx- xxxx and then displays the number in the form xxx.xxx.xxxx:

Enter phone number [(xxx)  xxx-xxxx] : (404)  817-6900

You entered  404.817.6900

# include "conio.h"
# include "stdio.h"
int main()
{
char str1[5], str2[8], str[13];
int i;

printf("Enter phone number [(xxx) xxx-xxxx]");
scanf("%s%s",str1,str2);
for(i=1;i<4;i++)
str[i-1]=str1[i] ;
str[3]='.';
for(i=0;i<3;i++)
str[i+4]=str2[i] ;
str[7]='.';
for(i=0;i<4;i++)
str[i+8]=str2[i+4] ;
str[12]='\0';


printf("You entered %s", str);
return 0;
}

2. Write a program that reads an integer entered by the user and displays it in octal (base 8):

Enter a number between 0 and 32767 : 1953

In octal, your number is : 03641

The output should be displayed using five digits, even if fewer digits are sufficient.  Hint: To

convert the number to octal, first divide it by 8; the remainder is the last digit of the octal

number (1, in this case).  Then divide the original number by 8 and repeat the process to arrive

at the next-to-last digit.


# include "stdio.h"
int main()
{
int num,oct=0,i,temp,j;
printf("Enter a number between 0 and 32767 : ");
scanf("%d",&num);
for(i=num,j=0;i>7;i=i/8,j=j+1){
temp=i%8;
oct=oct+temp*pow(10,j);
}
oct=oct+i*pow(10,j);
printf("In octal, your number is : %d",oct);
return 0;
}


3. Write a program that finds the largest and smallest of four integers entered by the user:

Enter four integers : 21  43  10  35

Largest : 43

Smallest : 10

Use as few if statements as possible.

# include "conio.h"
# include "stdio.h"
int main()
{
int n[4], large, small,i;
printf("Enter four integers : ");
scanf("%d %d %d %d",&n[0],&n[1],&n[2],&n[3]);
large=small=n[0];
for(i=1;i<4;i++){
if(n[i]>large)
large=n[i];

if(n[i]
small=n[i];
}
printf("Largest : %d\nSmallest : %d",large,small);
return 0;
}

4. Write a program that finds the largest in a series of numbers entered by the user.  The

program must prompt the user to enter numbers one by one.  When the user enters 0 or a

negative number, the program must display the largest nonnegative number entered:

Enter a number : 60

Enter a number : 38.3

Enter a number : 4.89

Enter a number : 100.62

Enter a number : 75.2295

Enter a number : 0

The largest number entered was 100.62

Notice that the numbers aren't necessarily integers.

# include "conio.h"
# include "stdio.h"
int main()
{
float input, large=0;
do {
printf("\n Enter a number : ");
scanf("%f",&input);
if(input>large) large=input;
}while(input>0);
printf("Largest number was : %f",large);
return 0;
},

5. Write a program that counts the number of vowels in a sentence:

Enter a sentence : And that's the way it is.

Your sentence contains 6 vowels.

# include "conio.h"
# include "stdio.h"

int main()
{   char phrase[100];
int i=0, ctr=0;
printf("Enter your sentence : ");
gets(phrase);
while(phrase[i]!='\0'){
switch(phrase[i]){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':ctr++;

}
i++;
}
printf("The number of vowels is : %d",ctr);
return 0;
}

>>Click here for more simple programs in C (2) (pointers and structures and number theory)>>


Rate this Article:

Rating: 4.0/5 (4 votes cast)

Image by Getty Images via Daylife
  • Nothing Found!

    Why not submit your own content? Signup here.


* You must be logged in order to leave comments, please login or join us.

Comments

No comments yet.



Bookmark and Share
Sign up for our email newsletter
Name:
Email: