Technocrat

  • Home
Showing posts with label For Loop. Show all posts
Showing posts with label For Loop. Show all posts

Tuesday, October 13, 2015

Find the GCD of two numbers

 Sumit Kar     October 13, 2015     DS, For Loop, Recursion     No comments   

Using For Loop
#include<stdio.h>
int main(){
int x,y,m,i;
do
{
printf("Enter two number: ");
scanf("%d%d",&x,&y);
if(x==0 || y==0)
printf("Please check the input and try again...\n");
}while(x==0 || y==0);
if(x>y)
m=y;
else
m=x;

for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nGCD of %d and %d is %d",x,y,i) ;
break;
}
}
return 0;
}
Using Recursion
#include <stdio.h>

int gcd(int a, int b)
{

if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}

int main()
{
int x,y,m,i;
do
{
printf("Enter two number: ");
scanf("%d%d",&x,&y);
if(x==0 || y==0)
printf("Please check the input and try again...\n");
}while(x==0 || y==0);
printf("GCD of %d and %d is %d ", x, y, gcd(x, y));
return 0;
}

   

Output
Enter two number: 0 99
Please check the input and try again...
Enter two number: 99 121

GCD of 99 and 121 is 11

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find the Factorial of a number

 Sumit Kar     October 13, 2015     For Loop, Recursion, While Loop     No comments   


Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!).
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.



Using While Loop
#include<stdio.h>
void main()
{
int a,f,i;
printf("Enter a number: ");
scanf("%d",&a);
f=1;
i=1;
while(i<=a)
{
f = f * i;
i++;
}
printf("\nFactorial of %d is: %d",a,f);
}


Using For Loop
#include<stdio.h>
void main()
{
int a,f,i;
printf("Enter a number: ");
scanf("%d",&a);
f=1;
for(i=1;i<=a;i++)
f = f * i;
printf("\nFactorial of %d is: %d",a,f);
}


Using Recursion
#include<stdio.h>
int fact(int);
int main(){
int num,f;

printf("\nEnter a number: ");
scanf("%d",&num);

f=fact(num);

printf("\nFactorial of %d is: %d",num,f);
return 0;
}

int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}



   
Output

Enter a number: 5

Factorial of 5 is: 120
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Wednesday, September 23, 2015

Find the maximum occurrence of a character in a string

 Sumit Kar     September 23, 2015     Array, For Loop, If Else, Tricky     No comments   

#include<stdio.h>
#include<conio.h>

int main() {
char str[20], ch;
int count = 0, i,j=0 , max;
int arr[10];
printf("\nEnter a string : ");
scanf("%s", &str);

printf("\nEnter the character to be searched : ");
scanf("%c", &ch);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch)
count++;
else if(count != 0)
{
arr[j] = count;
j++;
count = 0;
}
}
arr[j]=count;
max = 0;
for(i=0;i<=j;i++)
if(max<arr[i])
max=arr[i];
if (max == 0)
printf("\nCharacter '%c'is not present", ch);
else
{
printf("\nMaximum occurrence of character '%c' : %d", ch, max);
}
return (0);
}

   

Output

Enter a string : aabbbaaab

Enter the character to be searched :
Maximum occurrence of character 'a' : 3

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Monday, July 27, 2015

Accept Array elements

 Sumit Kar     July 27, 2015     Array, Basic, For Loop     No comments   

#include<stdio.h>
void main() {
int arr[10], i, j, k, size;
printf("\nEnter array size : ");
scanf("%d", &size);

printf("\nAccept Numbers : \n");
for (i = 0; i < size; i++)
{
printf("Data [%d]: ",i+1);
scanf("%d", &arr[i]);
}

printf("\nThe Array is : ");
for (i = 0; i < size; i++)
printf("%d ", arr[i]);

getch();
}

   

Output

Enter array size : 5

Accept Numbers :
Data [1]: 2
Data [2]: 6
Data [3]: 7
Data [4]: 1
Data [5]: 2

The Array is : 2 6 7 1 2

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Saturday, July 25, 2015

Print Pattern 5 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

#include <stdio.h>
void main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(k=5;k>=i;k--)
{
printf(" ");
}
for(j=1;j<i;j++)
{
printf("*");
}

printf("\n");
}
getch();
}


   

Output

****
***
**
*

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print Pattern 4 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

#include <stdio.h>
void main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
}

   

Output
*****
****
***
**
*

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print Pattern 3 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

#include <stdio.h>
void main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=5;k>=i;k--)
{
printf("*");
}
printf("\n");
}
getch();
}

   

Output

*
**
***
****
*****

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print Pattern 2 

 Sumit Kar     July 25, 2015     Basic, For Loop, Pattern     No comments   

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


   

Output
*
**
***
****
*****

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Friday, July 24, 2015

Print Pattern 1 

 Sumit Kar     July 24, 2015     Basic, For Loop, Pattern     No comments   

