Tuesday, September 2, 2014

print the following pattern on the screen (console)

0 comments

Problem

Write a program to generate the following pattern on screen.
A
A B
A B C
A B C D
A B C D E
A B C D E F

Solution - Code

#include<stdio.h>
int main()
{
 int i,j;
 for(i=1;i<=6;i++)
 {
  char ch='A';
  for(j=1;j<=i;j++)
  {
   printf("%c",ch);
   ch++;
  }
  printf("\n");
 }
 system("pause");
 return 0;
}

Sample Output


print pattern c language

Read More..

print the following combination on the screen using for loop

0 comments

Problem

Create a program that prints the following combination on the screen (console) using for loop.
1 1
1 2
2 1
3 2

Solution - Code

#include<stdio.h>
int main()
{
 int i,j;
 for(i=1;i<=3;i++)
 {
  for(j=1;j<=2;j++)
  {
   if(i==2 && j==2 || i==3 && j==1)
   continue;
   else
   {
    printf("%d%d\n",i,j);
   }
  }
 }
 system("pause");
 return 0;
}

Sample Output

combination using loop c language

Read More..

print the following pattern on the screen using loops

0 comments

Problem

create a c language program to generate following pattern on the console (screen) using for loop.
*
* *
* * *
* * * *
* * * * *

Solution - Code

#include<stdio.h>
int main()
{
 int i,j;
 i=1;
 j=1;
 for(i=1;i<=5;i++)
 {
  for(j=1;j<=i;j++)
  {
   printf("*");
  }
  printf("\n");
 }
 system("pause");
 return 0;
}

Sample Output


Read More..

Program that prints multiplication table of a number (multiplicand and multiplier input by the user)

1 comments

Problem

Create a program that prints the multiplication table of a number entered by the user (user enter x and y and your program will print the table of x upto y). The table should get displayed in the following form.
2* 1 = 2
2* 2 = 4

Solution - Code

#include<stdio.h>
int main()
{
 int x,y,i;
 printf("Enter x and y: ");
 scanf("%d%d",&x,&y);
 i=1;
 while(i<=y)
 {
  printf("%d*%d=%d\n",x,i,x*i);
  i = i + 1;
 }
 system("pause");
 return 0;
}

Sample Output


Read More..

Generate all possible combinations of 1, 2 and 3 (using for loop)

2 comments

Problem

create a program that generates all possible combinations of 1, 2 and 3 using for loop.

Solution - Code

#include<stdio.h>
int main()
{
 int i,j,k;
 for(i=1;i<=3;i++)
 {
  for(j=1;j<=3;j++)
  {
   {
   for(k=1;k>=3;k++)
    if(j==k)
    continue;
    printf("%d%d%d\n",i,j,k);
   }
   if(i==j)
   continue;
   printf("%d%d%d\n",i,j,k);
  }
 }
 system("pause");
 return 0;
}

Sample Output


generate combinations c language

Read More..

ask user to input starting number and ending number with incremental number and display the numbers between them

0 comments

Problem

write a program which prompts the user for 3 inputs (shown below - determines how and what to count). The user’s answers should be stored in variables. Your counting program should make use of the for loop.
  • Ask the user to enter beginning number to start counting from
  • Ask the user to enter ending number to stop counting at
  • and also ask the user to enter the incremental number

Solution - Code

#include<stdio.h>
int main()
{
 int i,x,y,z;
 printf("Enter beginning and ending number: ");
 scanf("%d%d",&x,&z);
 printf("Enter increment = ");
 scanf("%d",&y);
 for(i=x;i<=z;i=i+y)
 {
  printf("%d\n",i);
 }
 system("pause");
 return 0;
}

Sample Output


starting and ending number with incremental number in c language

Read More..

Monday, September 1, 2014

print ASCII characters in c language using while loop

0 comments

Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.

Code

#include<stdio.h>
int main()
{
int i,x;
i=0;
while(i<=255)
{
printf("%c\t",i);
i=i+1;
}
system("pause");
return 0;
}

Sample Output

print ASCII characters in c language

Read More..

print hearts and faces on the entire screen using loops

0 comments

Write the program to fill the entire screen by printing smiling face and heart alternatively. The smiling face has an ASCI value 1and heart has 3.

Code

#include<stdio.h>
int main()
{
int i,x,y;
x=1;
y=3;
i=1;
while(i<=1000)
{
printf("%c%c",x,y);
i=i+1;
}
system("pause");
return 0;
}

Sample Output

print face/heart c language

Read More..

count backward from 100 to 1 in increments of 10

0 comments

Create a counting program that counts backward from 100 to 1in increments of 10.

Code

#include<stdio.h>
int main()
{
int i;
i=100;
while(i>=1)
{
printf("%d\n",i);
i=i-10;
}
system("pause");
return 0;
}

Sample Output

backward counting c language

Read More..

finding value of x raise to power y

0 comments

Write a program that asks the user to input two numbers x and y. the program calculates x
raise to power y using loop and display the result.

Code

#include<stdio.h>
int main()
{
int x,y,p,i;
printf("Enter two numbers = ");
scanf("%d%d",&x,&y);
p=1;
i=1;
while(i<=y)
{
p=p*x;
i=i+1;
}
printf("Answer= %d\n",p);
system("pause");
return 0;
}

Sample Output


Read More..

find factorial of a number in c language

0 comments

Write a program to find the factorial value of any numberentered through the keyboard.

Solution Code

#include<stdio.h>
int main()
{
int x,j,i;
printf("Enter any number = ");
scanf("%d",&x);
i = 1;
j = 1;
while(i<=x)
{
j=j*i;
i=i+1;
}
printf("Answer = %d \n",j);
system("pause");
return 0;
}

Sample Output

find factorial number c language

Read More..

Create a counting program that counts from 1 to 100 with increments of 5.

0 comments

Code

#include<stdio.h>
int main()
{
int i;
i=1;
while(i<=100)
{
printf("%d\n",i);
i=i+5;
}
system("pause");
return 0;
}

Output

count from 1 to 100 c program

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact