Some simple C programs
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)>>
Nothing Found!
Why not submit your own content? Signup here.
-
What to look for when choosing a web host | By MaxwellPayne | in Programming
When searching for a good website hosting service your website, you'll need to consider a few things....
-
What are User Agents? | By MaxwellPayne | in Programming
Find out what user agents are and what they do....
-
What is website cloaking? | By MaxwellPayne | in Programming
Learn about website cloaking and how to use cloaking....
-
The Pros and Cons of Using Website Splash Pages | By MaxwellPayne | in Programming
Splash pages can serve many useful purposes as well as add a level of professionalism and nice design to a page....
-
Basic Spring Transactions | By davistechyinfo | in Programming
Spring offers a few options when it comes to transaction management. I used the Programmatic Transaction option thi...
-
Simple programs in C (2) | By t2thomas | in Programming
More simple programs in C owing to the success of the previous C article...
-
Choosing a lawyer | By t2thomas | in Legal
Choosing a good lawyer is a tricky thing. Read these tips to make sure you make the right steps in the unfortunate ...
-
Kerala Style Cabbage Thoran | By t2thomas | in Recipes
If you love vegetarian Indian food, this South-Indian dish will steal your heart. Feel free to modulate the spices ...








No comments yet.