First Step in C Programming: Exploring A.P & G.P Calculations

Excited to share that I've just scratched the basics of C programming! ?? I’ve successfully written my first code using only IF statements and FOR loops to calculate Arithmetic Progression (A.P) and Geometric Progression (G.P) based on user inputs. It’s just the beginning, and I’m eager to dive deeper into the world of programming! #CProgramming #Learning #CodingJourney #TechSkills"

#include <stdio.h>

int main()
{
    int a,d,r,t,p,n,i,sum;

	printf("1. Arithematic progression\n2. Geometric Progression\nEnter the Number of desired Progression: ");
	scanf("%d", &p);

	if(p==1)
	{
		printf("Enter the number of terms: ");
		scanf("%d", &n);

		printf("Enter the first term: ");
		scanf("%d", &a);

		printf("Enter the common difference: ");
		scanf("%d", &d);

		sum = 0;

		for (i=1;i<=n;i++)
		{
			t = a + ((i-1)*d);
			sum = sum + t;
			printf("t_%d = %d\n", i,t);
		}
		printf("The sum of all the terms is %d", sum);
	}

	else
	{
		printf("Enter the number of terms: ");
		scanf("%d", &n);

		printf("Enter the first term: ");
		scanf("%d", &a);

		printf("Enter the constant ratio: ");
		scanf("%d", &r);

		sum = 0;

		for (i=1;i<=n;i++)
		{
			t = a;
			sum = sum + t;
			printf("t_%d = %d\n", i,t);
			a = t * r;
			
		}
		printf("The sum of all the terms is %d", sum);
	}

	return 0;

}        

要查看或添加评论,请登录

Tushar Singh的更多文章

  • C Code to find the day of the week on a particular Date

    C Code to find the day of the week on a particular Date

    ?? Just Completed Another C Program! ?? I’m excited to share that I've just written a C program that takes any date…

    1 条评论
  • C Program for matrix multiplication

    C Program for matrix multiplication

    ?? Excited to share another basic code I've written for matrix multiplication! This C program takes user inputs for two…

    1 条评论

社区洞察

其他会员也浏览了