#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter number of rows/columns: ");
scanf("%d",&num);
for(r=1; r<=num; r++)
{
for(c=1; c<=num; c++)
printf("* ");
printf("\n");
}
getch();
return 0;
}

   

Output
Enter number of rows/columns: 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Tuesday, June 30, 2015

List all the Prime Numbers

 Sumit Kar     June 30, 2015     Basic, For Loop     No comments   

#include<stdio.h>
void main()
{
int n,i,j,flag;
printf("\n\nEnter the limit\t:");
scanf("%d",&n);
printf("\nThe prime numbers within the given limit:\n");
for(i=1;i<=n;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d\t",i);
}
}

   
Output
Enter the limit :60

The prime numbers within the given limit:
1 2 3 5 7 11 13 17 19 23
29 31 37 41 43 47 53 59

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Sunday, June 28, 2015

Generate the Fibonacci Series

 Sumit Kar     June 28, 2015     Basic, For Loop     No comments   

#include<stdio.h>

void main()
{
int t,i,m=0,n=1,f=1;
printf("\n\nEnter the number of terms:");
scanf("%d",&t);
printf("\n The Fibonacci Series of %d terms:\n",t);
for(i=1;i<=t;i++)
{
printf("%d\t",f);
f=m+n;
m=n;
n=f;
}
}


  

Output
Enter the number of terms:10

The Fibonacci Series of 10 terms:
1 1 2 3 5 8 13 21 34 55

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print the ASCII values of all the English Alphabets

 Sumit Kar     June 28, 2015     Basic, For Loop     No comments   

#include<stdio.h>
void main()
{
char chl='a';
char chu='A';
int i;
printf("\n===========================");
for(i=1;i<=26;i++,chu++,chl++)
printf("\n|%c | %d \t|| %c| %d |",chl,chl, chu, chu);
printf("\n===========================");
}




  
Output
===========================
|a | 97 || A| 65 |
|b | 98 || B| 66 |
|c | 99 || C| 67 |
|d | 100 || D| 68 |
|e | 101 || E| 69 |
|f | 102 || F| 70 |
|g | 103 || G| 71 |
|h | 104 || H| 72 |
|i | 105 || I| 73 |
|j | 106 || J| 74 |
|k | 107 || K| 75 |
|l | 108 || L| 76 |
|m | 109 || M| 77 |
|n | 110 || N| 78 |
|o | 111 || O| 79 |
|p | 112 || P| 80 |
|q | 113 || Q| 81 |
|r | 114 || R| 82 |
|s | 115 || S| 83 |
|t | 116 || T| 84 |
|u | 117 || U| 85 |
|v | 118 || V| 86 |
|w | 119 || W| 87 |
|x | 120 || X| 88 |
|y | 121 || Y| 89 |
|z | 122 || Z| 90 |
===========================
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Thursday, May 21, 2015

Print 1 to 10 using For Loop

 Sumit Kar     May 21, 2015     Basic, For Loop     No comments   

#include<stdio.h>
int main()
{
int x=1;

for(x=1; x<=10; x++)
printf("\n%d",x);

return 0;
}

  

Output
1
2
3
4
5
6
7
8
9
10
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp
Older Posts Home

Pages

  • Home

Trending Now

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • Addition of two numbers on Server sent from Client [TCP] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • Concatenation of two strings sent from Client on the Server - [ TCP ] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Blog Archive

  • ▼  2020 (1)
    • ▼  May (1)
      • Automating your deployments using Ansible
  • ►  2015 (92)
    • ►  October (4)
    • ►  September (3)
    • ►  August (3)
    • ►  July (9)
    • ►  June (9)
    • ►  May (20)
    • ►  April (7)
    • ►  March (22)
    • ►  February (7)
    • ►  January (8)
  • ►  2014 (158)
    • ►  November (70)
    • ►  October (6)
    • ►  September (82)
Powered by Blogger.

Categories

C - Programming Java Programming Basic Technology 8085 Assembly Programming For Loop Numerical Methods WhatsApp Algorithm Shell Programming Programming Networking Windows Android C++ Programming CPP If Else Tricky Internet Microsoft Pattern Photography Socket Program News While Loop Array DBMS DS Macro Recursion User Defined Function Conditional Operator Data Structure Durga Puja Earthquake Google Mela Nokia SQL Share Yahoo Airtel Bio Command Prompt Confused Facebook Finance Firefox Ganges Graph HokKolorob Input Kolkata MCQ Math Matrix NetNeutrality Oracle Religion Search Sumit Kar Survey Switch Case Viral Virus Visual Studio do-while featured featured_slider

Popular Programs

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • Addition of two numbers on Server sent from Client [TCP] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • Concatenation of two strings sent from Client on the Server - [ TCP ] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Daily Hits

Copyright © Sumit Kar